bgneal@232: """ bgneal@232: This module contains view functions related to forum favorites (bookmarks). bgneal@232: """ bgneal@232: from django.contrib.auth.decorators import login_required bgneal@232: from django.core.urlresolvers import reverse bgneal@232: from django.views.decorators.http import require_POST bgneal@232: from django.shortcuts import get_object_or_404 bgneal@1032: from django.shortcuts import render bgneal@232: from django.http import HttpResponseRedirect bgneal@232: from django.http import HttpResponseForbidden bgneal@232: from django.http import Http404 bgneal@232: bgneal@662: from django.core.paginator import InvalidPage bgneal@232: from core.paginator import DiggPaginator bgneal@232: from forums.models import Topic bgneal@459: import forums.permissions as perms bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: @require_POST bgneal@232: def favorite_topic(request, topic_id): bgneal@232: """ bgneal@232: This function handles the "favoriting" (bookmarking) of a forum topic by a bgneal@232: user. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) bgneal@459: if perms.can_access(topic.forum.category, request.user): bgneal@232: topic.bookmarkers.add(request.user) bgneal@232: return HttpResponseRedirect( bgneal@232: reverse("forums-favorites_status", args=[topic.id])) bgneal@459: return HttpResponseForbidden() bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def manage_favorites(request): bgneal@232: """Display a user's favorite topics and allow them to be deleted.""" bgneal@232: bgneal@232: user = request.user bgneal@232: if request.method == "POST": bgneal@232: if request.POST.get('delete_all'): bgneal@232: user.favorite_topics.clear() bgneal@232: else: bgneal@232: delete_ids = request.POST.getlist('delete_ids') bgneal@232: try: bgneal@232: delete_ids = [int(id) for id in delete_ids] bgneal@232: except ValueError: bgneal@232: raise Http404 bgneal@232: for topic in user.favorite_topics.filter(id__in=delete_ids): bgneal@232: user.favorite_topics.remove(topic) bgneal@232: bgneal@383: return HttpResponseRedirect(reverse("forums-manage_favorites")) bgneal@232: bgneal@383: page_num = request.GET.get('page', 1) bgneal@232: topics = user.favorite_topics.select_related().order_by('-update_date') bgneal@232: paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2) bgneal@232: try: bgneal@232: page_num = int(page_num) bgneal@232: except ValueError: bgneal@232: page_num = 1 bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: bgneal@1032: return render(request, 'forums/manage_topics.html', { bgneal@232: 'page_title': 'Favorite Topics', bgneal@232: 'description': 'Your favorite topics are listed below.', bgneal@232: 'page': page, bgneal@1032: }) bgneal@1032: bgneal@232: bgneal@232: @login_required bgneal@232: def favorites_status(request, topic_id): bgneal@232: """Display the favorite status for the given topic.""" bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), id=topic_id) bgneal@232: is_favorite = request.user in topic.bookmarkers.all() bgneal@1032: return render(request, 'forums/favorite_status.html', { bgneal@232: 'topic': topic, bgneal@232: 'is_favorite': is_favorite, bgneal@1032: }) bgneal@1032: bgneal@232: bgneal@232: @login_required bgneal@232: @require_POST bgneal@232: def unfavorite_topic(request, topic_id): bgneal@232: """ bgneal@232: Un-favorite the user from the requested topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic, id=topic_id) bgneal@232: topic.bookmarkers.remove(request.user) bgneal@232: return HttpResponseRedirect( bgneal@232: reverse("forums-favorites_status", args=[topic.id]))