annotate forums/views/favorites.py @ 916:1cb227d59e20

Potd app refactor. For Django 1.7.7 upgrade.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2015 19:36:38 -0500
parents b347a31d12dd
children e932f2ecd4a7
rev   line source
bgneal@232 1 """
bgneal@232 2 This module contains view functions related to forum favorites (bookmarks).
bgneal@232 3 """
bgneal@232 4 from django.contrib.auth.decorators import login_required
bgneal@232 5 from django.core.urlresolvers import reverse
bgneal@232 6 from django.views.decorators.http import require_POST
bgneal@232 7 from django.shortcuts import get_object_or_404
bgneal@232 8 from django.shortcuts import render_to_response
bgneal@232 9 from django.template import RequestContext
bgneal@232 10 from django.http import HttpResponseRedirect
bgneal@232 11 from django.http import HttpResponseForbidden
bgneal@232 12 from django.http import Http404
bgneal@232 13
bgneal@662 14 from django.core.paginator import InvalidPage
bgneal@232 15 from core.paginator import DiggPaginator
bgneal@232 16 from forums.models import Topic
bgneal@459 17 import forums.permissions as perms
bgneal@232 18
bgneal@232 19
bgneal@232 20 @login_required
bgneal@232 21 @require_POST
bgneal@232 22 def favorite_topic(request, topic_id):
bgneal@232 23 """
bgneal@232 24 This function handles the "favoriting" (bookmarking) of a forum topic by a
bgneal@232 25 user.
bgneal@232 26 """
bgneal@232 27 topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
bgneal@459 28 if perms.can_access(topic.forum.category, request.user):
bgneal@232 29 topic.bookmarkers.add(request.user)
bgneal@232 30 return HttpResponseRedirect(
bgneal@232 31 reverse("forums-favorites_status", args=[topic.id]))
bgneal@459 32 return HttpResponseForbidden()
bgneal@232 33
bgneal@232 34
bgneal@232 35 @login_required
bgneal@232 36 def manage_favorites(request):
bgneal@232 37 """Display a user's favorite topics and allow them to be deleted."""
bgneal@232 38
bgneal@232 39 user = request.user
bgneal@232 40 if request.method == "POST":
bgneal@232 41 if request.POST.get('delete_all'):
bgneal@232 42 user.favorite_topics.clear()
bgneal@232 43 else:
bgneal@232 44 delete_ids = request.POST.getlist('delete_ids')
bgneal@232 45 try:
bgneal@232 46 delete_ids = [int(id) for id in delete_ids]
bgneal@232 47 except ValueError:
bgneal@232 48 raise Http404
bgneal@232 49 for topic in user.favorite_topics.filter(id__in=delete_ids):
bgneal@232 50 user.favorite_topics.remove(topic)
bgneal@232 51
bgneal@383 52 return HttpResponseRedirect(reverse("forums-manage_favorites"))
bgneal@232 53
bgneal@383 54 page_num = request.GET.get('page', 1)
bgneal@232 55 topics = user.favorite_topics.select_related().order_by('-update_date')
bgneal@232 56 paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2)
bgneal@232 57 try:
bgneal@232 58 page_num = int(page_num)
bgneal@232 59 except ValueError:
bgneal@232 60 page_num = 1
bgneal@232 61 try:
bgneal@232 62 page = paginator.page(page_num)
bgneal@232 63 except InvalidPage:
bgneal@232 64 raise Http404
bgneal@232 65
bgneal@232 66 return render_to_response('forums/manage_topics.html', {
bgneal@232 67 'page_title': 'Favorite Topics',
bgneal@232 68 'description': 'Your favorite topics are listed below.',
bgneal@232 69 'page': page,
bgneal@232 70 },
bgneal@232 71 context_instance=RequestContext(request))
bgneal@232 72
bgneal@232 73 @login_required
bgneal@232 74 def favorites_status(request, topic_id):
bgneal@232 75 """Display the favorite status for the given topic."""
bgneal@232 76 topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
bgneal@232 77 is_favorite = request.user in topic.bookmarkers.all()
bgneal@232 78 return render_to_response('forums/favorite_status.html', {
bgneal@232 79 'topic': topic,
bgneal@232 80 'is_favorite': is_favorite,
bgneal@232 81 },
bgneal@232 82 context_instance=RequestContext(request))
bgneal@232 83
bgneal@232 84 @login_required
bgneal@232 85 @require_POST
bgneal@232 86 def unfavorite_topic(request, topic_id):
bgneal@232 87 """
bgneal@232 88 Un-favorite the user from the requested topic.
bgneal@232 89 """
bgneal@232 90 topic = get_object_or_404(Topic, id=topic_id)
bgneal@232 91 topic.bookmarkers.remove(request.user)
bgneal@232 92 return HttpResponseRedirect(
bgneal@232 93 reverse("forums-favorites_status", args=[topic.id]))