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