Commits

Pedro Ferreira  committed 4edf376

- limit the number of posts to show at once in a thread (only a certain number per page)

  • Participants
  • Parent commits b07ce31

Comments (0)

Files changed (7)

File forum/models.py

         return self.title
 
     def get_url(self):
-        return reverse( 'thread', args= [ self.slug ] )
+        return reverse( 'thread', args= [ self.slug, 0 ] )
 
     def get_last_post(self):
 
             if post == self:
                 position = index
 
-        url = reverse( 'thread', args= [ self.thread.slug ] )
+        postsPerPage = settings.POSTS_PER_PAGE
+        page = 0
+
+        while position >= postsPerPage:
+            position -= postsPerPage
+            page += 1
+
+        url = reverse( 'thread', args= [ self.thread.slug, page ] )
 
         url += '#post_' + str( position + 1 )
 

File forum/settings.py

 RETIRED_USERNAME = 'user_no_more'
 RETIRED_PASSWORD = 'a'
 
+POSTS_PER_PAGE = 10
+
 INSTALLED_APPS = (
     'django.contrib.auth',
     'django.contrib.contenttypes',

File forum/urls.py

 
     url( r'^$', 'forum.views.index', name= 'index' ),
     url( r'^forum/(?P<forumSlug>[\w-]+)$', 'forum.views.sub_forum', name= 'subForum' ),
-    url( r'^thread/(?P<threadSlug>[\w-]+)$', 'forum.views.open_thread', name= 'thread' ),
+    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' ),
     url( r'^new_category$', 'forum.views.new_category', name= 'new_category' ),

File forum/views.py

 from django.utils import timezone
 from django.conf import settings
 
+import math
+
 from forum.models import Category, SubForum, Thread, Post, PrivateMessage
 from forum.forms import PostForm, MyUserCreationForm, PrivateMessageForm, ThreadForm, CategoryForm, SubForumForm
 import forum.utilities as utilities
     return render( request, 'sub_forum.html', context )
 
 
-def open_thread( request, threadSlug ):
+def open_thread( request, threadSlug, page= 0 ):
 
     try:
         theThread = Thread.objects.get( slug= threadSlug )
     else:
         form = PostForm()
 
+    postPerPage = settings.POSTS_PER_PAGE
+    page = int( page )
+    startPost = page * postPerPage
+
+    allPosts = theThread.post_set.all()
+    totalPosts = allPosts.count()
+    totalPages = int( math.ceil( totalPosts / postPerPage ) )
+
+    if page != 0 and startPost >= totalPosts:
+        raise Http404( "Invalid thread page." )
+
+    pagePosts = allPosts[ startPost : startPost + postPerPage ]
+
     context = {
         'thread': theThread,
+        'posts': pagePosts,
         'threadSlug': threadSlug,
+        'page': page,
+        'pages': range( 0, totalPages + 1 ),
         'form': form,
         'paths': [ theThread.sub_forum, theThread ]
     }

File static/style.css

     list-style-type: none;
     }
 
+.PageLink
+    {
+    display: inline-block;
+    }
+
+.PageLink em
+    {
+    font-weight: bold;
+    font-style: normal;
+    }
+
     /* menu */
 
 #Menu li
     background-color: rgba(229, 214, 237, 0.05);
     }
 
+.ThreadRow .ThreadLeft
+    {
+    background-color: rgba(201, 219, 237, 0.83);
+    }
+
+.ThreadRow .ThreadRight
+    {
+    background-color: rgba(234, 244, 255, 0.74);
+    }
+
 #ThreadTable
     {
     width: 100%;

File templates/sub_forum.html

             <td>posts: {{ thread.get_post_count }}</td>
             <td>lastest post:
                 {% if thread.get_last_post %}
-                    <a href="{% url 'thread' thread.get_last_post.thread.slug %}">{{ thread.get_last_post.text|truncatechars:10 }}</a> by <a href="{% url 'user_page' thread.get_last_post.user.username %}">{{ thread.get_last_post.user }}</a> {{ thread.get_last_post.date_created|naturaltime }}
+                    <a href="{% url 'thread' thread.get_last_post.thread.slug 0 %}">{{ thread.get_last_post.text|truncatechars:10 }}</a> by <a href="{% url 'user_page' thread.get_last_post.user.username %}">{{ thread.get_last_post.user }}</a> {{ thread.get_last_post.date_created|naturaltime }}
                 {% else %}
                     ---
                 {% endif %}

File templates/thread.html

 {% block content %}
 {% if thread %}
 
+<p>
+{% for pageNumber in pages %}
+    <a href="{% url 'thread' threadSlug pageNumber %}" class="PageLink">
+        {% if pageNumber == page %}
+            <em>{{ pageNumber }}</em>
+        {% else %}
+            {{ pageNumber }}
+        {% endif %}
+    </a>
+{% endfor %}
+</p>
+
+
 <table id="ThreadTable">
-    <tr id="post_0">
+    <tr id="post_0" class="ThreadRow">
         <td class="ThreadLeft">
             <a href="{{ thread.user.get_url }}">{{ thread.user }}</a> {{ thread.date_created|naturaltime }}<br />
             {{ thread.user.get_post_count }} posts<br />
     </tr>
 
 
-{% if thread.post_set.all %}
-    {% for post in thread.post_set.all %}
-    <tr id="post_{{ forloop.counter }}">
+{% if posts %}
+    {% for post in posts %}
+    <tr id="post_{{ forloop.counter }}" class="PostRow">
         <td class="ThreadLeft">
             <a href="{{ post.user.get_url }}">{{ post.user }}</a> {{ post.date_created|naturaltime }}<br />
             {{ post.user.get_post_count }} posts<br />
 {% endif %}
 </table>
 
-    {% if user.is_authenticated %}
-        {% if form.errors %}
-            <p>{{ form.errors }}</p>
+<p>
+{% for pageNumber in pages %}
+    <a href="{% url 'thread' threadSlug pageNumber %}" class="PageLink">
+        {% if pageNumber == page %}
+            <em>{{ pageNumber }}</em>
+        {% else %}
+            {{ pageNumber }}
         {% endif %}
-        <form id="ThreadPostForm" method="post" action="{% url 'thread' threadSlug %}">
-            {% csrf_token %}
+    </a>
+{% endfor %}
+</p>
 
-            {{ form.text }}
+{% if user.is_authenticated %}
+    {% if form.errors %}
+        <p>{{ form.errors }}</p>
+    {% endif %}
+    <form id="ThreadPostForm" method="post" action="{% url 'thread' threadSlug page %}">
+        {% csrf_token %}
 
-            <input type="submit" value="post" />
-            <input type="hidden" name="next" value="{{ next }}" />
-        </form>
-    {% else %}
-        <p>Need to login to post.</p>
-    {% endif %}
+        {{ form.text }}
+
+        <input type="submit" value="post" />
+        <input type="hidden" name="next" value="{{ next }}" />
+    </form>
+{% else %}
+    <p>Need to login to post.</p>
+{% endif %}
 {% else %}
     <p>No thread.</p>
 {% endif %}