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): def __repr__(self):
return "<UserProfile: %s>" % (self.user.username,) 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): class ZephyrClass(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
@ -48,9 +55,9 @@ class Zephyr(models.Model):
display_recipient = get_display_recipient(self.recipient) display_recipient = get_display_recipient(self.recipient)
return "<Zephyr: %s / %s / %r>" % (display_recipient, self.instance, self.sender) return "<Zephyr: %s / %s / %r>" % (display_recipient, self.instance, self.sender)
def create_user_profile(**kwargs): class Subscription(models.Model):
"""When creating a new user, make a profile for him or her.""" userprofile_id = models.ForeignKey(UserProfile)
u = kwargs["instance"] recipient_id = models.ForeignKey(Recipient)
if not UserProfile.objects.filter(user=u):
UserProfile(user=u, pointer=-1).save() def __repr__(self):
post_save.connect(create_user_profile, sender=User) 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.utils.timezone import utc
from django.contrib.auth.models import User 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 from zephyr.forms import RegistrationForm
import datetime import datetime
@ -40,7 +41,7 @@ def home(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect('accounts/home/') return HttpResponseRedirect('accounts/home/')
zephyrs = Zephyr.objects.all() zephyrs = filter_by_subscription(Zephyr.objects.all(), request.user)
for zephyr in zephyrs: for zephyr in zephyrs:
zephyr.display_recipient = get_display_recipient(zephyr.recipient) zephyr.display_recipient = get_display_recipient(zephyr.recipient)
@ -63,12 +64,25 @@ def update(request):
user_profile.save() user_profile.save()
return HttpResponse(simplejson.dumps({}), mimetype='application/json') 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): def get_updates(request):
if not request.POST: if not request.POST:
# Do something # Do something
pass pass
last_received = request.POST.get('last_received') 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 = [] new_zephyr_list = []
for zephyr in new_zephyrs: for zephyr in new_zephyrs:
new_zephyr_list.append({"id": zephyr.id, new_zephyr_list.append({"id": zephyr.id,
@ -90,10 +104,10 @@ def personal_zephyr(request):
# Do something reasonable. # Do something reasonable.
return HttpResponseRedirect(reverse('zephyr.views.home')) return HttpResponseRedirect(reverse('zephyr.views.home'))
recipient = Recipient() # Right now, you can't make recipients on the fly by sending zephyrs to new
recipient.user_or_class = user.pk # classes or people.
recipient.type = "personal" user_profile = UserProfile.objects.get(user=user)
recipient.save() recipient = Recipient.objects.get(user_or_class=user_profile.id, type="personal")
new_zephyr = Zephyr() new_zephyr = Zephyr()
new_zephyr.sender = UserProfile.objects.get(user=request.user) new_zephyr.sender = UserProfile.objects.get(user=request.user)
@ -115,10 +129,9 @@ def zephyr(request):
my_class.name = class_name my_class.name = class_name
my_class.save() my_class.save()
recipient = Recipient() # Right now, you can't make recipients on the fly by sending zephyrs to new
recipient.user_or_class = my_class.pk # classes or people.
recipient.type = "class" recipient = Recipient.objects.get(user_or_class=my_class.id, type="class")
recipient.save()
new_zephyr = Zephyr() new_zephyr = Zephyr()
new_zephyr.sender = UserProfile.objects.get(user=request.user) new_zephyr.sender = UserProfile.objects.get(user=request.user)