changeset 90:317c7bcaecee

Forums: pagination for topics.
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Sep 2009 00:21:54 +0000 (2009-09-13)
parents 021492db4aad
children 62af8cd8f57b
files gpp/forums/templatetags/forum_tags.py gpp/forums/views.py gpp/templates/forums/post_pagination.html gpp/templates/forums/topic.html media/css/base.css
diffstat 5 files changed, 63 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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}
--- 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"))
--- /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 @@
+<div class="forums-post-navigation">
+{% ifequal page.paginator.num_pages 1 %}
+Page 1 of 1
+{% else %}
+Goto Page:
+{% if page.has_previous %}
+<a href="./?page={{ page.previous_page_number }}">Previous</a>
+{% endif %}
+{% for num in page.page_range %}
+{% if num %}
+{% ifequal num page.number %}
+{{ num }}
+{% else %}
+<a href="./?page={{ num }}">{{ num }}</a>
+{% endifequal %}
+{% else %}
+&hellip;
+{% endif %}
+{% endfor %}
+{% if page.has_next %}
+<a href="./?page={{ page.next_page_number }}">Next</a>
+{% endif %}
+{% endifequal %}
+</div>
--- 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 %}
 <a href="#forum-reply-form">New Reply</a> &bull;
 {% else %}
-<a href="">New Reply</a> &bull;
+<a href="./?page={{ page.paginator.num_pages }}#forum-reply-form">New Reply</a> &bull;
 {% endif %}
 <a href="{% url forums-new_topic slug=forum.slug %}">New Topic</a>
+{% post_navigation page %}
 
 <table class="forum-topic" id="forum-topic">
-{% for post in posts %}
+{% for post in page.object_list %}
 {% include 'forums/display_post.html' %}
 {% endfor %}
 </table>
+{% post_navigation page %}
+
 {% if last_page and user.is_authenticated %}
 <a name="forum-reply-form"></a>
 <form action="" method="post" id="forums-quick-reply">
--- 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;
+}