2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-02-12 21:14:48 +01:00
|
|
|
from django.contrib.auth.models import UserManager
|
|
|
|
from django.utils import timezone
|
2016-06-16 22:22:26 +02:00
|
|
|
from zerver.models import UserProfile, Recipient, Subscription, Realm, Stream
|
2013-02-12 21:14:48 +01:00
|
|
|
import base64
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2013-07-30 01:59:24 +02:00
|
|
|
import os
|
2013-05-30 20:21:26 +02:00
|
|
|
import string
|
2015-11-01 17:15:05 +01:00
|
|
|
from six.moves import range
|
2013-05-08 15:27:27 +02:00
|
|
|
|
2016-06-16 22:22:26 +02:00
|
|
|
from six import text_type
|
|
|
|
from typing import Optional
|
|
|
|
|
2013-07-19 17:34:58 +02:00
|
|
|
def random_api_key():
|
2016-06-16 22:22:26 +02:00
|
|
|
# type: () -> text_type
|
2013-07-30 01:59:24 +02:00
|
|
|
choices = string.ascii_letters + string.digits
|
2016-01-24 05:18:35 +01:00
|
|
|
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")
|
2013-07-19 17:34:58 +02:00
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
# create_user_profile is based on Django's User.objects.create_user,
|
2013-02-12 21:14:48 +01:00
|
|
|
# except that we don't save to the database so it can used in
|
|
|
|
# bulk_creates
|
2013-04-01 16:57:50 +02:00
|
|
|
#
|
|
|
|
# Only use this for bulk_create -- for normal usage one should use
|
|
|
|
# create_user (below) which will also make the Subscription and
|
|
|
|
# Recipient objects
|
2016-05-18 20:23:03 +02:00
|
|
|
def create_user_profile(realm, email, password, active, bot_type, full_name,
|
2016-08-10 03:05:26 +02:00
|
|
|
short_name, bot_owner, is_mirror_dummy, tos_version):
|
|
|
|
# type: (Realm, text_type, text_type, bool, Optional[int], text_type, text_type, Optional[UserProfile], bool, Optional[text_type]) -> UserProfile
|
2013-02-12 21:14:48 +01:00
|
|
|
now = timezone.now()
|
|
|
|
email = UserManager.normalize_email(email)
|
2014-10-01 02:17:11 +02:00
|
|
|
|
2015-02-06 00:30:08 +01:00
|
|
|
enable_stream_desktop_notifications = (realm.domain != 'zulip.com')
|
2014-10-01 02:17:11 +02:00
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
user_profile = UserProfile(email=email, is_staff=False, is_active=active,
|
|
|
|
full_name=full_name, short_name=short_name,
|
|
|
|
last_login=now, date_joined=now, realm=realm,
|
2016-05-18 20:23:03 +02:00
|
|
|
pointer=-1, is_bot=bool(bot_type), bot_type=bot_type,
|
2016-07-15 10:14:51 +02:00
|
|
|
is_mirror_dummy=is_mirror_dummy,
|
2016-08-10 03:05:26 +02:00
|
|
|
tos_version=tos_version,
|
2014-10-01 02:17:11 +02:00
|
|
|
enable_stream_desktop_notifications=enable_stream_desktop_notifications,
|
2016-08-04 17:32:41 +02:00
|
|
|
onboarding_steps=ujson.dumps([]),
|
|
|
|
default_language=realm.default_language)
|
2016-07-15 10:14:51 +02:00
|
|
|
if bot_owner is not None:
|
|
|
|
# `user_profile.bot_owner = bot_owner` doesn't work on python 3.4
|
|
|
|
user_profile.bot_owner_id = bot_owner.id
|
2013-02-12 21:14:48 +01:00
|
|
|
|
2016-05-18 20:23:03 +02:00
|
|
|
if bot_type or not active:
|
2013-11-12 18:20:05 +01:00
|
|
|
password = None
|
|
|
|
|
|
|
|
user_profile.set_password(password)
|
2013-05-03 00:25:43 +02:00
|
|
|
|
2013-07-19 17:34:58 +02:00
|
|
|
user_profile.api_key = random_api_key()
|
2013-04-01 16:57:50 +02:00
|
|
|
return user_profile
|
2013-02-12 21:14:48 +01:00
|
|
|
|
|
|
|
def create_user(email, password, realm, full_name, short_name,
|
2016-08-10 03:05:26 +02:00
|
|
|
active=True, bot_type=None, bot_owner=None, tos_version=None,
|
2014-01-07 18:57:54 +01:00
|
|
|
avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
|
2014-02-11 18:43:30 +01:00
|
|
|
is_mirror_dummy=False, default_sending_stream=None,
|
|
|
|
default_events_register_stream=None,
|
2014-05-03 03:04:16 +02:00
|
|
|
default_all_public_streams=None, user_profile_id=None):
|
2016-08-10 03:05:26 +02:00
|
|
|
# type: (text_type, text_type, Realm, text_type, text_type, bool, Optional[int], Optional[UserProfile], Optional[text_type], text_type, bool, Optional[Stream], Optional[Stream], Optional[bool], Optional[int]) -> UserProfile
|
2016-05-18 20:23:03 +02:00
|
|
|
user_profile = create_user_profile(realm, email, password, active, bot_type,
|
2014-01-07 18:57:54 +01:00
|
|
|
full_name, short_name, bot_owner,
|
2016-08-10 03:05:26 +02:00
|
|
|
is_mirror_dummy, tos_version)
|
2013-06-14 20:03:11 +02:00
|
|
|
user_profile.avatar_source = avatar_source
|
2014-02-11 18:43:30 +01:00
|
|
|
user_profile.default_sending_stream = default_sending_stream
|
|
|
|
user_profile.default_events_register_stream = default_events_register_stream
|
|
|
|
# Allow the ORM default to be used if not provided
|
|
|
|
if default_all_public_streams is not None:
|
|
|
|
user_profile.default_all_public_streams = default_all_public_streams
|
|
|
|
|
2014-05-03 03:04:16 +02:00
|
|
|
if user_profile_id is not None:
|
|
|
|
user_profile.id = user_profile_id
|
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
user_profile.save()
|
|
|
|
recipient = Recipient.objects.create(type_id=user_profile.id,
|
|
|
|
type=Recipient.PERSONAL)
|
|
|
|
Subscription.objects.create(user_profile=user_profile, recipient=recipient)
|
|
|
|
return user_profile
|