utils: Cast generate_random_token to str.

Having this be Text is forcing various URLs, emails, etc to be type
annotated as Text.
This commit is contained in:
Rishi Gupta 2017-07-07 17:46:51 -07:00 committed by Tim Abbott
parent a610cec8fc
commit 3bc74113ad
5 changed files with 13 additions and 13 deletions

View File

@ -22,7 +22,7 @@ from typing import Any, Dict, Optional, Text, Union
B16_RE = re.compile('^[a-f0-9]{40}$')
def generate_key():
# type: () -> Text
# type: () -> str
return generate_random_token(40)
class ConfirmationManager(models.Manager):
@ -47,17 +47,17 @@ class ConfirmationManager(models.Manager):
return False
def get_link_for_object(self, obj, host):
# type: (Union[ContentType, int], str) -> Text
# type: (Union[ContentType, int], str) -> str
key = generate_key()
self.create(content_object=obj, date_sent=timezone_now(), confirmation_key=key)
return self.get_activation_url(key, host)
def get_activation_url(self, confirmation_key, host):
# type: (Text, str) -> Text
return u'%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
host,
reverse(self.url_pattern_name,
kwargs={'confirmation_key': confirmation_key}))
# type: (str, str) -> str
return '%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
host,
reverse(self.url_pattern_name,
kwargs={'confirmation_key': confirmation_key}))
class EmailChangeConfirmationManager(ConfirmationManager):
url_pattern_name = 'zerver.views.user_settings.confirm_email_change'

View File

@ -102,7 +102,7 @@ def get_missed_message_token_from_address(address):
return msg_string[2:]
def create_missed_message_address(user_profile, message):
# type: (UserProfile, Message) -> Text
# type: (UserProfile, Message) -> str
if settings.EMAIL_GATEWAY_PATTERN == '':
logging.warning("EMAIL_GATEWAY_PATTERN is an empty string, using "
"NOREPLY_EMAIL_ADDRESS in the 'from' field.")

View File

@ -116,8 +116,8 @@ def log_statsd_event(name):
statsd.incr(event_name)
def generate_random_token(length):
# type: (int) -> Text
return base64.b16encode(os.urandom(length // 2)).decode('utf-8').lower()
# type: (int) -> str
return str(base64.b16encode(os.urandom(length // 2)).decode('utf-8').lower())
def mkdir_p(path):
# type: (str) -> None

View File

@ -863,7 +863,7 @@ class PushDeviceToken(AbstractPushDeviceToken):
user = models.ForeignKey(UserProfile, db_index=True, on_delete=CASCADE) # type: UserProfile
def generate_email_token_for_stream():
# type: () -> Text
# type: () -> str
return generate_random_token(32)
class Stream(ModelReprMixin, models.Model):
@ -875,7 +875,7 @@ class Stream(ModelReprMixin, models.Model):
# e-mail length of 254, and our max stream length is 30, so we
# have plenty of room for the token.
email_token = models.CharField(
max_length=32, default=generate_email_token_for_stream) # type: Text
max_length=32, default=generate_email_token_for_stream) # type: str
description = models.CharField(max_length=1024, default=u'') # type: Text
date_created = models.DateTimeField(default=timezone_now) # type: datetime.datetime

View File

@ -384,7 +384,7 @@ def create_user_backend(request, user_profile, email=REQ(), password=REQ(),
return json_success()
def generate_client_id():
# type: () -> Text
# type: () -> str
return generate_random_token(32)
def get_profile_backend(request, user_profile):