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
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
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-12-21 13:17:53 +01:00
|
|
|
from typing import Optional, Text
|
2016-06-16 22:22:26 +02:00
|
|
|
|
2013-07-19 17:34:58 +02:00
|
|
|
def random_api_key():
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: () -> Text
|
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,
|
2017-03-22 23:26:35 +01:00
|
|
|
short_name, bot_owner, is_mirror_dummy, tos_version,
|
2017-03-29 23:21:44 +02:00
|
|
|
tutorial_status=UserProfile.TUTORIAL_WAITING,
|
|
|
|
enter_sends=False):
|
|
|
|
# type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text], bool) -> UserProfile
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2013-02-12 21:14:48 +01:00
|
|
|
email = UserManager.normalize_email(email)
|
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,
|
2017-01-12 11:14:13 +01:00
|
|
|
bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,
|
2016-08-10 03:05:26 +02:00
|
|
|
tos_version=tos_version,
|
2017-03-22 23:26:35 +01:00
|
|
|
tutorial_status=tutorial_status,
|
2017-03-29 23:21:44 +02:00
|
|
|
enter_sends=enter_sends,
|
2016-08-04 17:32:41 +02:00
|
|
|
onboarding_steps=ujson.dumps([]),
|
|
|
|
default_language=realm.default_language)
|
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-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text, Realm, Text, Text, bool, Optional[int], Optional[UserProfile], Optional[Text], Text, 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
|