annotate forums/views/favorites.py @ 1128:538f35ce3429

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