Mercurial > public > sg101
comparison gpp/forums/views.py @ 167:cf9f9d4c4d54
Adding a query to the forums to get all the topics with unread posts. This is for ticket #54.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 24 Jan 2010 22:33:11 +0000 |
parents | 445e1466a98d |
children | e6d4dfdfbc64 |
comparison
equal
deleted
inserted
replaced
166:8acf5be27f18 | 167:cf9f9d4c4d54 |
---|---|
23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ | 23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ |
24 ForumLastVisit | 24 ForumLastVisit |
25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ | 25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ |
26 SplitTopicForm | 26 SplitTopicForm |
27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ | 27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ |
28 get_post_unread_status | 28 get_post_unread_status, get_unread_topics |
29 | 29 |
30 from bio.models import UserProfile | 30 from bio.models import UserProfile |
31 ####################################################################### | 31 ####################################################################### |
32 | 32 |
33 TOPICS_PER_PAGE = 50 | 33 TOPICS_PER_PAGE = 50 |
34 POSTS_PER_PAGE = 20 | 34 POSTS_PER_PAGE = 20 |
35 | 35 |
36 | |
37 def get_page_num(request): | |
38 """Returns the value of the 'page' variable in GET if it exists, or 1 | |
39 if it does not.""" | |
40 | |
41 try: | |
42 page_num = int(request.GET.get('page', 1)) | |
43 except ValueError: | |
44 page_num = 1 | |
45 | |
46 return page_num | |
47 | |
48 | |
36 def create_topic_paginator(topics): | 49 def create_topic_paginator(topics): |
37 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2) | 50 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2) |
38 | 51 |
39 def create_post_paginator(posts): | 52 def create_post_paginator(posts): |
40 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) | 53 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) |
41 | 54 |
55 | |
56 def attach_topic_page_ranges(topics): | |
57 """Attaches a page_range attribute to each topic in the supplied list. | |
58 This attribute will be None if it is a single page topic. This is used | |
59 by the templates to generate "goto page x" links. | |
60 """ | |
61 for topic in topics: | |
62 if topic.post_count > POSTS_PER_PAGE: | |
63 pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE, | |
64 body=2, tail=3, margin=1) | |
65 topic.page_range = pp.page(1).page_range | |
66 else: | |
67 topic.page_range = None | |
68 | |
42 ####################################################################### | 69 ####################################################################### |
43 | 70 |
44 def index(request): | 71 def index(request): |
45 """ | 72 """ |
46 This view displays all the forums available, ordered in each category. | 73 This view displays all the forums available, ordered in each category. |
47 """ | 74 """ |
75 # check for special forum queries | |
76 query = request.GET.get("query") | |
77 if query == "unread": | |
78 return HttpResponseRedirect(reverse('forums-unread_topics')) | |
79 | |
48 forums = Forum.objects.forums_for_user(request.user) | 80 forums = Forum.objects.forums_for_user(request.user) |
49 get_forum_unread_status(forums, request.user) | 81 get_forum_unread_status(forums, request.user) |
50 cats = {} | 82 cats = {} |
51 for forum in forums: | 83 for forum in forums: |
52 cat = cats.setdefault(forum.category.id, { | 84 cat = cats.setdefault(forum.category.id, { |
75 | 107 |
76 topics = forum.topics.select_related('user', 'last_post', 'last_post__user') | 108 topics = forum.topics.select_related('user', 'last_post', 'last_post__user') |
77 get_topic_unread_status(forum, topics, request.user) | 109 get_topic_unread_status(forum, topics, request.user) |
78 | 110 |
79 paginator = create_topic_paginator(topics) | 111 paginator = create_topic_paginator(topics) |
80 page_num = int(request.GET.get('page', 1)) | 112 page_num = get_page_num(request) |
81 try: | 113 try: |
82 page = paginator.page(page_num) | 114 page = paginator.page(page_num) |
83 except InvalidPage: | 115 except InvalidPage: |
84 raise Http404 | 116 raise Http404 |
85 | 117 |
86 # Attach "goto page" navigation links onto those topics that have | 118 attach_topic_page_ranges(page.object_list) |
87 # more than 1 page: | |
88 | |
89 for topic in page.object_list: | |
90 if topic.post_count > POSTS_PER_PAGE: | |
91 pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE, | |
92 body=2, tail=3, margin=1) | |
93 topic.page_range = pp.page(1).page_range | |
94 else: | |
95 topic.page_range = None | |
96 | |
97 | 119 |
98 # we do this for the template since it is rendered twice | 120 # we do this for the template since it is rendered twice |
99 page_nav = render_to_string('forums/pagination.html', {'page': page}) | 121 page_nav = render_to_string('forums/pagination.html', {'page': page}) |
100 | 122 |
101 can_moderate = _can_moderate(forum, request.user) | 123 can_moderate = _can_moderate(forum, request.user) |
122 topic.save() | 144 topic.save() |
123 | 145 |
124 posts = topic.posts.select_related() | 146 posts = topic.posts.select_related() |
125 | 147 |
126 paginator = create_post_paginator(posts) | 148 paginator = create_post_paginator(posts) |
127 page_num = int(request.GET.get('page', 1)) | 149 page_num = get_page_num(request) |
128 try: | 150 try: |
129 page = paginator.page(page_num) | 151 page = paginator.page(page_num) |
130 except InvalidPage: | 152 except InvalidPage: |
131 raise Http404 | 153 raise Http404 |
132 get_post_unread_status(topic, page.object_list, request.user) | 154 get_post_unread_status(topic, page.object_list, request.user) |
523 if not _can_moderate(forum, request.user): | 545 if not _can_moderate(forum, request.user): |
524 return HttpResponseForbidden() | 546 return HttpResponseForbidden() |
525 | 547 |
526 topics = forum.topics.select_related('user', 'last_post', 'last_post__user') | 548 topics = forum.topics.select_related('user', 'last_post', 'last_post__user') |
527 paginator = create_topic_paginator(topics) | 549 paginator = create_topic_paginator(topics) |
528 page_num = int(request.REQUEST.get('page', 1)) | 550 page_num = get_page_num(request) |
529 try: | 551 try: |
530 page = paginator.page(page_num) | 552 page = paginator.page(page_num) |
531 except InvalidPage: | 553 except InvalidPage: |
532 raise Http404 | 554 raise Http404 |
533 | 555 |
617 'form': form, | 639 'form': form, |
618 }, | 640 }, |
619 context_instance=RequestContext(request)) | 641 context_instance=RequestContext(request)) |
620 | 642 |
621 | 643 |
644 @login_required | |
645 def unread_topics(request): | |
646 topics = get_unread_topics(request.user) | |
647 | |
648 paginator = create_topic_paginator(topics) | |
649 page_num = get_page_num(request) | |
650 try: | |
651 page = paginator.page(page_num) | |
652 except InvalidPage: | |
653 raise Http404 | |
654 | |
655 attach_topic_page_ranges(page.object_list) | |
656 | |
657 # we do this for the template since it is rendered twice | |
658 page_nav = render_to_string('forums/pagination.html', {'page': page}) | |
659 | |
660 return render_to_response('forums/topic_list.html', { | |
661 'title': 'Topics With Unread Posts', | |
662 'page': page, | |
663 'page_nav': page_nav, | |
664 }, | |
665 context_instance=RequestContext(request)) | |
666 | |
667 | |
622 def _can_moderate(forum, user): | 668 def _can_moderate(forum, user): |
623 """ | 669 """ |
624 Determines if a user has permission to moderate a given forum. | 670 Determines if a user has permission to moderate a given forum. |
625 """ | 671 """ |
626 return user.is_authenticated() and ( | 672 return user.is_authenticated() and ( |