2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-02-12 21:14:48 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth.models import UserManager
|
|
|
|
from django.utils import timezone
|
2013-04-01 16:57:50 +02:00
|
|
|
from zephyr.lib.initial_password import initial_api_key
|
|
|
|
from zephyr.models import UserProfile, Recipient, Subscription
|
2013-02-12 21:14:48 +01:00
|
|
|
import base64
|
|
|
|
import hashlib
|
|
|
|
|
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
|
2013-05-03 00:25:43 +02:00
|
|
|
def create_user_profile(realm, email, password, active, bot, full_name, short_name, bot_owner):
|
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-03 00:25:43 +02:00
|
|
|
pointer=-1, is_bot=bot, bot_owner=bot_owner)
|
2013-02-12 21:14:48 +01:00
|
|
|
|
2013-05-03 00:25:43 +02:00
|
|
|
if bot or not active:
|
2013-04-01 16:57:50 +02:00
|
|
|
user_profile.set_unusable_password()
|
2013-05-03 00:25:43 +02:00
|
|
|
else:
|
|
|
|
user_profile.set_password(password)
|
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
user_profile.api_key = initial_api_key(email)
|
|
|
|
return user_profile
|
2013-02-12 21:14:48 +01:00
|
|
|
|
|
|
|
def create_user(email, password, realm, full_name, short_name,
|
2013-05-03 00:25:43 +02:00
|
|
|
active=True, bot=False, bot_owner=None):
|
|
|
|
user_profile = create_user_profile(realm, email, password, active, bot,
|
|
|
|
full_name, short_name, bot_owner)
|
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
|