mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +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"]
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ from django.shortcuts import render
 | 
			
		||||
from django.utils.timezone import utc
 | 
			
		||||
 | 
			
		||||
from django.contrib.auth.models import User
 | 
			
		||||
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient
 | 
			
		||||
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, get_display_recipient
 | 
			
		||||
from zephyr.forms import RegistrationForm
 | 
			
		||||
 | 
			
		||||
import datetime
 | 
			
		||||
@@ -42,12 +42,7 @@ def home(request):
 | 
			
		||||
 | 
			
		||||
    zephyrs = Zephyr.objects.all()
 | 
			
		||||
    for zephyr in zephyrs:
 | 
			
		||||
        if zephyr.recipient.type == "class":
 | 
			
		||||
            zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
 | 
			
		||||
            zephyr.display_recipient = zephyr_class.name
 | 
			
		||||
        else:
 | 
			
		||||
            user = User.objects.get(pk=zephyr.recipient.user_or_class)
 | 
			
		||||
            zephyr.display_recipient = user.username
 | 
			
		||||
        zephyr.display_recipient = get_display_recipient(zephyr.recipient)
 | 
			
		||||
 | 
			
		||||
    user = request.user
 | 
			
		||||
    user_profile = UserProfile.objects.get(user=user)
 | 
			
		||||
@@ -76,16 +71,9 @@ def get_updates(request):
 | 
			
		||||
    new_zephyrs = Zephyr.objects.filter(id__gt=last_received)
 | 
			
		||||
    new_zephyr_list = []
 | 
			
		||||
    for zephyr in new_zephyrs:
 | 
			
		||||
        if zephyr.recipient.type == "class":
 | 
			
		||||
            zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
 | 
			
		||||
            display_recipient = zephyr_class.name
 | 
			
		||||
        else:
 | 
			
		||||
            user = User.objects.get(pk=zephyr.recipient.user_or_class)
 | 
			
		||||
            display_recipient = user.username
 | 
			
		||||
 | 
			
		||||
        new_zephyr_list.append({"id": zephyr.id,
 | 
			
		||||
                                "sender": zephyr.sender.user.username,
 | 
			
		||||
                                "display_recipient": display_recipient,
 | 
			
		||||
                                "display_recipient": get_display_recipient(zephyr.recipient),
 | 
			
		||||
                                "instance": zephyr.instance,
 | 
			
		||||
                                "content": zephyr.content
 | 
			
		||||
                                })
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user