2012-08-28 18:44:51 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
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":
|
|
|
|
zephyr_class = ZephyrClass.objects.get(pk=recipient.user_or_class)
|
|
|
|
return zephyr_class.name
|
|
|
|
else:
|
|
|
|
user = User.objects.get(pk=recipient.user_or_class)
|
|
|
|
return user.username
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class UserProfile(models.Model):
|
|
|
|
user = models.OneToOneField(User)
|
|
|
|
pointer = models.IntegerField()
|
|
|
|
|
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):
|
|
|
|
UserProfile(user=u, pointer=-1).save()
|
|
|
|
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):
|
|
|
|
user_or_class = models.IntegerField()
|
|
|
|
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)
|
|
|
|
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.user_or_class, self.type)
|
|
|
|
|
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-29 17:50:36 +02:00
|
|
|
class Subscription(models.Model):
|
|
|
|
userprofile_id = models.ForeignKey(UserProfile)
|
|
|
|
recipient_id = models.ForeignKey(Recipient)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Subscription: %r -> %r>" % (self.userprofile_id, self.recipient_id)
|