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
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import UserProfile, Recipient, Subscription
|
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
|
2013-05-08 15:27:27 +02:00
|
|
|
|
2013-07-19 17:34:58 +02:00
|
|
|
def random_api_key():
|
2013-07-30 01:59:24 +02:00
|
|
|
choices = string.ascii_letters + string.digits
|
|
|
|
altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)])
|
|
|
|
return base64.b64encode(os.urandom(24), altchars=altchars)
|
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
|
2014-01-07 18:57:54 +01:00
|
|
|
def create_user_profile(realm, email, password, active, bot, full_name,
|
|
|
|
short_name, bot_owner, is_mirror_dummy):
|
2013-02-12 21:14:48 +01:00
|
|
|
now = timezone.now()
|
|
|
|
email = UserManager.normalize_email(email)
|
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,
|
2013-05-08 15:27:27 +02:00
|
|
|
pointer=-1, is_bot=bot, bot_owner=bot_owner,
|
2014-01-07 18:57:54 +01:00
|
|
|
is_mirror_dummy=is_mirror_dummy,
|
2013-10-31 15:56:30 +01:00
|
|
|
onboarding_steps=ujson.dumps([]))
|
2013-02-12 21:14:48 +01:00
|
|
|
|
2013-05-03 00:25:43 +02:00
|
|
|
if bot 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,
|
2013-06-14 20:03:11 +02:00
|
|
|
active=True, bot=False, bot_owner=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):
|
2013-05-03 00:25:43 +02:00
|
|
|
user_profile = create_user_profile(realm, email, password, active, bot,
|
2014-01-07 18:57:54 +01:00
|
|
|
full_name, short_name, bot_owner,
|
|
|
|
is_mirror_dummy)
|
2014-02-11 18:43:30 +01:00
|
|
|
|
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
|