Mercurial > public > sg101
comparison gpp/forums/subscriptions.py @ 181:500e5875a306
Implementing #61: adding a forum topic subscription feature.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 28 Mar 2010 01:07:47 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
180:aef00df91165 | 181:500e5875a306 |
---|---|
1 """This module handles the subscriptions of users to forum topics.""" | |
2 from django.conf import settings | |
3 from django.contrib.auth.decorators import login_required | |
4 from django.contrib.sites.models import Site | |
5 from django.core.paginator import InvalidPage | |
6 from django.core.urlresolvers import reverse | |
7 from django.http import HttpResponseRedirect | |
8 from django.http import Http404 | |
9 from django.template.loader import render_to_string | |
10 from django.shortcuts import get_object_or_404 | |
11 from django.shortcuts import render_to_response | |
12 from django.template import RequestContext | |
13 from django.views.decorators.http import require_POST | |
14 | |
15 from forums.models import Topic | |
16 from core.functions import send_mail | |
17 from core.paginator import DiggPaginator | |
18 | |
19 | |
20 def notify_topic_subscribers(post): | |
21 """The argument post is a newly created post. Send out an email | |
22 notification to all subscribers of the post's parent Topic.""" | |
23 | |
24 topic = post.topic | |
25 recipients = topic.subscribers.exclude( | |
26 id=post.user.id).values_list('email', flat=True) | |
27 | |
28 if recipients: | |
29 site = Site.objects.get_current() | |
30 subject = "[%s] Topic Reply: %s" % (site.name, topic.name) | |
31 url_prefix = "http://%s" % site.domain | |
32 post_url = url_prefix + post.get_absolute_url() | |
33 unsubscribe_url = url_prefix + reverse("forums-manage_subscriptions") | |
34 msg = render_to_string("forums/topic_notify_email.txt", { | |
35 'poster': post.user.username, | |
36 'topic_name': topic.name, | |
37 'message': post.body, | |
38 'post_url': post_url, | |
39 'unsubscribe_url': unsubscribe_url, | |
40 }) | |
41 for recipient in recipients: | |
42 send_mail(subject, msg, settings.DEFAULT_FROM_EMAIL, [recipient]) | |
43 | |
44 | |
45 @login_required | |
46 @require_POST | |
47 def subscribe_topic(request, topic_id): | |
48 """Subscribe the user to the requested topic.""" | |
49 topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) | |
50 if topic.forum.category.can_access(request.user): | |
51 topic.subscribers.add(request.user) | |
52 return HttpResponseRedirect( | |
53 reverse("forums-subscription_status", args=[topic.id])) | |
54 raise Http404 | |
55 | |
56 | |
57 @login_required | |
58 @require_POST | |
59 def unsubscribe_topic(request, topic_id): | |
60 """Unsubscribe the user to the requested topic.""" | |
61 topic = get_object_or_404(Topic, id=topic_id) | |
62 topic.subscribers.remove(request.user) | |
63 return HttpResponseRedirect( | |
64 reverse("forums-subscription_status", args=[topic.id])) | |
65 | |
66 | |
67 @login_required | |
68 def subscription_status(request, topic_id): | |
69 """Display the subscription status for the given topic.""" | |
70 topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) | |
71 is_subscribed = request.user in topic.subscribers.all() | |
72 return render_to_response('forums/subscription_status.html', { | |
73 'topic': topic, | |
74 'is_subscribed': is_subscribed, | |
75 }, | |
76 context_instance=RequestContext(request)) | |
77 | |
78 | |
79 @login_required | |
80 def manage_subscriptions(request): | |
81 """Display a user's topic subscriptions, and allow them to be deleted.""" | |
82 | |
83 user = request.user | |
84 if request.method == "POST": | |
85 if request.POST.get('delete_all'): | |
86 user.subscriptions.clear() | |
87 else: | |
88 delete_ids = request.POST.getlist('delete_ids') | |
89 try: | |
90 delete_ids = [int(id) for id in delete_ids] | |
91 except ValueError: | |
92 raise Http404 | |
93 for topic in user.subscriptions.filter(id__in=delete_ids): | |
94 user.subscriptions.remove(topic) | |
95 | |
96 page_num = request.POST.get('page', 1) | |
97 else: | |
98 page_num = request.GET.get('page', 1) | |
99 | |
100 topics = user.subscriptions.select_related().order_by('-creation_date') | |
101 paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2) | |
102 try: | |
103 page_num = int(page_num) | |
104 except ValueError: | |
105 page_num = 1 | |
106 try: | |
107 page = paginator.page(page_num) | |
108 except InvalidPage: | |
109 raise Http404 | |
110 | |
111 return render_to_response('forums/manage_subscriptions.html', { | |
112 'page': page, | |
113 }, | |
114 context_instance=RequestContext(request)) |