utils: Move random API key generator as generate_api_key.

random_api_key, the function we use to generate random tokens for API
keys, has been moved to zerver/lib/utils.py because it's used in more
parts of the codebase (apart from user creation), and having it in
zerver/lib/create_user.py was prone to cyclic dependencies.

The function has also been renamed to generate_api_key to have an
imperative name, that makes clearer what it does.
This commit is contained in:
Yago González
2018-08-01 11:18:37 +02:00
committed by Tim Abbott
parent f6219745de
commit 6a192ac84c
6 changed files with 19 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ from django.utils.timezone import now as timezone_now
from zerver.models import UserProfile, Recipient, Subscription, Realm, Stream
from zerver.lib.upload import copy_avatar
from zerver.lib.hotspots import copy_hotpots
from zerver.lib.utils import generate_api_key
import base64
import ujson
@@ -12,11 +13,6 @@ import string
from typing import Optional
def random_api_key() -> str:
choices = string.ascii_letters + string.digits
altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)]).encode("utf-8")
return base64.b64encode(os.urandom(24), altchars=altchars).decode("utf-8")
def copy_user_settings(source_profile: UserProfile, target_profile: UserProfile) -> None:
"""Warning: Does not save, to avoid extra database queries"""
for settings_name in UserProfile.property_types:
@@ -73,7 +69,7 @@ def create_user_profile(realm: Realm, email: str, password: Optional[str],
user_profile.set_password(password)
user_profile.api_key = random_api_key()
user_profile.api_key = generate_api_key()
return user_profile
def create_user(email: str, password: Optional[str], realm: Realm,