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