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-08-31 16:11:23 +02:00
|
|
|
from django.db.models import Q
|
2012-08-28 18:44:51 +02:00
|
|
|
from django.db.models.signals import post_save
|
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-14 23:28:38 +02:00
|
|
|
import datetime
|
2012-09-19 18:41:42 +02:00
|
|
|
from zephyr.lib.cache import cache_with_key
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-09-21 16:10:36 +02:00
|
|
|
from django.db.models.signals import class_prepared
|
2012-09-26 20:41:54 +02:00
|
|
|
import markdown
|
|
|
|
md_engine = markdown.Markdown(
|
|
|
|
extensions = ['fenced_code', 'codehilite'],
|
|
|
|
output_format = 'xhtml' )
|
2012-09-21 16:10:36 +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 get_display_recipient(recipient):
|
|
|
|
"""
|
|
|
|
recipient: an instance of Recipient.
|
|
|
|
|
|
|
|
returns: an appropriate string describing the recipient (the class
|
2012-09-21 00:26:59 +02:00
|
|
|
name, for a class, 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-09-07 20:14:13 +02:00
|
|
|
if recipient.type == Recipient.CLASS:
|
2012-09-10 19:43:11 +02:00
|
|
|
zephyr_class = ZephyrClass.objects.get(id=recipient.type_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
|
|
|
return zephyr_class.name
|
2012-09-07 20:14:13 +02:00
|
|
|
elif recipient.type == Recipient.HUDDLE:
|
2012-09-26 19:30:01 +02:00
|
|
|
user_profile_list = [UserProfile.objects.get(user=s.userprofile) for s in
|
|
|
|
Subscription.objects.filter(recipient=recipient)]
|
2012-09-26 21:25:49 +02:00
|
|
|
return [{'email': user_profile.user.email,
|
|
|
|
'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-09-10 19:43:11 +02:00
|
|
|
user = User.objects.get(id=recipient.type_id)
|
2012-09-21 00:26:59 +02:00
|
|
|
return user.email
|
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-08-28 22:56:21 +02:00
|
|
|
callback_table = {}
|
2012-09-26 16:44:11 +02:00
|
|
|
mit_sync_table = {}
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-09-05 21:49:56 +02:00
|
|
|
class Realm(models.Model):
|
2012-09-14 23:28:38 +02:00
|
|
|
domain = models.CharField(max_length=40, db_index=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-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-09-05 21:49:56 +02:00
|
|
|
realm = models.ForeignKey(Realm)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
# The user receives this message
|
|
|
|
def receive(self, message):
|
|
|
|
global callback_table
|
|
|
|
|
|
|
|
# Should also store in permanent database the receipt
|
2012-09-07 17:04:41 +02:00
|
|
|
um = UserMessage(user_profile=self, message=message)
|
|
|
|
um.save()
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
for cb in callback_table.get(self.user.id, []):
|
|
|
|
cb([message])
|
|
|
|
|
|
|
|
callback_table[self.user.id] = []
|
|
|
|
|
|
|
|
def add_callback(self, cb, last_received):
|
|
|
|
global callback_table
|
|
|
|
|
2012-09-26 23:43:18 +02:00
|
|
|
new_zephyrs = (Zephyr.objects
|
|
|
|
.filter(usermessage__user_profile = self,
|
|
|
|
id__gt = last_received)
|
|
|
|
.order_by('id')[:400])
|
2012-09-05 16:43:20 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
if new_zephyrs:
|
|
|
|
return cb(new_zephyrs)
|
|
|
|
callback_table.setdefault(self.user.id, []).append(cb)
|
|
|
|
|
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):
|
|
|
|
profile = cls(user=user, pointer=-1, realm_id=realm.id,
|
|
|
|
full_name=full_name, short_name=short_name)
|
|
|
|
profile.save()
|
|
|
|
# Auto-sub to the ability to receive personals.
|
|
|
|
recipient = Recipient(type_id=profile.id, type=Recipient.PERSONAL)
|
|
|
|
recipient.save()
|
|
|
|
Subscription(userprofile=profile, recipient=recipient).save()
|
2012-08-29 17:50:36 +02:00
|
|
|
|
2012-09-21 16:40:46 +02:00
|
|
|
def create_user(email, password, realm, full_name, short_name):
|
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-09-21 16:40:46 +02:00
|
|
|
user = User.objects.create_user(username=username, password=password,
|
|
|
|
email=email)
|
|
|
|
user.save()
|
|
|
|
UserProfile.create(user, realm, full_name, short_name)
|
|
|
|
|
2012-09-27 20:46:42 +02:00
|
|
|
def create_user_if_needed(realm, email, password, full_name, short_name):
|
|
|
|
try:
|
|
|
|
return User.objects.get(email=email)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
# forge a user for this person
|
|
|
|
create_user(email, password, realm,
|
|
|
|
full_name, short_name)
|
|
|
|
user = User.objects.get(email=email)
|
|
|
|
return user
|
|
|
|
|
|
|
|
def create_class_if_needed(realm, class_name):
|
|
|
|
try:
|
|
|
|
return ZephyrClass.objects.get(name=class_name, realm=realm)
|
|
|
|
except ZephyrClass.DoesNotExist:
|
|
|
|
new_class = ZephyrClass()
|
|
|
|
new_class.name = class_name
|
|
|
|
new_class.realm = realm
|
|
|
|
new_class.save()
|
|
|
|
recipient = Recipient(type_id=new_class.id, type=Recipient.CLASS)
|
|
|
|
recipient.save()
|
|
|
|
return new_class
|
|
|
|
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class ZephyrClass(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):
|
|
|
|
return "<ZephyrClass: %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-09-19 18:54:57 +02:00
|
|
|
@classmethod
|
|
|
|
def create(cls, name, realm):
|
|
|
|
zephyr_class = cls(name=name, realm=realm)
|
|
|
|
zephyr_class.save()
|
2012-09-07 19:24:54 +02:00
|
|
|
|
2012-09-19 18:54:57 +02:00
|
|
|
recipient = Recipient(type_id=zephyr_class.id, type=Recipient.CLASS)
|
|
|
|
recipient.save()
|
|
|
|
return (zephyr_class, 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-09-04 23:20:21 +02:00
|
|
|
# Valid types are {personal, class, huddle}
|
2012-09-07 20:14:13 +02:00
|
|
|
PERSONAL = 1
|
|
|
|
CLASS = 2
|
|
|
|
HUDDLE = 3
|
|
|
|
|
|
|
|
def type_name(self):
|
|
|
|
if self.type == self.PERSONAL:
|
|
|
|
return "personal"
|
|
|
|
elif self.type == self.CLASS:
|
|
|
|
return "class"
|
|
|
|
elif self.type == self.HUDDLE:
|
|
|
|
return "huddle"
|
|
|
|
else:
|
|
|
|
raise
|
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-08-28 18:44:51 +02:00
|
|
|
class Zephyr(models.Model):
|
|
|
|
sender = models.ForeignKey(UserProfile)
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient = models.ForeignKey(Recipient)
|
2012-08-28 18:44:51 +02:00
|
|
|
instance = models.CharField(max_length=30)
|
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')
|
|
|
|
|
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)
|
|
|
|
return "<Zephyr: %s / %s / %r>" % (display_recipient, self.instance, 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-09-19 18:41:42 +02:00
|
|
|
@cache_with_key(lambda self: 'zephyr_dict:%d' % (self.id,))
|
2012-08-30 19:56:15 +02:00
|
|
|
def to_dict(self):
|
2012-09-26 23:08:13 +02:00
|
|
|
try:
|
|
|
|
new_content = self.content.decode("utf-8")
|
|
|
|
except:
|
|
|
|
new_content = self.content
|
2012-08-30 19:56:15 +02:00
|
|
|
return {'id' : self.id,
|
2012-09-11 21:20:24 +02:00
|
|
|
'sender_email' : self.sender.user.email,
|
2012-09-11 19:20:01 +02:00
|
|
|
'sender_name' : self.sender.full_name,
|
2012-09-07 20:14:13 +02:00
|
|
|
'type' : self.recipient.type_name(),
|
2012-08-30 19:56:15 +02:00
|
|
|
'display_recipient': get_display_recipient(self.recipient),
|
2012-09-21 22:57:04 +02:00
|
|
|
'recipient_id' : self.recipient.id,
|
2012-08-30 19:56:15 +02:00
|
|
|
'instance' : self.instance,
|
2012-09-26 23:08:13 +02:00
|
|
|
'content' : md_engine.convert(new_content),
|
2012-09-12 22:35:06 +02:00
|
|
|
'timestamp' : calendar.timegm(self.pub_date.timetuple()),
|
2012-09-26 22:34:24 +02:00
|
|
|
'gravatar_hash' : hashlib.md5(self.sender.user.email.lower()).hexdigest(),
|
2012-09-11 00:18:09 +02:00
|
|
|
}
|
2012-08-30 19:56:15 +02:00
|
|
|
|
2012-09-07 17:04:41 +02:00
|
|
|
class UserMessage(models.Model):
|
|
|
|
user_profile = models.ForeignKey(UserProfile)
|
|
|
|
message = models.ForeignKey(Zephyr)
|
|
|
|
# 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]
|
|
|
|
return UserProfile.objects.get(id=uid)
|
|
|
|
|
2012-09-24 20:29:33 +02:00
|
|
|
def do_send_zephyr(zephyr, synced_from_mit=False):
|
2012-09-26 16:44:11 +02:00
|
|
|
mit_sync_table[zephyr.id] = synced_from_mit
|
2012-09-24 20:29:33 +02:00
|
|
|
zephyr.save()
|
2012-09-07 20:14:13 +02:00
|
|
|
if zephyr.recipient.type == Recipient.PERSONAL:
|
2012-09-14 22:43:54 +02:00
|
|
|
recipients = list(set([get_user_profile_by_id(zephyr.recipient.type_id),
|
|
|
|
get_user_profile_by_id(zephyr.sender_id)]))
|
2012-08-31 16:58:09 +02:00
|
|
|
# For personals, you send out either 1 or 2 copies of the zephyr, for
|
|
|
|
# personals to yourself or to someone else, respectively.
|
|
|
|
assert((len(recipients) == 1) or (len(recipients) == 2))
|
2012-09-07 20:14:13 +02:00
|
|
|
elif (zephyr.recipient.type == Recipient.CLASS or
|
|
|
|
zephyr.recipient.type == Recipient.HUDDLE):
|
2012-09-14 22:43:54 +02:00
|
|
|
recipients = [get_user_profile_by_id(s.userprofile_id) for
|
2012-09-05 21:55:40 +02:00
|
|
|
s in Subscription.objects.filter(recipient=zephyr.recipient, active=True)]
|
2012-08-28 22:56:21 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
for recipient in recipients:
|
|
|
|
recipient.receive(zephyr)
|
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
class Subscription(models.Model):
|
2012-09-05 21:55:40 +02:00
|
|
|
userprofile = models.ForeignKey(UserProfile)
|
|
|
|
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
|
|
|
|
|
|
|
def __repr__(self):
|
2012-09-05 21:55:40 +02:00
|
|
|
return "<Subscription: %r -> %r>" % (self.userprofile, 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-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-09-14 23:28:38 +02:00
|
|
|
huddle_hash = models.CharField(max_length=40, db_index=True)
|
2012-09-04 23:20:21 +02:00
|
|
|
|
|
|
|
def get_huddle(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-09-04 23:20:21 +02:00
|
|
|
huddle_hash = hashlib.sha1(hash_key).hexdigest()
|
|
|
|
if Huddle.objects.filter(huddle_hash=huddle_hash):
|
|
|
|
return Huddle.objects.get(huddle_hash=huddle_hash)
|
|
|
|
else:
|
|
|
|
# since we don't have one, make a new huddle
|
|
|
|
huddle = Huddle(huddle_hash = huddle_hash)
|
|
|
|
huddle.save()
|
2012-09-10 19:43:11 +02:00
|
|
|
recipient = Recipient(type_id=huddle.id, type=Recipient.HUDDLE)
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient.save()
|
|
|
|
|
|
|
|
# Add subscriptions
|
|
|
|
for uid in id_list:
|
2012-09-05 21:55:40 +02:00
|
|
|
s = Subscription(recipient = recipient,
|
|
|
|
userprofile = UserProfile.objects.get(id=uid))
|
2012-09-04 23:20:21 +02:00
|
|
|
s.save()
|
|
|
|
return huddle
|
|
|
|
|
2012-09-07 19:54:42 +02:00
|
|
|
# This is currently dead code since all the places where we used to
|
|
|
|
# use it now have faster implementations, but I expect this to be
|
|
|
|
# potentially useful for code in the future, so not deleting it yet.
|
2012-08-28 22:56:21 +02:00
|
|
|
def filter_by_subscriptions(zephyrs, user):
|
|
|
|
userprofile = UserProfile.objects.get(user=user)
|
|
|
|
subscribed_zephyrs = []
|
2012-09-07 19:53:24 +02:00
|
|
|
subscriptions = [sub.recipient for sub in
|
|
|
|
Subscription.objects.filter(userprofile=userprofile, active=True)]
|
2012-08-28 22:56:21 +02:00
|
|
|
for zephyr in zephyrs:
|
2012-09-07 19:53:24 +02:00
|
|
|
# If you are subscribed to the personal or class, or if you
|
|
|
|
# sent the personal, you can see the zephyr.
|
2012-08-28 22:56:21 +02:00
|
|
|
if (zephyr.recipient in subscriptions) or \
|
2012-09-07 20:14:13 +02:00
|
|
|
(zephyr.recipient.type == Recipient.PERSONAL and
|
2012-09-07 19:53:24 +02:00
|
|
|
zephyr.sender == userprofile):
|
2012-08-28 22:56:21 +02:00
|
|
|
subscribed_zephyrs.append(zephyr)
|
|
|
|
|
|
|
|
return subscribed_zephyrs
|