Only display zephyrs from your subscriptions.

That means your personals, classes to which you are subscribed, and
personals you sent to other people.

(imported from commit 15fbea92b538ff345f9978438c1c5e66766d0a67)
This commit is contained in:
Jessica McKellar 2012-08-29 11:50:36 -04:00
parent cc01ad04bf
commit 1394a685e2
2 changed files with 37 additions and 17 deletions

View File

@ -23,6 +23,13 @@ class UserProfile(models.Model):
def __repr__(self):
return "<UserProfile: %s>" % (self.user.username,)
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)
class ZephyrClass(models.Model):
name = models.CharField(max_length=30)
@ -48,9 +55,9 @@ class Zephyr(models.Model):
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"]
if not UserProfile.objects.filter(user=u):
UserProfile(user=u, pointer=-1).save()
post_save.connect(create_user_profile, sender=User)
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)

View File

@ -8,7 +8,8 @@ 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, get_display_recipient
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \
Recipient, get_display_recipient
from zephyr.forms import RegistrationForm
import datetime
@ -40,7 +41,7 @@ def home(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('accounts/home/')
zephyrs = Zephyr.objects.all()
zephyrs = filter_by_subscription(Zephyr.objects.all(), request.user)
for zephyr in zephyrs:
zephyr.display_recipient = get_display_recipient(zephyr.recipient)
@ -63,12 +64,25 @@ def update(request):
user_profile.save()
return HttpResponse(simplejson.dumps({}), mimetype='application/json')
def filter_by_subscription(zephyrs, user):
userprofile = UserProfile.objects.get(user=user)
subscribed_zephyrs = []
subscriptions = [sub.recipient_id for sub in Subscription.objects.filter(userprofile_id=userprofile)]
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.sender == userprofile) and zephyr.recipient.type == "personal"):
subscribed_zephyrs.append(zephyr)
return subscribed_zephyrs
def get_updates(request):
if not request.POST:
# Do something
pass
last_received = request.POST.get('last_received')
new_zephyrs = Zephyr.objects.filter(id__gt=last_received)
new_zephyrs = filter_by_subscription(Zephyr.objects.filter(id__gt=last_received),
request.user)
new_zephyr_list = []
for zephyr in new_zephyrs:
new_zephyr_list.append({"id": zephyr.id,
@ -90,10 +104,10 @@ def personal_zephyr(request):
# Do something reasonable.
return HttpResponseRedirect(reverse('zephyr.views.home'))
recipient = Recipient()
recipient.user_or_class = user.pk
recipient.type = "personal"
recipient.save()
# Right now, you can't make recipients on the fly by sending zephyrs to new
# classes or people.
user_profile = UserProfile.objects.get(user=user)
recipient = Recipient.objects.get(user_or_class=user_profile.id, type="personal")
new_zephyr = Zephyr()
new_zephyr.sender = UserProfile.objects.get(user=request.user)
@ -115,10 +129,9 @@ def zephyr(request):
my_class.name = class_name
my_class.save()
recipient = Recipient()
recipient.user_or_class = my_class.pk
recipient.type = "class"
recipient.save()
# Right now, you can't make recipients on the fly by sending zephyrs to new
# classes or people.
recipient = Recipient.objects.get(user_or_class=my_class.id, type="class")
new_zephyr = Zephyr()
new_zephyr.sender = UserProfile.objects.get(user=request.user)