2012-08-28 18:44:51 +02:00
|
|
|
from django.db import models
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
name, for a class, or the username, for a user).
|
|
|
|
"""
|
|
|
|
if recipient.type == "class":
|
2012-09-04 22:22:06 +02:00
|
|
|
zephyr_class = ZephyrClass.objects.get(pk=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
|
|
|
|
else:
|
2012-09-04 22:22:06 +02:00
|
|
|
user = User.objects.get(pk=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 user.username
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
callback_table = {}
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class UserProfile(models.Model):
|
|
|
|
user = models.OneToOneField(User)
|
|
|
|
pointer = models.IntegerField()
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
# This filter should also restrict to the current user's subs
|
|
|
|
new_zephyrs = Zephyr.objects.filter(id__gt=last_received)
|
|
|
|
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):
|
|
|
|
return "<UserProfile: %s>" % (self.user.username,)
|
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
def create_user_profile(**kwargs):
|
|
|
|
"""When creating a new user, make a profile for him or her."""
|
|
|
|
u = kwargs["instance"]
|
|
|
|
if not UserProfile.objects.filter(user=u):
|
2012-08-31 16:58:09 +02:00
|
|
|
profile = UserProfile(user=u, pointer=-1)
|
|
|
|
profile.save()
|
|
|
|
# Auto-sub to the ability to receive personals.
|
2012-09-04 22:22:06 +02:00
|
|
|
recipient = Recipient(type_id=profile.pk, type="personal")
|
2012-08-31 16:58:09 +02:00
|
|
|
recipient.save()
|
|
|
|
Subscription(userprofile_id=profile, recipient_id=recipient).save()
|
2012-08-29 17:50:36 +02:00
|
|
|
post_save.connect(create_user_profile, sender=User)
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class ZephyrClass(models.Model):
|
|
|
|
name = models.CharField(max_length=30)
|
|
|
|
|
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-08-28 21:27:42 +02:00
|
|
|
class Recipient(models.Model):
|
2012-09-04 22:22:06 +02:00
|
|
|
type_id = models.IntegerField()
|
2012-08-28 21:27:42 +02:00
|
|
|
type = models.CharField(max_length=30)
|
|
|
|
|
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-08-28 21:27:42 +02:00
|
|
|
recipient = models.ForeignKey(Recipient) # personal or class
|
2012-08-28 18:44:51 +02:00
|
|
|
instance = models.CharField(max_length=30)
|
|
|
|
content = models.CharField(max_length=200)
|
|
|
|
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-08-30 19:56:15 +02:00
|
|
|
def to_dict(self):
|
|
|
|
return {'id' : self.id,
|
|
|
|
'sender' : self.sender.user.username,
|
|
|
|
'type' : self.recipient.type,
|
|
|
|
'display_recipient': get_display_recipient(self.recipient),
|
|
|
|
'instance' : self.instance,
|
|
|
|
'content' : self.content }
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
def send_zephyr(**kwargs):
|
|
|
|
zephyr = kwargs["instance"]
|
|
|
|
if zephyr.recipient.type == "personal":
|
2012-09-04 22:22:06 +02:00
|
|
|
recipients = UserProfile.objects.filter(Q(user=zephyr.recipient.type_id) | Q(user=zephyr.sender))
|
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-08-28 22:56:21 +02:00
|
|
|
elif zephyr.recipient.type == "class":
|
|
|
|
recipients = [UserProfile.objects.get(user=s.userprofile_id) for
|
2012-08-30 18:04:35 +02:00
|
|
|
s in Subscription.objects.filter(recipient_id=zephyr.recipient, active=True)]
|
2012-08-28 22:56:21 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
for recipient in recipients:
|
|
|
|
recipient.receive(zephyr)
|
|
|
|
|
|
|
|
post_save.connect(send_zephyr, sender=Zephyr)
|
|
|
|
|
2012-08-29 17:50:36 +02:00
|
|
|
class Subscription(models.Model):
|
|
|
|
userprofile_id = models.ForeignKey(UserProfile)
|
|
|
|
recipient_id = 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):
|
|
|
|
return "<Subscription: %r -> %r>" % (self.userprofile_id, self.recipient_id)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
|
|
|
def filter_by_subscriptions(zephyrs, user):
|
|
|
|
userprofile = UserProfile.objects.get(user=user)
|
|
|
|
subscribed_zephyrs = []
|
2012-08-30 23:31:11 +02:00
|
|
|
subscriptions = [sub.recipient_id for sub in Subscription.objects.filter(userprofile_id=userprofile, active=True)]
|
2012-08-28 22:56:21 +02:00
|
|
|
for zephyr in zephyrs:
|
|
|
|
# If you are subscribed to the personal or class, or if you sent the personal, you can see the zephyr.
|
|
|
|
if (zephyr.recipient in subscriptions) or \
|
|
|
|
(zephyr.recipient.type == "personal" and zephyr.sender == userprofile):
|
|
|
|
subscribed_zephyrs.append(zephyr)
|
|
|
|
|
|
|
|
return subscribed_zephyrs
|