Commits

Pedro Ferreira  committed 55124e0

- limit the number of threads to show at once in a sub-forum (only a certain number per page)

  • Participants
  • Parent commits 4edf376

Comments (0)

Files changed (5)

File forum/models.py

         return self.name
 
     def get_url(self):
-        return reverse( 'subForum', args= [ self.slug ] )
+        return reverse( 'subForum', args= [ self.slug, 0 ] )
 
     def get_last_post(self):
 

File forum/settings.py

 RETIRED_USERNAME = 'user_no_more'
 RETIRED_PASSWORD = 'a'
 
-POSTS_PER_PAGE = 10
+POSTS_PER_PAGE = 10     # in a thread
+THREADS_PER_PAGE = 10   # in a sub-forum
 
 INSTALLED_APPS = (
     'django.contrib.auth',

File forum/urls.py

 urlpatterns = patterns('',
 
     url( r'^$', 'forum.views.index', name= 'index' ),
-    url( r'^forum/(?P<forumSlug>[\w-]+)$', 'forum.views.sub_forum', name= 'subForum' ),
+    url( r'^forum/(?P<forumSlug>[\w-]+)/(?P<page>[0-9]+)$', 'forum.views.sub_forum', name= 'subForum' ),
     url( r'^thread/(?P<threadSlug>[\w-]+)/(?P<page>[0-9]+)$', 'forum.views.open_thread', name= 'thread' ),
 
     url( r'^new_thread/(?P<forumSlug>[\w-]+)$', 'forum.views.new_thread', name= 'new_thread' ),

File forum/views.py

     return render( request, 'index.html', context )
 
 
-def sub_forum( request, forumSlug ):
+def sub_forum( request, forumSlug, page= 0 ):
 
     try:
         forum = SubForum.objects.get( slug= forumSlug )
 
     ordered = sorted( threads, key= sortThreads, reverse= True )
 
+    threadsPerPage = settings.THREADS_PER_PAGE
+    page = int( page )
+    startThread = page * threadsPerPage
+    totalThreads = len( ordered )
+    totalPages = int( math.ceil( float(totalThreads) / float(threadsPerPage) ) )
+
+    if page != 0 and startThread >= totalThreads:
+        raise Http404( "Invalid sub-forum page." )
+
+    pageThreads = ordered[ startThread : startThread + threadsPerPage ]
+
     context = {
-        'threads': ordered,
+        'threads': pageThreads,
         'forumSlug': forumSlug,
+        'page': page,
+        'pages': range( 0, totalPages ),
         'paths': [ forum ]
     }
 
 
     allPosts = theThread.post_set.all()
     totalPosts = allPosts.count()
-    totalPages = int( math.ceil( totalPosts / postPerPage ) )
+    totalPages = int( math.ceil( float(totalPosts) / float(postPerPage) ) )
 
     if page != 0 and startPost >= totalPosts:
         raise Http404( "Invalid thread page." )
         'posts': pagePosts,
         'threadSlug': threadSlug,
         'page': page,
-        'pages': range( 0, totalPages + 1 ),
+        'pages': range( 0, totalPages ),
         'form': form,
         'paths': [ theThread.sub_forum, theThread ]
     }

File templates/sub_forum.html

     {% endif %}
 </p>
 
+<p>
+{% for pageNumber in pages %}
+    <a href="{% url 'subForum' forumSlug pageNumber %}" class="PageLink">
+        {% if pageNumber == page %}
+            <em>{{ pageNumber }}</em>
+        {% else %}
+            {{ pageNumber }}
+        {% endif %}
+    </a>
+{% endfor %}
+</p>
+
 {% if threads %}
     <table id="SubForumTable">
     {% for thread in threads %}
         </tr>
     {% endfor %}
     </table>
+
+    <p>
+    {% for pageNumber in pages %}
+        <a href="{% url 'subForum' forumSlug pageNumber %}" class="PageLink">
+            {% if pageNumber == page %}
+                <em>{{ pageNumber }}</em>
+            {% else %}
+                {{ pageNumber }}
+            {% endif %}
+        </a>
+    {% endfor %}
+    </p>
+
 {% else %}
     <p>No threads.</p>
 {% endif %}