Mercurial > public > sg101
comparison gpp/forums/views.py @ 90:317c7bcaecee
Forums: pagination for topics.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 13 Sep 2009 00:21:54 +0000 |
parents | 021492db4aad |
children | 62af8cd8f57b |
comparison
equal
deleted
inserted
replaced
89:021492db4aad | 90:317c7bcaecee |
---|---|
2 Views for the forums application. | 2 Views for the forums application. |
3 """ | 3 """ |
4 from django.contrib.auth.decorators import login_required | 4 from django.contrib.auth.decorators import login_required |
5 from django.http import Http404 | 5 from django.http import Http404 |
6 from django.http import HttpResponseBadRequest | 6 from django.http import HttpResponseBadRequest |
7 from django.http import HttpResponseForbidden | |
7 from django.http import HttpResponseRedirect | 8 from django.http import HttpResponseRedirect |
8 from django.core.urlresolvers import reverse | 9 from django.core.urlresolvers import reverse |
9 from django.shortcuts import get_object_or_404 | 10 from django.shortcuts import get_object_or_404 |
10 from django.shortcuts import render_to_response | 11 from django.shortcuts import render_to_response |
11 from django.template import RequestContext | 12 from django.template import RequestContext |
12 from django.views.decorators.http import require_POST | 13 from django.views.decorators.http import require_POST |
13 | 14 |
15 from core.paginator import DiggPaginator | |
14 from forums.models import Forum | 16 from forums.models import Forum |
15 from forums.models import Topic | 17 from forums.models import Topic |
16 from forums.forms import NewTopicForm | 18 from forums.forms import NewTopicForm |
17 from forums.forms import PostForm | 19 from forums.forms import PostForm |
18 | 20 |
21 ####################################################################### | |
22 | |
23 POSTS_PER_PAGE = 2 | |
24 | |
25 def create_paginator(links): | |
26 return DiggPaginator(links, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) | |
27 | |
28 ####################################################################### | |
19 | 29 |
20 def index(request): | 30 def index(request): |
21 """ | 31 """ |
22 This view displays all the forums available, ordered in each category. | 32 This view displays all the forums available, ordered in each category. |
23 """ | 33 """ |
60 topic = get_object_or_404(Topic, pk=id) | 70 topic = get_object_or_404(Topic, pk=id) |
61 topic.view_count += 1 | 71 topic.view_count += 1 |
62 topic.save() | 72 topic.save() |
63 | 73 |
64 posts = topic.posts.select_related() | 74 posts = topic.posts.select_related() |
65 last_page = True # TODO | 75 paginator = create_paginator(posts) |
76 page_num = int(request.GET.get('page', '1')) | |
77 try: | |
78 page = paginator.page(page_num) | |
79 except InvalidPage: | |
80 raise Http404 | |
81 | |
82 last_page = page_num == paginator.num_pages | |
66 | 83 |
67 return render_to_response('forums/topic.html', { | 84 return render_to_response('forums/topic.html', { |
68 'forum': topic.forum, | 85 'forum': topic.forum, |
69 'topic': topic, | 86 'topic': topic, |
70 'posts': posts, | 87 'page': page, |
71 'last_page': last_page, | 88 'last_page': last_page, |
72 'form': PostForm(initial={'topic_id': topic.id}), | 89 'form': PostForm(initial={'topic_id': topic.id}), |
73 }, | 90 }, |
74 context_instance=RequestContext(request)) | 91 context_instance=RequestContext(request)) |
75 | 92 |
107 'topic': topic, | 124 'topic': topic, |
108 }, | 125 }, |
109 context_instance=RequestContext(request)) | 126 context_instance=RequestContext(request)) |
110 | 127 |
111 | 128 |
112 @login_required | |
113 @require_POST | 129 @require_POST |
114 def quick_reply_ajax(request): | 130 def quick_reply_ajax(request): |
115 """ | 131 """ |
116 This function handles the quick reply to a thread function. This | 132 This function handles the quick reply to a thread function. This |
117 function is meant to be the target of an AJAX post, and returns | 133 function is meant to be the target of an AJAX post, and returns |
118 the HTML for the new post, which the client-side script appends | 134 the HTML for the new post, which the client-side script appends |
119 to the document. | 135 to the document. |
120 """ | 136 """ |
137 if not request.user.is_authenticated(): | |
138 return HttpResponseForbidden() | |
139 | |
121 form = PostForm(request.POST) | 140 form = PostForm(request.POST) |
122 if form.is_valid(): | 141 if form.is_valid(): |
123 post = form.save(request.user, request.META.get("REMOTE_ADDR")) | 142 post = form.save(request.user, request.META.get("REMOTE_ADDR")) |
124 return render_to_response('forums/display_post.html', { | 143 return render_to_response('forums/display_post.html', { |
125 'post': post, | 144 'post': post, |