Mercurial > public > sg101
comparison forums/views/subscriptions.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/forums/views/subscriptions.py@82b97697312e |
children | 4aadaf3bc234 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
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 import forums.permissions as perms | |
17 from core.functions import send_mail | |
18 from core.paginator import DiggPaginator | |
19 | |
20 | |
21 def notify_topic_subscribers(post, defer=True): | |
22 """ | |
23 The argument post is a newly created post. Send out an email | |
24 notification to all subscribers of the post's parent Topic. | |
25 | |
26 The defer flag is passed to core.functions.send_mail. If True, the mail is | |
27 sent on a Celery task. If False, the mail is sent on the caller's thread. | |
28 """ | |
29 topic = post.topic | |
30 recipients = topic.subscribers.exclude(id=post.user.id).values_list( | |
31 'email', flat=True) | |
32 | |
33 if recipients: | |
34 site = Site.objects.get_current() | |
35 subject = "[%s] Topic Reply: %s" % (site.name, topic.name) | |
36 url_prefix = "http://%s" % site.domain | |
37 post_url = url_prefix + post.get_absolute_url() | |
38 unsubscribe_url = url_prefix + reverse("forums-manage_subscriptions") | |
39 msg = render_to_string("forums/topic_notify_email.txt", { | |
40 'poster': post.user.username, | |
41 'topic_name': topic.name, | |
42 'message': post.body, | |
43 'post_url': post_url, | |
44 'unsubscribe_url': unsubscribe_url, | |
45 }) | |
46 for recipient in recipients: | |
47 send_mail(subject, msg, settings.DEFAULT_FROM_EMAIL, [recipient], | |
48 defer=defer) | |
49 | |
50 | |
51 @login_required | |
52 @require_POST | |
53 def subscribe_topic(request, topic_id): | |
54 """Subscribe the user to the requested topic.""" | |
55 topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) | |
56 if perms.can_access(topic.forum.category, request.user): | |
57 topic.subscribers.add(request.user) | |
58 return HttpResponseRedirect( | |
59 reverse("forums-subscription_status", args=[topic.id])) | |
60 raise Http404 | |
61 | |
62 | |
63 @login_required | |
64 @require_POST | |
65 def unsubscribe_topic(request, topic_id): | |
66 """Unsubscribe the user to the requested topic.""" | |
67 topic = get_object_or_404(Topic, id=topic_id) | |
68 topic.subscribers.remove(request.user) | |
69 return HttpResponseRedirect( | |
70 reverse("forums-subscription_status", args=[topic.id])) | |
71 | |
72 | |
73 @login_required | |
74 def subscription_status(request, topic_id): | |
75 """Display the subscription status for the given topic.""" | |
76 topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) | |
77 is_subscribed = request.user in topic.subscribers.all() | |
78 return render_to_response('forums/subscription_status.html', { | |
79 'topic': topic, | |
80 'is_subscribed': is_subscribed, | |
81 }, | |
82 context_instance=RequestContext(request)) | |
83 | |
84 | |
85 @login_required | |
86 def manage_subscriptions(request): | |
87 """Display a user's topic subscriptions, and allow them to be deleted.""" | |
88 | |
89 user = request.user | |
90 if request.method == "POST": | |
91 if request.POST.get('delete_all'): | |
92 user.subscriptions.clear() | |
93 else: | |
94 delete_ids = request.POST.getlist('delete_ids') | |
95 try: | |
96 delete_ids = [int(id) for id in delete_ids] | |
97 except ValueError: | |
98 raise Http404 | |
99 | |
100 for topic in user.subscriptions.filter(id__in=delete_ids): | |
101 user.subscriptions.remove(topic) | |
102 | |
103 return HttpResponseRedirect(reverse("forums-manage_subscriptions")) | |
104 | |
105 page_num = request.GET.get('page', 1) | |
106 topics = user.subscriptions.select_related().order_by('-update_date') | |
107 paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2) | |
108 try: | |
109 page_num = int(page_num) | |
110 except ValueError: | |
111 page_num = 1 | |
112 try: | |
113 page = paginator.page(page_num) | |
114 except InvalidPage: | |
115 raise Http404 | |
116 | |
117 return render_to_response('forums/manage_topics.html', { | |
118 'page_title': 'Topic Subscriptions', | |
119 'description': 'The forum topics you are currently subscribed to are listed below.', | |
120 'page': page, | |
121 }, | |
122 context_instance=RequestContext(request)) |