Mercurial > public > sg101
comparison gpp/forums/views.py @ 93:4c33e266db03
Forums: paginate the topic list inside a forum.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 13 Sep 2009 04:33:15 +0000 |
parents | 62af8cd8f57b |
children | 96eec1ed0fd3 |
comparison
equal
deleted
inserted
replaced
92:62eb9cbbcffc | 93:4c33e266db03 |
---|---|
20 from forums.forms import NewTopicForm | 20 from forums.forms import NewTopicForm |
21 from forums.forms import PostForm | 21 from forums.forms import PostForm |
22 | 22 |
23 ####################################################################### | 23 ####################################################################### |
24 | 24 |
25 TOPICS_PER_PAGE = 50 | |
25 POSTS_PER_PAGE = 2 | 26 POSTS_PER_PAGE = 2 |
26 | 27 |
27 def create_paginator(links): | 28 def create_topic_paginator(topics): |
28 return DiggPaginator(links, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) | 29 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2) |
30 | |
31 def create_post_paginator(posts): | |
32 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) | |
29 | 33 |
30 ####################################################################### | 34 ####################################################################### |
31 | 35 |
32 def index(request): | 36 def index(request): |
33 """ | 37 """ |
55 """ | 59 """ |
56 Displays all the topics in a forum. | 60 Displays all the topics in a forum. |
57 """ | 61 """ |
58 forum = get_object_or_404(Forum, slug=slug) | 62 forum = get_object_or_404(Forum, slug=slug) |
59 topics = forum.topics.select_related() | 63 topics = forum.topics.select_related() |
64 paginator = create_topic_paginator(topics) | |
65 page_num = int(request.GET.get('page', 1)) | |
66 try: | |
67 page = paginator.page(page_num) | |
68 except InvalidPage: | |
69 raise Http404 | |
60 | 70 |
61 return render_to_response('forums/forum_index.html', { | 71 return render_to_response('forums/forum_index.html', { |
62 'forum': forum, | 72 'forum': forum, |
63 'topics': topics, | 73 'page': page, |
64 }, | 74 }, |
65 context_instance=RequestContext(request)) | 75 context_instance=RequestContext(request)) |
66 | 76 |
67 | 77 |
68 def topic_index(request, id): | 78 def topic_index(request, id): |
72 topic = get_object_or_404(Topic, pk=id) | 82 topic = get_object_or_404(Topic, pk=id) |
73 topic.view_count += 1 | 83 topic.view_count += 1 |
74 topic.save() | 84 topic.save() |
75 | 85 |
76 posts = topic.posts.select_related() | 86 posts = topic.posts.select_related() |
77 paginator = create_paginator(posts) | 87 paginator = create_post_paginator(posts) |
78 page_num = int(request.GET.get('page', '1')) | 88 page_num = int(request.GET.get('page', 1)) |
79 try: | 89 try: |
80 page = paginator.page(page_num) | 90 page = paginator.page(page_num) |
81 except InvalidPage: | 91 except InvalidPage: |
82 raise Http404 | 92 raise Http404 |
83 | 93 |