# HG changeset patch # User Brian Neal # Date 1252801314 0 # Node ID 317c7bcaeceea89f6434cbccb70159e094a52467 # Parent 021492db4aad2cc530607784606f7351f7ec91fd Forums: pagination for topics. diff -r 021492db4aad -r 317c7bcaecee gpp/forums/templatetags/forum_tags.py --- a/gpp/forums/templatetags/forum_tags.py Sat Sep 12 21:29:31 2009 +0000 +++ b/gpp/forums/templatetags/forum_tags.py Sun Sep 13 00:21:54 2009 +0000 @@ -9,3 +9,8 @@ @register.inclusion_tag('forums/last_post_info.html') def last_post_info(post): return {'post': post} + + +@register.inclusion_tag('forums/post_pagination.html') +def post_navigation(page): + return {'page': page} diff -r 021492db4aad -r 317c7bcaecee gpp/forums/views.py --- a/gpp/forums/views.py Sat Sep 12 21:29:31 2009 +0000 +++ b/gpp/forums/views.py Sun Sep 13 00:21:54 2009 +0000 @@ -4,6 +4,7 @@ from django.contrib.auth.decorators import login_required from django.http import Http404 from django.http import HttpResponseBadRequest +from django.http import HttpResponseForbidden from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.shortcuts import get_object_or_404 @@ -11,11 +12,20 @@ from django.template import RequestContext from django.views.decorators.http import require_POST +from core.paginator import DiggPaginator from forums.models import Forum from forums.models import Topic from forums.forms import NewTopicForm from forums.forms import PostForm +####################################################################### + +POSTS_PER_PAGE = 2 + +def create_paginator(links): + return DiggPaginator(links, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) + +####################################################################### def index(request): """ @@ -62,12 +72,19 @@ topic.save() posts = topic.posts.select_related() - last_page = True # TODO + paginator = create_paginator(posts) + page_num = int(request.GET.get('page', '1')) + try: + page = paginator.page(page_num) + except InvalidPage: + raise Http404 + + last_page = page_num == paginator.num_pages return render_to_response('forums/topic.html', { 'forum': topic.forum, 'topic': topic, - 'posts': posts, + 'page': page, 'last_page': last_page, 'form': PostForm(initial={'topic_id': topic.id}), }, @@ -109,7 +126,6 @@ context_instance=RequestContext(request)) -@login_required @require_POST def quick_reply_ajax(request): """ @@ -118,6 +134,9 @@ the HTML for the new post, which the client-side script appends to the document. """ + if not request.user.is_authenticated(): + return HttpResponseForbidden() + form = PostForm(request.POST) if form.is_valid(): post = form.save(request.user, request.META.get("REMOTE_ADDR")) diff -r 021492db4aad -r 317c7bcaecee gpp/templates/forums/post_pagination.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/post_pagination.html Sun Sep 13 00:21:54 2009 +0000 @@ -0,0 +1,24 @@ +
+{% ifequal page.paginator.num_pages 1 %} +Page 1 of 1 +{% else %} +Goto Page: +{% if page.has_previous %} +Previous +{% endif %} +{% for num in page.page_range %} +{% if num %} +{% ifequal num page.number %} +{{ num }} +{% else %} +{{ num }} +{% endifequal %} +{% else %} +… +{% endif %} +{% endfor %} +{% if page.has_next %} +Next +{% endif %} +{% endifequal %} +
diff -r 021492db4aad -r 317c7bcaecee gpp/templates/forums/topic.html --- a/gpp/templates/forums/topic.html Sat Sep 12 21:29:31 2009 +0000 +++ b/gpp/templates/forums/topic.html Sun Sep 13 00:21:54 2009 +0000 @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% load forum_tags %} {% block title %}Forums: {{ topic.name }}{% endblock %} {% block custom_js %}{% if last_page %}{{ form.media }}{% endif %}{% endblock %} {% block content %} @@ -14,15 +15,18 @@ {% if last_page %} New Reply • {% else %} -New Reply • +New Reply • {% endif %} New Topic +{% post_navigation page %} -{% for post in posts %} +{% for post in page.object_list %} {% include 'forums/display_post.html' %} {% endfor %}
+{% post_navigation page %} + {% if last_page and user.is_authenticated %}
diff -r 021492db4aad -r 317c7bcaecee media/css/base.css --- a/media/css/base.css Sat Sep 12 21:29:31 2009 +0000 +++ b/media/css/base.css Sun Sep 13 00:21:54 2009 +0000 @@ -221,3 +221,9 @@ float: left; margin-right: 5px; } +.forums-post-navigation { + text-align: right; +} +#forums-quick-reply { + margin-top: 1.5em; +}