changeset 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 62eb9cbbcffc
children 806399f3b950
files gpp/forums/templatetags/forum_tags.py gpp/forums/views.py gpp/templates/forums/forum_index.html gpp/templates/forums/last_post_info.html gpp/templates/forums/pagination.html gpp/templates/forums/post_pagination.html gpp/templates/forums/topic.html
diffstat 7 files changed, 49 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/templatetags/forum_tags.py	Sun Sep 13 04:09:08 2009 +0000
+++ b/gpp/forums/templatetags/forum_tags.py	Sun Sep 13 04:33:15 2009 +0000
@@ -11,6 +11,6 @@
     return {'post': post}
 
 
-@register.inclusion_tag('forums/post_pagination.html')
-def post_navigation(page):
+@register.inclusion_tag('forums/pagination.html')
+def forum_page_navigation(page):
     return {'page': page}
--- a/gpp/forums/views.py	Sun Sep 13 04:09:08 2009 +0000
+++ b/gpp/forums/views.py	Sun Sep 13 04:33:15 2009 +0000
@@ -22,10 +22,14 @@
 
 #######################################################################
 
+TOPICS_PER_PAGE = 50
 POSTS_PER_PAGE = 2
 
-def create_paginator(links):
-   return DiggPaginator(links, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
+def create_topic_paginator(topics):
+   return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
+
+def create_post_paginator(posts):
+   return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
 
 #######################################################################
 
@@ -57,10 +61,16 @@
     """
     forum = get_object_or_404(Forum, slug=slug)
     topics = forum.topics.select_related()
+    paginator = create_topic_paginator(topics)
+    page_num = int(request.GET.get('page', 1))
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
     
     return render_to_response('forums/forum_index.html', {
         'forum': forum,
-        'topics': topics,
+        'page': page,
         },
         context_instance=RequestContext(request))
 
@@ -74,8 +84,8 @@
     topic.save()
 
     posts = topic.posts.select_related()
-    paginator = create_paginator(posts)
-    page_num = int(request.GET.get('page', '1'))
+    paginator = create_post_paginator(posts)
+    page_num = int(request.GET.get('page', 1))
     try:
         page = paginator.page(page_num)
     except InvalidPage:
--- a/gpp/templates/forums/forum_index.html	Sun Sep 13 04:09:08 2009 +0000
+++ b/gpp/templates/forums/forum_index.html	Sun Sep 13 04:33:15 2009 +0000
@@ -10,7 +10,8 @@
 </h3>
 
 <div class="forum-block">
-<a href="{% url forums-new_topic slug=forum.slug %}">New Post</a>
+<a href="{% url forums-new_topic slug=forum.slug %}">New Topic</a>
+{% forum_page_navigation page %}
 <table class="forum-index-table">
    <thead>
       <tr>
@@ -22,8 +23,8 @@
       </tr>
    </thead>
    <tbody>
-   {% for topic in topics %}
-      <tr>
+   {% for topic in page.object_list %}
+      <tr class="{% cycle 'odd' 'even' %}">
          <td><h4><a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a></h4></td>
          <td class="forum-index_replies">{{ topic.reply_count }}</td>
          <td class="forum-index_author"><a href="{% url bio-view_profile username=topic.user.username %}" title="View profile for {{ topic.user.username }}">{{ topic.user.username }}</a></td>
@@ -41,5 +42,6 @@
    {% endfor %}
    </tbody>
 </table>
+{% forum_page_navigation page %}
 </div>
 {% endblock %}
--- a/gpp/templates/forums/last_post_info.html	Sun Sep 13 04:09:08 2009 +0000
+++ b/gpp/templates/forums/last_post_info.html	Sun Sep 13 04:33:15 2009 +0000
@@ -1,5 +1,5 @@
 {% if post %}
-{{ post.update_date|date:"M d, Y H:i"}}<br />
+<a href="{{ post.get_absolute_url }}">{{ post.update_date|date:"M d, Y H:i"}}</a><br />
 <a href="{% url bio-view_profile username=post.user.username %}" title="View profile for {{ post.user.username }}">{{ post.user.username }}</a>
 {% else %}
 <i>No posts</i>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/pagination.html	Sun Sep 13 04:33:15 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/post_pagination.html	Sun Sep 13 04:09:08 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-<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	Sun Sep 13 04:09:08 2009 +0000
+++ b/gpp/templates/forums/topic.html	Sun Sep 13 04:33:15 2009 +0000
@@ -18,14 +18,14 @@
 <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 %}
+{% forum_page_navigation page %}
 
 <table class="forum-topic" id="forum-topic">
 {% for post in page.object_list %}
 {% include 'forums/display_post.html' %}
 {% endfor %}
 </table>
-{% post_navigation page %}
+{% forum_page_navigation page %}
 
 {% if last_page and user.is_authenticated %}
 <a name="forum-reply-form"></a>