2012-08-28 18:44:51 +02:00
|
|
|
from django.db import models
|
2012-09-19 19:39:34 +02:00
|
|
|
from django.conf import settings
|
2012-08-28 18:44:51 +02:00
|
|
|
from django.contrib.auth.models import User
|
2012-09-04 23:20:21 +02:00
|
|
|
import hashlib
|
2012-09-21 17:25:20 +02:00
|
|
|
import base64
|
2012-09-12 22:35:06 +02:00
|
|
|
import calendar
|
2012-09-19 18:41:42 +02:00
|
|
|
from zephyr.lib.cache import cache_with_key
|
2012-10-18 03:08:59 +02:00
|
|
|
from zephyr.lib.initial_password import initial_password, initial_api_key
|
2012-09-27 19:58:42 +02:00
|
|
|
import fcntl
|
|
|
|
import os
|
|
|
|
import simplejson
|
2012-10-23 23:29:56 +02:00
|
|
|
from django.db import transaction, IntegrityError
|
2012-10-15 22:03:50 +02:00
|
|
|
from zephyr.lib import bugdown
|
2012-10-20 18:02:58 +02:00
|
|
|
from zephyr.lib.bulk_create import batch_bulk_create
|
2012-10-17 04:07:35 +02:00
|
|
|
from zephyr.lib.avatar import gravatar_hash
|
2012-10-09 22:21:03 +02:00
|
|
|
import requests
|
2012-10-20 18:02:58 +02:00
|
|
|
from django.contrib.auth.models import UserManager
|
|
|
|
from django.utils import timezone
|
2012-10-29 19:43:00 +01:00
|
|
|
from django.contrib.sessions.models import Session
|
2012-11-06 19:21:51 +01:00
|
|
|
import time
|
2012-09-21 16:10:36 +02:00
|
|
|
|
2012-11-02 18:12:12 +01:00
|
|
|
@cache_with_key(lambda self: 'display_recipient_dict:%d' % (self.id,))
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def get_display_recipient(recipient):
|
|
|
|
"""
|
2012-10-30 21:47:43 +01:00
|
|
|
recipient: an instance of Recipient.
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-10-10 22:58:51 +02:00
|
|
|
returns: an appropriate string describing the recipient (the stream
|
|
|
|
name, for a stream, or the email, for a user).
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
"""
|
2012-10-10 22:57:21 +02:00
|
|
|
if recipient.type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=recipient.type_id)
|
|
|
|
return stream.name
|
2012-09-07 20:14:13 +02:00
|
|
|
elif recipient.type == Recipient.HUDDLE:
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile_list = [UserProfile.objects.select_related().get(user=s.user_profile) for s in
|
2012-09-26 19:30:01 +02:00
|
|
|
Subscription.objects.filter(recipient=recipient)]
|
2012-09-26 21:25:49 +02:00
|
|
|
return [{'email': user_profile.user.email,
|
2012-10-12 16:47:01 +02:00
|
|
|
'full_name': user_profile.full_name,
|
|
|
|
'short_name': user_profile.short_name} for user_profile in user_profile_list]
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
else:
|
2012-10-15 18:29:24 +02:00
|
|
|
user_profile = UserProfile.objects.select_related().get(user=recipient.type_id)
|
2012-10-12 16:47:01 +02:00
|
|
|
return {'email': user_profile.user.email,
|
|
|
|
'full_name': user_profile.full_name,
|
|
|
|
'short_name': user_profile.short_name}
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
def get_log_recipient(recipient):
|
|
|
|
"""
|
2012-10-30 21:47:43 +01:00
|
|
|
recipient: an instance of Recipient.
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-10 22:58:51 +02:00
|
|
|
returns: an appropriate string describing the recipient (the stream
|
|
|
|
name, for a stream, or the email, for a user).
|
2012-09-27 19:58:42 +02:00
|
|
|
"""
|
2012-10-10 22:57:21 +02:00
|
|
|
if recipient.type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=recipient.type_id)
|
|
|
|
return stream.name
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile_list = [UserProfile.objects.select_related().get(user=s.user_profile) for s in
|
2012-09-27 19:58:42 +02:00
|
|
|
Subscription.objects.filter(recipient=recipient)]
|
|
|
|
return [{'email': user_profile.user.email,
|
|
|
|
'full_name': user_profile.full_name,
|
|
|
|
'short_name': user_profile.short_name} for user_profile in user_profile_list]
|
|
|
|
|
2012-10-31 16:07:36 +01:00
|
|
|
class Callbacks(object):
|
2012-10-17 21:18:20 +02:00
|
|
|
TYPE_RECEIVE = 0
|
2012-10-17 23:10:23 +02:00
|
|
|
TYPE_POINTER_UPDATE = 1
|
|
|
|
TYPE_MAX = 2
|
2012-10-17 21:18:20 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.table = {}
|
|
|
|
|
|
|
|
def add(self, key, cb_type, callback):
|
|
|
|
if not self.table.has_key(key):
|
|
|
|
self.create_key(key)
|
|
|
|
self.table[key][cb_type].append(callback)
|
|
|
|
|
2012-10-31 16:22:03 +01:00
|
|
|
def call(self, key, cb_type, **kwargs):
|
2012-10-17 21:18:20 +02:00
|
|
|
if not self.table.has_key(key):
|
|
|
|
self.create_key(key)
|
|
|
|
|
2012-10-31 16:22:03 +01:00
|
|
|
for cb in self.table[key][cb_type]:
|
|
|
|
cb(**kwargs)
|
|
|
|
|
2012-10-17 21:18:20 +02:00
|
|
|
self.table[key][cb_type] = []
|
|
|
|
|
|
|
|
def create_key(self, key):
|
|
|
|
self.table[key] = [[] for i in range(0, Callbacks.TYPE_MAX)]
|
|
|
|
|
2012-10-31 16:22:03 +01:00
|
|
|
|
2012-09-05 21:49:56 +02:00
|
|
|
class Realm(models.Model):
|
2012-10-22 19:23:11 +02:00
|
|
|
domain = models.CharField(max_length=40, db_index=True, unique=True)
|
2012-09-05 21:49:56 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Realm: %s %s>" % (self.domain, self.id)
|
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def bulk_create_realms(realm_list):
|
2012-10-31 16:28:27 +01:00
|
|
|
existing_realms = set(r.domain for r in Realm.objects.select_related().all())
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
realms_to_create = []
|
|
|
|
for domain in realm_list:
|
|
|
|
if domain not in existing_realms:
|
|
|
|
realms_to_create.append(Realm(domain=domain))
|
|
|
|
existing_realms.add(domain)
|
|
|
|
batch_bulk_create(Realm, realms_to_create)
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class UserProfile(models.Model):
|
|
|
|
user = models.OneToOneField(User)
|
2012-09-11 19:20:01 +02:00
|
|
|
full_name = models.CharField(max_length=100)
|
|
|
|
short_name = models.CharField(max_length=100)
|
2012-08-28 18:44:51 +02:00
|
|
|
pointer = models.IntegerField()
|
2012-10-17 17:42:40 +02:00
|
|
|
last_pointer_updater = models.CharField(max_length=64)
|
2012-09-05 21:49:56 +02:00
|
|
|
realm = models.ForeignKey(Realm)
|
2012-10-01 21:36:44 +02:00
|
|
|
api_key = models.CharField(max_length=32)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-10-31 16:18:36 +01:00
|
|
|
# This is class data, not instance data!
|
|
|
|
# There is one callbacks_table for the whole process.
|
|
|
|
callbacks_table = Callbacks()
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
# The user receives this message
|
2012-10-09 22:21:03 +02:00
|
|
|
# Called in the Tornado process
|
2012-08-28 22:56:21 +02:00
|
|
|
def receive(self, message):
|
2012-10-31 16:22:03 +01:00
|
|
|
self.callbacks_table.call(self.user.id, Callbacks.TYPE_RECEIVE,
|
|
|
|
messages=[message], update_types=["new_messages"])
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-10-23 20:03:45 +02:00
|
|
|
def update_pointer(self, new_pointer, pointer_updater):
|
2012-10-31 16:22:03 +01:00
|
|
|
self.callbacks_table.call(self.user.id, Callbacks.TYPE_POINTER_UPDATE,
|
|
|
|
new_pointer=new_pointer, pointer_updater=pointer_updater)
|
2012-10-17 23:10:23 +02:00
|
|
|
|
2012-10-17 21:18:20 +02:00
|
|
|
def add_receive_callback(self, cb):
|
2012-10-31 16:18:36 +01:00
|
|
|
self.callbacks_table.add(self.user.id, Callbacks.TYPE_RECEIVE, cb)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-10-17 23:10:23 +02:00
|
|
|
def add_pointer_update_callback(self, cb):
|
2012-10-31 16:18:36 +01:00
|
|
|
self.callbacks_table.add(self.user.id, Callbacks.TYPE_POINTER_UPDATE, cb)
|
2012-10-17 23:10:23 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
2012-09-21 00:26:59 +02:00
|
|
|
return "<UserProfile: %s %s>" % (self.user.email, self.realm)
|
2012-09-05 21:49:56 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-09-19 18:54:57 +02:00
|
|
|
@classmethod
|
|
|
|
def create(cls, user, realm, full_name, short_name):
|
|
|
|
"""When creating a new user, make a profile for him or her."""
|
|
|
|
if not cls.objects.filter(user=user):
|
2012-11-02 23:41:51 +01:00
|
|
|
profile = cls(user=user, pointer=-1, realm=realm,
|
2012-09-19 18:54:57 +02:00
|
|
|
full_name=full_name, short_name=short_name)
|
2012-10-12 17:04:26 +02:00
|
|
|
profile.api_key = initial_api_key(user.email)
|
2012-09-19 18:54:57 +02:00
|
|
|
profile.save()
|
|
|
|
# Auto-sub to the ability to receive personals.
|
2012-10-19 23:40:44 +02:00
|
|
|
recipient = Recipient.objects.create(type_id=profile.id, type=Recipient.PERSONAL)
|
2012-10-22 20:15:25 +02:00
|
|
|
Subscription.objects.create(user_profile=profile, recipient=recipient)
|
2012-10-31 19:06:06 +01:00
|
|
|
return profile
|
2012-08-29 17:50:36 +02:00
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
class PreregistrationUser(models.Model):
|
|
|
|
email = models.EmailField(unique=True)
|
2012-10-29 19:08:18 +01:00
|
|
|
# status: whether an object has been confirmed.
|
|
|
|
# if confirmed, set to confirmation.settings.STATUS_ACTIVE
|
|
|
|
status = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
class MitUser(models.Model):
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
# status: whether an object has been confirmed.
|
|
|
|
# if confirmed, set to confirmation.settings.STATUS_ACTIVE
|
2012-09-28 22:47:05 +02:00
|
|
|
status = models.IntegerField(default=0)
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
# create_user_hack is the same as Django's User.objects.create_user,
|
|
|
|
# except that we don't save to the database so it can used in
|
|
|
|
# bulk_creates
|
2012-10-23 22:39:40 +02:00
|
|
|
def create_user_hack(username, password, email, active):
|
2012-10-20 18:02:58 +02:00
|
|
|
now = timezone.now()
|
|
|
|
email = UserManager.normalize_email(email)
|
|
|
|
user = User(username=username, email=email,
|
2012-10-23 22:39:40 +02:00
|
|
|
is_staff=False, is_active=active, is_superuser=False,
|
2012-10-20 18:02:58 +02:00
|
|
|
last_login=now, date_joined=now)
|
|
|
|
|
2012-10-23 22:39:40 +02:00
|
|
|
if active:
|
|
|
|
user.set_password(password)
|
|
|
|
else:
|
|
|
|
user.set_unusable_password()
|
2012-10-20 18:02:58 +02:00
|
|
|
return user
|
|
|
|
|
2012-10-23 22:39:40 +02:00
|
|
|
def create_user_base(email, password, active=True):
|
2012-09-21 17:25:20 +02:00
|
|
|
# NB: the result of Base32 + truncation is not a valid Base32 encoding.
|
|
|
|
# It's just a unique alphanumeric string.
|
|
|
|
# Use base32 instead of base64 so we don't have to worry about mixed case.
|
|
|
|
# Django imposes a limit of 30 characters on usernames.
|
|
|
|
email_hash = hashlib.sha256(settings.HASH_SALT + email).digest()
|
|
|
|
username = base64.b32encode(email_hash)[:30]
|
2012-10-23 22:39:40 +02:00
|
|
|
return create_user_hack(username, password, email, active)
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-10-29 20:56:02 +01:00
|
|
|
def create_user(email, password, realm, full_name, short_name,
|
|
|
|
active=True):
|
|
|
|
user = create_user_base(email=email, password=password,
|
|
|
|
active=active)
|
2012-09-21 16:40:46 +02:00
|
|
|
user.save()
|
2012-10-22 23:45:59 +02:00
|
|
|
return UserProfile.create(user, realm, full_name, short_name)
|
2012-09-21 16:40:46 +02:00
|
|
|
|
2012-10-19 23:40:44 +02:00
|
|
|
# TODO: This has a race where a user could be created twice. Need to
|
|
|
|
# add transactions.
|
2012-10-29 20:56:02 +01:00
|
|
|
def create_user_if_needed(realm, email, full_name, short_name,
|
|
|
|
active=True):
|
2012-09-27 20:46:42 +02:00
|
|
|
try:
|
2012-10-22 23:45:59 +02:00
|
|
|
return UserProfile.objects.get(user__email=email)
|
|
|
|
except UserProfile.DoesNotExist:
|
2012-09-27 20:46:42 +02:00
|
|
|
# forge a user for this person
|
2012-10-22 23:45:59 +02:00
|
|
|
return create_user(email, initial_password(email), realm,
|
2012-10-29 20:56:02 +01:00
|
|
|
full_name, short_name, active=active)
|
2012-09-27 20:46:42 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def bulk_create_users(realms, users_raw):
|
|
|
|
"""
|
|
|
|
Creates and saves a User with the given email.
|
|
|
|
Has some code based off of UserManage.create_user, but doesn't .save()
|
|
|
|
"""
|
|
|
|
users = []
|
|
|
|
existing_users = set(u.email for u in User.objects.all())
|
2012-10-23 22:39:40 +02:00
|
|
|
for (email, full_name, short_name, active) in users_raw:
|
2012-10-20 18:02:58 +02:00
|
|
|
if email in existing_users:
|
|
|
|
continue
|
2012-10-23 22:39:40 +02:00
|
|
|
users.append((email, full_name, short_name, active))
|
2012-10-20 18:02:58 +02:00
|
|
|
existing_users.add(email)
|
|
|
|
|
|
|
|
users_to_create = []
|
2012-10-23 22:39:40 +02:00
|
|
|
for (email, full_name, short_name, active) in users:
|
|
|
|
users_to_create.append(create_user_base(email, initial_password(email),
|
|
|
|
active=active))
|
2012-10-20 18:02:58 +02:00
|
|
|
batch_bulk_create(User, users_to_create, 30)
|
|
|
|
|
|
|
|
users_by_email = {}
|
|
|
|
for user in User.objects.all():
|
|
|
|
users_by_email[user.email] = user
|
|
|
|
|
|
|
|
# Now create user_profiles
|
|
|
|
profiles_to_create = []
|
2012-10-23 22:39:40 +02:00
|
|
|
for (email, full_name, short_name, active) in users:
|
2012-10-20 18:02:58 +02:00
|
|
|
domain = email.split('@')[1]
|
|
|
|
profile = UserProfile(user=users_by_email[email], pointer=-1,
|
2012-11-02 23:41:51 +01:00
|
|
|
realm=realms[domain],
|
2012-10-20 18:02:58 +02:00
|
|
|
full_name=full_name, short_name=short_name)
|
2012-10-21 00:48:12 +02:00
|
|
|
profile.api_key = initial_api_key(email)
|
2012-10-20 18:02:58 +02:00
|
|
|
profiles_to_create.append(profile)
|
|
|
|
batch_bulk_create(UserProfile, profiles_to_create, 50)
|
|
|
|
|
|
|
|
profiles_by_email = {}
|
|
|
|
profiles_by_id = {}
|
|
|
|
for profile in UserProfile.objects.select_related().all():
|
|
|
|
profiles_by_email[profile.user.email] = profile
|
|
|
|
profiles_by_id[profile.user.id] = profile
|
|
|
|
|
|
|
|
recipients_to_create = []
|
2012-10-23 22:39:40 +02:00
|
|
|
for (email, _, _, _) in users:
|
2012-10-20 18:02:58 +02:00
|
|
|
recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id,
|
|
|
|
type=Recipient.PERSONAL))
|
|
|
|
batch_bulk_create(Recipient, recipients_to_create)
|
|
|
|
|
|
|
|
recipients_by_email = {}
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):
|
|
|
|
recipients_by_email[profiles_by_id[recipient.type_id].user.email] = recipient
|
|
|
|
|
|
|
|
subscriptions_to_create = []
|
2012-10-23 22:39:40 +02:00
|
|
|
for (email, _, _, _) in users:
|
2012-10-31 16:28:44 +01:00
|
|
|
subscriptions_to_create.append(
|
2012-10-22 20:15:25 +02:00
|
|
|
Subscription(user_profile_id=profiles_by_email[email].id,
|
2012-10-20 18:02:58 +02:00
|
|
|
recipient=recipients_by_email[email]))
|
|
|
|
batch_bulk_create(Subscription, subscriptions_to_create)
|
|
|
|
|
2012-10-10 22:58:51 +02:00
|
|
|
def create_stream_if_needed(realm, stream_name):
|
2012-10-19 23:40:44 +02:00
|
|
|
(stream, created) = Stream.objects.get_or_create(
|
|
|
|
realm=realm, name__iexact=stream_name,
|
|
|
|
defaults={'name': stream_name})
|
|
|
|
if created:
|
|
|
|
Recipient.objects.create(type_id=stream.id, type=Recipient.STREAM)
|
|
|
|
return stream
|
2012-09-27 20:46:42 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def bulk_create_streams(realms, stream_list):
|
2012-10-31 16:28:27 +01:00
|
|
|
existing_streams = set((stream.realm.domain, stream.name.lower())
|
|
|
|
for stream in Stream.objects.select_related().all())
|
2012-10-20 18:02:58 +02:00
|
|
|
streams_to_create = []
|
|
|
|
for (domain, name) in stream_list:
|
2012-10-21 01:52:01 +02:00
|
|
|
if (domain, name.lower()) not in existing_streams:
|
2012-10-20 18:02:58 +02:00
|
|
|
streams_to_create.append(Stream(realm=realms[domain], name=name))
|
|
|
|
batch_bulk_create(Stream, streams_to_create)
|
|
|
|
|
|
|
|
recipients_to_create = []
|
|
|
|
for stream in Stream.objects.all():
|
2012-10-21 02:06:43 +02:00
|
|
|
if (stream.realm.domain, stream.name.lower()) not in existing_streams:
|
2012-10-20 18:02:58 +02:00
|
|
|
recipients_to_create.append(Recipient(type_id=stream.id,
|
|
|
|
type=Recipient.STREAM))
|
|
|
|
batch_bulk_create(Recipient, recipients_to_create)
|
|
|
|
|
2012-10-10 22:53:24 +02:00
|
|
|
class Stream(models.Model):
|
2012-09-14 23:28:38 +02:00
|
|
|
name = models.CharField(max_length=30, db_index=True)
|
|
|
|
realm = models.ForeignKey(Realm, db_index=True)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
2012-10-10 22:53:24 +02:00
|
|
|
return "<Stream: %s>" % (self.name,)
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-11-07 22:33:38 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("name", "realm")
|
|
|
|
|
2012-09-19 18:54:57 +02:00
|
|
|
@classmethod
|
|
|
|
def create(cls, name, realm):
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = cls(name=name, realm=realm)
|
|
|
|
stream.save()
|
2012-09-07 19:24:54 +02:00
|
|
|
|
2012-10-19 23:40:44 +02:00
|
|
|
recipient = Recipient.objects.create(type_id=stream.id,
|
|
|
|
type=Recipient.STREAM)
|
2012-10-10 22:53:24 +02:00
|
|
|
return (stream, recipient)
|
2012-09-07 19:24:54 +02:00
|
|
|
|
2012-08-28 21:27:42 +02:00
|
|
|
class Recipient(models.Model):
|
2012-09-14 23:28:38 +02:00
|
|
|
type_id = models.IntegerField(db_index=True)
|
|
|
|
type = models.PositiveSmallIntegerField(db_index=True)
|
2012-10-10 22:58:51 +02:00
|
|
|
# Valid types are {personal, stream, huddle}
|
2012-09-07 20:14:13 +02:00
|
|
|
PERSONAL = 1
|
2012-10-10 22:57:21 +02:00
|
|
|
STREAM = 2
|
2012-09-07 20:14:13 +02:00
|
|
|
HUDDLE = 3
|
|
|
|
|
2012-11-07 22:33:38 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("type", "type_id")
|
|
|
|
|
2012-09-07 20:14:13 +02:00
|
|
|
def type_name(self):
|
|
|
|
if self.type == self.PERSONAL:
|
|
|
|
return "personal"
|
2012-10-10 22:57:21 +02:00
|
|
|
elif self.type == self.STREAM:
|
|
|
|
return "stream"
|
2012-09-07 20:14:13 +02:00
|
|
|
elif self.type == self.HUDDLE:
|
|
|
|
return "huddle"
|
|
|
|
else:
|
2012-10-31 16:40:41 +01:00
|
|
|
raise ValueError('Bad recipient type')
|
2012-08-28 21:27:42 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self)
|
2012-09-04 22:22:06 +02:00
|
|
|
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-10-19 21:30:42 +02:00
|
|
|
class Client(models.Model):
|
2012-10-22 19:23:11 +02:00
|
|
|
name = models.CharField(max_length=30, db_index=True, unique=True)
|
2012-10-19 21:30:42 +02:00
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
@transaction.commit_on_success
|
2012-10-19 21:30:42 +02:00
|
|
|
def get_client(name):
|
2012-10-23 23:29:56 +02:00
|
|
|
try:
|
|
|
|
(client, _) = Client.objects.get_or_create(name=name)
|
|
|
|
except IntegrityError:
|
2012-11-08 00:11:31 +01:00
|
|
|
# If we're racing with other threads trying to create this
|
|
|
|
# client, get_or_create will throw IntegrityError (because our
|
|
|
|
# database is enforcing the no-duplicate-objects constraint);
|
|
|
|
# in this case one should just re-fetch the object. This race
|
|
|
|
# actually happens with populate_db.
|
|
|
|
#
|
|
|
|
# Much of the rest of our code that writes to the database
|
|
|
|
# doesn't handle this duplicate object on race issue correctly :(
|
2012-10-23 23:29:56 +02:00
|
|
|
transaction.commit()
|
|
|
|
return Client.objects.get(name=name)
|
2012-10-19 21:30:42 +02:00
|
|
|
return client
|
|
|
|
|
|
|
|
def bulk_create_clients(client_list):
|
2012-10-31 16:28:27 +01:00
|
|
|
existing_clients = set(client.name for client in Client.objects.select_related().all())
|
2012-10-19 21:30:42 +02:00
|
|
|
|
|
|
|
clients_to_create = []
|
|
|
|
for name in client_list:
|
|
|
|
if name not in existing_clients:
|
|
|
|
clients_to_create.append(Client(name=name))
|
|
|
|
existing_clients.add(name)
|
|
|
|
batch_bulk_create(Client, clients_to_create)
|
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
class Message(models.Model):
|
2012-08-28 18:44:51 +02:00
|
|
|
sender = models.ForeignKey(UserProfile)
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient = models.ForeignKey(Recipient)
|
2012-10-11 00:19:38 +02:00
|
|
|
subject = models.CharField(max_length=60)
|
2012-09-14 19:16:01 +02:00
|
|
|
content = models.TextField()
|
2012-08-28 18:44:51 +02:00
|
|
|
pub_date = models.DateTimeField('date published')
|
2012-10-19 21:30:42 +02:00
|
|
|
sending_client = models.ForeignKey(Client)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self.recipient)
|
2012-10-11 00:01:39 +02:00
|
|
|
return "<Message: %s / %s / %r>" % (display_recipient, self.subject, self.sender)
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
Give our models meaningful reprs.
>>> from zephyr.models import UserProfile, Recipient, Zephyr, ZephyrClass
>>> for klass in [UserProfile, Recipient, Zephyr, ZephyrClass]:
... print klass.objects.all()[:2]
...
[<UserProfile: othello>, <UserProfile: iago>]
[<Recipient: Verona (1, class)>, <Recipient: Denmark (2, class)>]
[<Zephyr: Scotland / Scotland3 / <UserProfile: prospero>>, <Zephyr: Venice / Venice3 / <UserProfile: iago>>]
[<ZephyrClass: Verona>, <ZephyrClass: Denmark>]
(imported from commit 9998ffe40800213a5425990d6e85f5c5a43a5355)
2012-08-29 16:15:06 +02:00
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
@cache_with_key(lambda self, apply_markdown: 'message_dict:%d:%d' % (self.id, apply_markdown))
|
2012-09-27 22:14:14 +02:00
|
|
|
def to_dict(self, apply_markdown):
|
2012-10-24 20:16:26 +02:00
|
|
|
obj = dict(
|
|
|
|
id = self.id,
|
|
|
|
sender_email = self.sender.user.email,
|
|
|
|
sender_full_name = self.sender.full_name,
|
|
|
|
sender_short_name = self.sender.short_name,
|
|
|
|
type = self.recipient.type_name(),
|
|
|
|
display_recipient = get_display_recipient(self.recipient),
|
|
|
|
recipient_id = self.recipient.id,
|
|
|
|
subject = self.subject,
|
|
|
|
timestamp = calendar.timegm(self.pub_date.timetuple()),
|
|
|
|
gravatar_hash = gravatar_hash(self.sender.user.email))
|
|
|
|
|
2012-09-27 22:14:14 +02:00
|
|
|
if apply_markdown:
|
2012-10-24 20:16:26 +02:00
|
|
|
obj['content'] = bugdown.convert(self.content)
|
2012-10-24 20:51:45 +02:00
|
|
|
obj['content_type'] = 'text/html'
|
2012-10-04 00:13:03 +02:00
|
|
|
else:
|
2012-10-24 20:16:26 +02:00
|
|
|
obj['content'] = self.content
|
2012-10-24 20:51:45 +02:00
|
|
|
obj['content_type'] = 'text/x-markdown'
|
2012-10-24 20:16:26 +02:00
|
|
|
|
|
|
|
return obj
|
2012-08-30 19:56:15 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
def to_log_dict(self):
|
2012-10-24 20:16:26 +02:00
|
|
|
return dict(
|
|
|
|
id = self.id,
|
|
|
|
sender_email = self.sender.user.email,
|
|
|
|
sender_full_name = self.sender.full_name,
|
|
|
|
sender_short_name = self.sender.short_name,
|
|
|
|
sending_client = self.sending_client.name,
|
|
|
|
type = self.recipient.type_name(),
|
|
|
|
recipient = get_log_recipient(self.recipient),
|
|
|
|
subject = self.subject,
|
|
|
|
content = self.content,
|
|
|
|
timestamp = calendar.timegm(self.pub_date.timetuple()))
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-09-07 17:04:41 +02:00
|
|
|
class UserMessage(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
2012-10-03 21:05:48 +02:00
|
|
|
message = models.ForeignKey(Message)
|
2012-09-07 17:04:41 +02:00
|
|
|
# We're not using the archived field for now, but create it anyway
|
|
|
|
# since this table will be an unpleasant one to do schema changes
|
|
|
|
# on later
|
|
|
|
archived = models.BooleanField()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
display_recipient = get_display_recipient(self.message.recipient)
|
2012-09-21 00:26:59 +02:00
|
|
|
return "<UserMessage: %s / %s>" % (display_recipient, self.user_profile.user.email)
|
2012-09-07 17:04:41 +02:00
|
|
|
|
2012-09-14 22:43:54 +02:00
|
|
|
user_hash = {}
|
|
|
|
def get_user_profile_by_id(uid):
|
|
|
|
if uid in user_hash:
|
|
|
|
return user_hash[uid]
|
2012-10-15 18:29:24 +02:00
|
|
|
return UserProfile.objects.select_related().get(id=uid)
|
2012-09-14 22:43:54 +02:00
|
|
|
|
2012-10-20 21:43:13 +02:00
|
|
|
# Store an event in the log for re-importing messages
|
|
|
|
def log_event(event):
|
2012-11-06 19:21:51 +01:00
|
|
|
assert("timestamp" in event)
|
2012-10-03 21:26:40 +02:00
|
|
|
if not os.path.exists(settings.MESSAGE_LOG + '.lock'):
|
|
|
|
file(settings.MESSAGE_LOG + '.lock', "w").write("0")
|
|
|
|
lock = open(settings.MESSAGE_LOG + '.lock', 'r')
|
2012-09-27 19:58:42 +02:00
|
|
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
2012-10-03 21:26:40 +02:00
|
|
|
f = open(settings.MESSAGE_LOG, "a")
|
2012-10-20 21:43:13 +02:00
|
|
|
f.write(simplejson.dumps(event) + "\n")
|
2012-09-27 19:58:42 +02:00
|
|
|
f.flush()
|
|
|
|
f.close()
|
|
|
|
fcntl.flock(lock, fcntl.LOCK_UN)
|
|
|
|
|
2012-10-20 21:43:13 +02:00
|
|
|
def log_message(message):
|
2012-10-23 17:06:22 +02:00
|
|
|
if not message.sending_client.name.startswith("test:"):
|
|
|
|
log_event(message.to_log_dict())
|
2012-10-20 21:43:13 +02:00
|
|
|
|
2012-10-19 21:37:37 +02:00
|
|
|
def do_send_message(message, no_log=False):
|
2012-10-03 21:29:38 +02:00
|
|
|
message.save()
|
2012-10-18 04:35:02 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
# Log the message to our message log for populate_db to refill
|
|
|
|
if not no_log:
|
2012-10-03 21:29:38 +02:00
|
|
|
log_message(message)
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
if message.recipient.type == Recipient.PERSONAL:
|
|
|
|
recipients = list(set([get_user_profile_by_id(message.recipient.type_id),
|
|
|
|
get_user_profile_by_id(message.sender_id)]))
|
|
|
|
# For personals, you send out either 1 or 2 copies of the message, for
|
2012-08-31 16:58:09 +02:00
|
|
|
# personals to yourself or to someone else, respectively.
|
|
|
|
assert((len(recipients) == 1) or (len(recipients) == 2))
|
2012-10-10 22:57:21 +02:00
|
|
|
elif (message.recipient.type == Recipient.STREAM or
|
2012-10-03 21:29:38 +02:00
|
|
|
message.recipient.type == Recipient.HUDDLE):
|
2012-10-22 20:15:25 +02:00
|
|
|
recipients = [s.user_profile for
|
2012-10-15 18:56:03 +02:00
|
|
|
s in Subscription.objects.select_related().filter(recipient=message.recipient, active=True)]
|
2012-08-28 22:56:21 +02:00
|
|
|
else:
|
2012-10-31 16:40:41 +01:00
|
|
|
raise ValueError('Bad recipient type')
|
2012-10-15 18:54:53 +02:00
|
|
|
|
|
|
|
# Save the message receipts in the database
|
2012-10-19 23:40:44 +02:00
|
|
|
# TODO: Use bulk_create here
|
2012-10-15 18:54:53 +02:00
|
|
|
with transaction.commit_on_success():
|
|
|
|
for user_profile in recipients:
|
2012-10-23 22:39:40 +02:00
|
|
|
# Only deliver messages to "active" user accounts
|
|
|
|
if user_profile.user.is_active:
|
|
|
|
UserMessage(user_profile=user_profile, message=message).save()
|
2012-10-15 18:54:53 +02:00
|
|
|
|
2012-10-12 21:27:19 +02:00
|
|
|
# We can only publish messages to longpolling clients if the Tornado server is running.
|
|
|
|
if settings.HAVE_TORNADO_SERVER:
|
|
|
|
requests.post(settings.NOTIFY_NEW_MESSAGE_URL, data=[
|
|
|
|
('secret', settings.SHARED_SECRET),
|
2012-10-17 01:58:20 +02:00
|
|
|
('message', message.id),
|
|
|
|
('users', ','.join(str(user.id) for user in recipients))])
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
class Subscription(models.Model):
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
2012-09-05 21:55:40 +02:00
|
|
|
recipient = models.ForeignKey(Recipient)
|
2012-08-30 18:03:58 +02:00
|
|
|
active = models.BooleanField(default=True)
|
2012-08-29 17:50:36 +02:00
|
|
|
|
2012-11-07 22:33:38 +01:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("user_profile", "recipient")
|
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
def __repr__(self):
|
2012-10-22 20:15:25 +02:00
|
|
|
return "<Subscription: %r -> %r>" % (self.user_profile, self.recipient)
|
2012-09-07 17:04:41 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-10-20 23:04:08 +02:00
|
|
|
def do_add_subscription(user_profile, stream, no_log=False):
|
2012-10-20 21:43:13 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=stream.id,
|
|
|
|
type=Recipient.STREAM)
|
|
|
|
(subscription, created) = Subscription.objects.get_or_create(
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile=user_profile, recipient=recipient,
|
2012-10-20 21:43:13 +02:00
|
|
|
defaults={'active': True})
|
|
|
|
did_subscribe = created
|
|
|
|
if not subscription.active:
|
|
|
|
did_subscribe = True
|
|
|
|
subscription.active = True
|
|
|
|
subscription.save()
|
2012-10-20 23:04:08 +02:00
|
|
|
if did_subscribe and not no_log:
|
2012-10-20 21:43:13 +02:00
|
|
|
log_event({'type': 'subscription_added',
|
|
|
|
'user': user_profile.user.email,
|
|
|
|
'name': stream.name,
|
2012-11-06 19:21:51 +01:00
|
|
|
'timestamp': time.time(),
|
2012-10-20 21:43:13 +02:00
|
|
|
'domain': stream.realm.domain})
|
|
|
|
return did_subscribe
|
|
|
|
|
2012-10-20 23:04:08 +02:00
|
|
|
def do_remove_subscription(user_profile, stream, no_log=False):
|
2012-10-20 21:43:13 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=stream.id,
|
|
|
|
type=Recipient.STREAM)
|
2012-10-22 20:15:25 +02:00
|
|
|
maybe_sub = Subscription.objects.filter(user_profile=user_profile,
|
2012-10-20 23:04:08 +02:00
|
|
|
recipient=recipient)
|
|
|
|
if len(maybe_sub) == 0:
|
|
|
|
return False
|
|
|
|
subscription = maybe_sub[0]
|
|
|
|
did_remove = subscription.active
|
2012-10-20 21:43:13 +02:00
|
|
|
subscription.active = False
|
|
|
|
subscription.save()
|
2012-10-20 23:04:08 +02:00
|
|
|
if did_remove and not no_log:
|
|
|
|
log_event({'type': 'subscription_removed',
|
|
|
|
'user': user_profile.user.email,
|
|
|
|
'name': stream.name,
|
2012-11-06 19:21:51 +01:00
|
|
|
'timestamp': time.time(),
|
2012-10-20 23:04:08 +02:00
|
|
|
'domain': stream.realm.domain})
|
|
|
|
return did_remove
|
2012-10-20 21:43:13 +02:00
|
|
|
|
2012-10-25 20:19:55 +02:00
|
|
|
def do_activate_user(user, log=True):
|
|
|
|
user.is_active = True
|
|
|
|
user.set_password(initial_password(user.email))
|
|
|
|
user.save()
|
|
|
|
if log:
|
|
|
|
log_event({'type': 'user_activated',
|
2012-11-06 19:21:51 +01:00
|
|
|
'timestamp': time.time(),
|
2012-10-25 20:19:55 +02:00
|
|
|
'user': user.email})
|
|
|
|
|
2012-10-25 21:39:34 +02:00
|
|
|
def do_change_password(user, password, log=True):
|
|
|
|
user.set_password(password)
|
|
|
|
user.save()
|
|
|
|
if log:
|
|
|
|
log_event({'type': 'user_change_password',
|
2012-11-06 19:21:51 +01:00
|
|
|
'timestamp': time.time(),
|
2012-10-25 21:39:34 +02:00
|
|
|
'user': user.email,
|
|
|
|
'pwhash': user.password})
|
|
|
|
|
|
|
|
def do_change_full_name(user_profile, full_name, log=True):
|
|
|
|
user_profile.full_name = full_name
|
|
|
|
user_profile.save()
|
|
|
|
if log:
|
|
|
|
log_event({'type': 'user_change_full_name',
|
2012-11-06 19:21:51 +01:00
|
|
|
'timestamp': time.time(),
|
2012-10-25 21:39:34 +02:00
|
|
|
'user': user_profile.user.email,
|
|
|
|
'full_name': full_name})
|
|
|
|
|
2012-09-04 23:20:21 +02:00
|
|
|
class Huddle(models.Model):
|
2012-09-07 20:14:13 +02:00
|
|
|
# TODO: We should consider whether using
|
|
|
|
# CommaSeparatedIntegerField would be better.
|
2012-10-22 19:23:11 +02:00
|
|
|
huddle_hash = models.CharField(max_length=40, db_index=True, unique=True)
|
2012-09-04 23:20:21 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def get_huddle_hash(id_list):
|
2012-09-05 17:38:09 +02:00
|
|
|
id_list = sorted(set(id_list))
|
2012-09-05 17:41:53 +02:00
|
|
|
hash_key = ",".join(str(x) for x in id_list)
|
2012-10-20 18:02:58 +02:00
|
|
|
return hashlib.sha1(hash_key).hexdigest()
|
|
|
|
|
|
|
|
def get_huddle(id_list):
|
|
|
|
huddle_hash = get_huddle_hash(id_list)
|
2012-10-19 23:40:44 +02:00
|
|
|
(huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash)
|
|
|
|
if created:
|
|
|
|
recipient = Recipient.objects.create(type_id=huddle.id,
|
|
|
|
type=Recipient.HUDDLE)
|
2012-09-04 23:20:21 +02:00
|
|
|
# Add subscriptions
|
|
|
|
for uid in id_list:
|
2012-10-19 23:40:44 +02:00
|
|
|
Subscription.objects.create(recipient = recipient,
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile = UserProfile.objects.get(id=uid))
|
2012-10-19 23:40:44 +02:00
|
|
|
return huddle
|
2012-09-04 23:20:21 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
def bulk_create_huddles(users, huddle_user_list):
|
|
|
|
huddles = {}
|
|
|
|
huddles_by_id = {}
|
|
|
|
huddle_set = set()
|
2012-10-31 18:44:44 +01:00
|
|
|
existing_huddles = set()
|
2012-10-20 18:02:58 +02:00
|
|
|
for huddle in Huddle.objects.all():
|
2012-10-31 18:44:44 +01:00
|
|
|
existing_huddles.add(huddle.huddle_hash)
|
2012-10-20 18:02:58 +02:00
|
|
|
for huddle_users in huddle_user_list:
|
|
|
|
user_ids = [users[email].id for email in huddle_users]
|
|
|
|
huddle_hash = get_huddle_hash(user_ids)
|
|
|
|
if huddle_hash in existing_huddles:
|
|
|
|
continue
|
|
|
|
huddle_set.add((huddle_hash, tuple(sorted(user_ids))))
|
|
|
|
|
|
|
|
huddles_to_create = []
|
|
|
|
for (huddle_hash, _) in huddle_set:
|
|
|
|
huddles_to_create.append(Huddle(huddle_hash=huddle_hash))
|
|
|
|
batch_bulk_create(Huddle, huddles_to_create)
|
|
|
|
|
|
|
|
for huddle in Huddle.objects.all():
|
|
|
|
huddles[huddle.huddle_hash] = huddle
|
|
|
|
huddles_by_id[huddle.id] = huddle
|
|
|
|
|
|
|
|
recipients_to_create = []
|
|
|
|
for (huddle_hash, _) in huddle_set:
|
|
|
|
recipients_to_create.append(Recipient(type_id=huddles[huddle_hash].id, type=Recipient.HUDDLE))
|
|
|
|
batch_bulk_create(Recipient, recipients_to_create)
|
|
|
|
|
|
|
|
huddle_recipients = {}
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):
|
|
|
|
huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient
|
|
|
|
|
|
|
|
subscriptions_to_create = []
|
|
|
|
for (huddle_hash, huddle_user_ids) in huddle_set:
|
|
|
|
for user_id in huddle_user_ids:
|
2012-10-22 20:15:25 +02:00
|
|
|
subscriptions_to_create.append(Subscription(active=True, user_profile_id=user_id,
|
2012-10-20 18:02:58 +02:00
|
|
|
recipient=huddle_recipients[huddle_hash]))
|
|
|
|
batch_bulk_create(Subscription, subscriptions_to_create)
|
|
|
|
|
2012-10-31 18:48:11 +01:00
|
|
|
# This function is used only by tests.
|
|
|
|
# We have faster implementations within the app itself.
|
2012-10-03 21:29:38 +02:00
|
|
|
def filter_by_subscriptions(messages, user):
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile = UserProfile.objects.get(user=user)
|
2012-10-03 21:29:38 +02:00
|
|
|
user_messages = []
|
2012-09-07 19:53:24 +02:00
|
|
|
subscriptions = [sub.recipient for sub in
|
2012-10-22 20:15:25 +02:00
|
|
|
Subscription.objects.filter(user_profile=user_profile, active=True)]
|
2012-10-03 21:29:38 +02:00
|
|
|
for message in messages:
|
2012-10-10 22:58:51 +02:00
|
|
|
# If you are subscribed to the personal or stream, or if you
|
2012-10-03 21:29:38 +02:00
|
|
|
# sent the personal, you can see the message.
|
|
|
|
if (message.recipient in subscriptions) or \
|
|
|
|
(message.recipient.type == Recipient.PERSONAL and
|
2012-10-22 20:15:25 +02:00
|
|
|
message.sender == user_profile):
|
2012-10-03 21:29:38 +02:00
|
|
|
user_messages.append(message)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-10-03 21:29:38 +02:00
|
|
|
return user_messages
|
2012-10-29 19:43:00 +01:00
|
|
|
|
|
|
|
def clear_database():
|
|
|
|
for model in [Message, Stream, UserProfile, User, Recipient,
|
|
|
|
Realm, Subscription, Huddle, UserMessage, Client]:
|
|
|
|
model.objects.all().delete()
|
|
|
|
Session.objects.all().delete()
|