bgneal@181: """This module handles the subscriptions of users to forum topics.""" bgneal@181: from django.conf import settings bgneal@181: from django.contrib.auth.decorators import login_required bgneal@181: from django.contrib.sites.models import Site bgneal@181: from django.core.paginator import InvalidPage bgneal@181: from django.core.urlresolvers import reverse bgneal@181: from django.http import HttpResponseRedirect bgneal@181: from django.http import Http404 bgneal@181: from django.template.loader import render_to_string bgneal@181: from django.shortcuts import get_object_or_404 bgneal@181: from django.shortcuts import render_to_response bgneal@181: from django.template import RequestContext bgneal@181: from django.views.decorators.http import require_POST bgneal@181: bgneal@181: from forums.models import Topic bgneal@181: from core.functions import send_mail bgneal@181: from core.paginator import DiggPaginator bgneal@181: bgneal@181: bgneal@181: def notify_topic_subscribers(post): bgneal@181: """The argument post is a newly created post. Send out an email bgneal@181: notification to all subscribers of the post's parent Topic.""" bgneal@181: bgneal@181: topic = post.topic bgneal@181: recipients = topic.subscribers.exclude( bgneal@181: id=post.user.id).values_list('email', flat=True) bgneal@181: bgneal@181: if recipients: bgneal@181: site = Site.objects.get_current() bgneal@181: subject = "[%s] Topic Reply: %s" % (site.name, topic.name) bgneal@181: url_prefix = "http://%s" % site.domain bgneal@181: post_url = url_prefix + post.get_absolute_url() bgneal@181: unsubscribe_url = url_prefix + reverse("forums-manage_subscriptions") bgneal@181: msg = render_to_string("forums/topic_notify_email.txt", { bgneal@181: 'poster': post.user.username, bgneal@181: 'topic_name': topic.name, bgneal@181: 'message': post.body, bgneal@181: 'post_url': post_url, bgneal@181: 'unsubscribe_url': unsubscribe_url, bgneal@181: }) bgneal@181: for recipient in recipients: bgneal@181: send_mail(subject, msg, settings.DEFAULT_FROM_EMAIL, [recipient]) bgneal@181: bgneal@181: bgneal@181: @login_required bgneal@181: @require_POST bgneal@181: def subscribe_topic(request, topic_id): bgneal@181: """Subscribe the user to the requested topic.""" bgneal@181: topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) bgneal@181: if topic.forum.category.can_access(request.user): bgneal@181: topic.subscribers.add(request.user) bgneal@181: return HttpResponseRedirect( bgneal@181: reverse("forums-subscription_status", args=[topic.id])) bgneal@181: raise Http404 bgneal@181: bgneal@181: bgneal@181: @login_required bgneal@181: @require_POST bgneal@181: def unsubscribe_topic(request, topic_id): bgneal@181: """Unsubscribe the user to the requested topic.""" bgneal@181: topic = get_object_or_404(Topic, id=topic_id) bgneal@181: topic.subscribers.remove(request.user) bgneal@181: return HttpResponseRedirect( bgneal@181: reverse("forums-subscription_status", args=[topic.id])) bgneal@181: bgneal@181: bgneal@181: @login_required bgneal@181: def subscription_status(request, topic_id): bgneal@181: """Display the subscription status for the given topic.""" bgneal@181: topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) bgneal@181: is_subscribed = request.user in topic.subscribers.all() bgneal@181: return render_to_response('forums/subscription_status.html', { bgneal@181: 'topic': topic, bgneal@181: 'is_subscribed': is_subscribed, bgneal@181: }, bgneal@181: context_instance=RequestContext(request)) bgneal@181: bgneal@181: bgneal@181: @login_required bgneal@181: def manage_subscriptions(request): bgneal@181: """Display a user's topic subscriptions, and allow them to be deleted.""" bgneal@181: bgneal@181: user = request.user bgneal@181: if request.method == "POST": bgneal@181: if request.POST.get('delete_all'): bgneal@181: user.subscriptions.clear() bgneal@181: else: bgneal@181: delete_ids = request.POST.getlist('delete_ids') bgneal@181: try: bgneal@181: delete_ids = [int(id) for id in delete_ids] bgneal@181: except ValueError: bgneal@181: raise Http404 bgneal@181: for topic in user.subscriptions.filter(id__in=delete_ids): bgneal@181: user.subscriptions.remove(topic) bgneal@181: bgneal@181: page_num = request.POST.get('page', 1) bgneal@181: else: bgneal@181: page_num = request.GET.get('page', 1) bgneal@181: bgneal@181: topics = user.subscriptions.select_related().order_by('-creation_date') bgneal@181: paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2) bgneal@181: try: bgneal@181: page_num = int(page_num) bgneal@181: except ValueError: bgneal@181: page_num = 1 bgneal@181: try: bgneal@181: page = paginator.page(page_num) bgneal@181: except InvalidPage: bgneal@181: raise Http404 bgneal@181: bgneal@181: return render_to_response('forums/manage_subscriptions.html', { bgneal@181: 'page': page, bgneal@181: }, bgneal@181: context_instance=RequestContext(request))