mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00: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)
This commit is contained in:
@@ -2,17 +2,41 @@ from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_save
|
||||
|
||||
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
|
||||
|
||||
class UserProfile(models.Model):
|
||||
user = models.OneToOneField(User)
|
||||
pointer = models.IntegerField()
|
||||
|
||||
def __repr__(self):
|
||||
return "<UserProfile: %s>" % (self.user.username,)
|
||||
|
||||
class ZephyrClass(models.Model):
|
||||
name = models.CharField(max_length=30)
|
||||
|
||||
def __repr__(self):
|
||||
return "<ZephyrClass: %s>" % (self.name,)
|
||||
|
||||
class Recipient(models.Model):
|
||||
user_or_class = models.IntegerField()
|
||||
type = models.CharField(max_length=30)
|
||||
|
||||
def __repr__(self):
|
||||
display_recipient = get_display_recipient(self)
|
||||
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.user_or_class, self.type)
|
||||
|
||||
class Zephyr(models.Model):
|
||||
sender = models.ForeignKey(UserProfile)
|
||||
recipient = models.ForeignKey(Recipient) # personal or class
|
||||
@@ -20,6 +44,10 @@ class Zephyr(models.Model):
|
||||
content = models.CharField(max_length=200)
|
||||
pub_date = models.DateTimeField('date published')
|
||||
|
||||
def __repr__(self):
|
||||
display_recipient = get_display_recipient(self.recipient)
|
||||
return "<Zephyr: %s / %s / %r>" % (display_recipient, self.instance, self.sender)
|
||||
|
||||
def create_user_profile(**kwargs):
|
||||
"""When creating a new user, make a profile for him or her."""
|
||||
u = kwargs["instance"]
|
||||
|
||||
Reference in New Issue
Block a user