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