Permalink
Browse files

added onsite subscriptions

  • Loading branch information...
1 parent 8a8031e commit 5ec4feea1da0f3dfa2e58a329132c24ef962cec7 @brosner brosner committed Feb 16, 2011
Showing with 52 additions and 18 deletions.
  1. +19 −8 agora/models.py
  2. +1 −0 agora/urls.py
  3. +32 −10 agora/views.py
View
27 agora/models.py
@@ -140,6 +140,7 @@ def export(self, out=None):
"id": s.id,
"thread": s.thread_id,
"user": s.user_id,
+ "kind": s.kind,
}
for s in t.subscriptions.all()
]
@@ -194,6 +195,7 @@ def restore(cls, in_):
id = subscriber_data["id"],
user_id = subscriber_data["user"],
thread_id = subscriber_data["thread"],
+ kind = subscriber_data["kind"],
)).save()
thread.last_reply_id = thread_data["last_reply"]
thread.save()
@@ -253,7 +255,7 @@ def update_reply_count(self):
self.save()
def update_subscriber_count(self):
- self.subscriber_count = self.subscriptions.count()
+ self.subscriber_count = self.subscriptions.filter(kind="email").count()
self.save()
def new_reply(self, reply):
@@ -263,25 +265,25 @@ def new_reply(self, reply):
self.save()
self.forum.new_reply(reply)
- def subscribe(self, user):
+ def subscribe(self, user, kind):
"""
Subscribes the given user to this thread (handling duplicates)
"""
- ThreadSubscription.objects.get_or_create(thread=self, user=user)
+ ThreadSubscription.objects.get_or_create(thread=self, user=user, kind=kind)
- def unsubscribe(self, user):
+ def unsubscribe(self, user, kind):
try:
- subscription = ThreadSubscription.objects.get(thread=self, user=user)
+ subscription = ThreadSubscription.objects.get(thread=self, user=user, kind=kind)
except ThreadSubscription.DoesNotExist:
return
else:
subscription.delete()
- def subscribed(self, user):
+ def subscribed(self, user, kind):
if user.is_anonymous():
return False
try:
- ThreadSubscription.objects.get(thread=self, user=user)
+ ThreadSubscription.objects.get(thread=self, user=user, kind=kind)
except ThreadSubscription.DoesNotExist:
return False
else:
@@ -327,9 +329,18 @@ class ThreadSubscription(models.Model):
thread = models.ForeignKey(ForumThread, related_name="subscriptions")
user = models.ForeignKey(User, related_name="forum_subscriptions")
+ kind = models.CharField(max_length=15)
class Meta:
- unique_together = [("thread", "user")]
+ unique_together = [("thread", "user", "kind")]
+
+ @classmethod
+ def setup_onsite(cls):
+ for user in User.objects.all():
+ threads = ForumThread.objects.filter(author=user).values_list("pk", flat=True)
+ threads_by_replies = ForumReply.objects.filter(author=user).distinct().values_list("thread", flat=True)
+ for thread in set().union(threads, threads_by_replies):
+ ForumThread.objects.get(pk=thread).subscribe(user, "onsite")
def signal(signals, sender=None):
View
1 agora/urls.py
@@ -11,4 +11,5 @@
url(r"^post_edit/(thread|reply)/(\d+)/$", "agora.views.post_edit", name="agora_post_edit"),
url(r"^subscribe/(\d+)/$", "agora.views.subscribe", name="agora_subscribe"),
url(r"^unsubscribe/(\d+)/$", "agora.views.unsubscribe", name="agora_unsubscribe"),
+ url(r"^thread_updates/$", "agora.views.thread_updates", name="agora_thread_updates"),
)
View
42 agora/views.py
@@ -82,10 +82,10 @@ def forum_thread(request, thread_id):
# subscribe the poster to the thread if requested (default value is True)
if subscribe:
- print "subscribing"
- thread.subscribe(reply.author)
- else:
- print "NOT subscribing"
+ thread.subscribe(reply.author, "email")
+
+ # all users are automatically subscribed to onsite
+ thread.subscribe(reply.author, "onsite")
return HttpResponseRedirect(reverse("agora_thread", args=[thread_id]))
@@ -99,7 +99,7 @@ def forum_thread(request, thread_id):
"thread": thread,
"posts": posts,
"order_type": order_type,
- "subscribed": thread.subscribed(request.user),
+ "subscribed": thread.subscribed(request.user, "email"),
}, context_instance=RequestContext(request))
@@ -121,7 +121,10 @@ def new_post(request, forum_id):
# subscribe the poster to the thread if requested (default value is True)
if subscribe:
- thread.subscribe(thread.author)
+ thread.subscribe(thread.author, "email")
+
+ # all users are automatically subscribed to onsite
+ thread.subscribe(thread.author, "onsite")
return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
@@ -155,7 +158,10 @@ def reply(request, thread_id):
# subscribe the poster to the thread if requested (default value is True)
if subscribe:
- thread.subscribe(reply.author)
+ thread.subscribe(reply.author, "email")
+
+ # all users are automatically subscribed to onsite
+ thread.subscribe(reply.author, "onsite")
return HttpResponseRedirect(reverse("agora_thread", args=[thread_id]))
@@ -165,7 +171,7 @@ def reply(request, thread_id):
"member": member,
"thread_id": thread_id,
"quote_content": quote_content,
- "subscribed": thread.subscribed(request.user),
+ "subscribed": thread.subscribed(request.user, "email"),
"first_reply": first_reply,
}, context_instance=RequestContext(request))
@@ -209,7 +215,7 @@ def subscribe(request, thread_id):
thread = get_object_or_404(ForumThread, pk=thread_id)
if request.method == "POST":
- thread.subscribe(user)
+ thread.subscribe(user, "email")
return HttpResponseRedirect(reverse("agora_thread", args=[thread_id]))
else:
ctx = RequestContext(request, {"thread": thread})
@@ -223,8 +229,24 @@ def unsubscribe(request, thread_id):
thread = get_object_or_404(ForumThread, pk=thread_id)
if request.method == "POST":
- thread.unsubscribe(user)
+ thread.unsubscribe(user, "email")
return HttpResponseRedirect(reverse("agora_thread", args=[thread_id]))
else:
ctx = RequestContext(request, {"thread": thread})
return render_to_response("agora/unsubscribe.html", ctx)
+
+
+@login_required
+def thread_updates(request):
+ subscriptions = ThreadSubscription.objects.filter(user=request.user, kind="onsite")
+ subscriptions = subscriptions.select_related("thread", "user")
+ subscriptions = subscriptions.order_by("-thread__last_modified")
+
+ if request.method == "POST":
+ subscriptions.filter(pk=request.POST["thread_id"]).delete()
+
+ ctx = {
+ "subscriptions": subscriptions,
+ }
+ ctx = RequestContext(request, ctx)
+ return render_to_response("agora/thread_updates.html", ctx)

0 comments on commit 5ec4fee

Please sign in to comment.