# HG changeset patch # User neko259 # Date 2014-01-08 15:01:47 # Node ID 1e58394d21335911655446feea97ac82e23c0a3f # Parent 6201400808df8be3ee4a15feaf0872489609a592 Using django paginator instead of manual pagination diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -6,6 +6,7 @@ import time import math import re from django.core.cache import cache +from django.core.paginator import Paginator from django.db import models from django.http import Http404 @@ -126,28 +127,12 @@ class PostManager(models.Manager): threads = threads.filter(archived=archived).order_by(order_by) if page != ALL_PAGES: - thread_count = threads.count() - - if page < self._get_page_count(thread_count): - start_thread = page * settings.THREADS_PER_PAGE - end_thread = min(start_thread + settings.THREADS_PER_PAGE, - thread_count) - threads = threads[start_thread:end_thread] + threads = Paginator(threads, settings.THREADS_PER_PAGE).page( + page).object_list return threads # TODO Move this method to thread manager - def get_thread_page_count(self, tag=None, archived=False): - if tag: - threads = Thread.objects.filter(tags=tag) - else: - threads = Thread.objects.all() - - threads = threads.filter(archived=archived) - - return self._get_page_count(threads.count()) - - # TODO Move this method to thread manager def _delete_old_threads(self): """ Preserves maximum thread count. If there are too many threads, @@ -180,13 +165,6 @@ class PostManager(models.Manager): referenced_post.last_edit_time = post.pub_time referenced_post.save() - def _get_page_count(self, thread_count): - """ - Get number of pages that will be needed for all threads - """ - - return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE))) - def get_posts_per_day(self): """ Get average count of posts per day for the last 7 days diff --git a/boards/templates/boards/archive.html b/boards/templates/boards/archive.html --- a/boards/templates/boards/archive.html +++ b/boards/templates/boards/archive.html @@ -6,23 +6,23 @@ {% load static %} {% block head %} - Neboard +Neboard - {% trans 'Archive' %} - {% if prev_page %} - {% endif %} - {% if next_page %} + {% if current_page.has_next %} {% endif %} @@ -34,9 +34,9 @@ {% get_current_language as LANGUAGE_CODE %} {% if threads %} - {% if prev_page %} + {% if current_page.has_previous %} {% endif %} @@ -111,9 +111,9 @@ {% endcache %} {% endfor %} - {% if next_page %} + {% if current_page.has_next %} {% endif %} {% else %} @@ -126,11 +126,11 @@ {% block metapanel %} - Neboard 1.5 Aker + Neboard 1.6 Amon {% trans "Pages:" %}[ - {% for page in pages %} + {% for page in paginator.page_range %} {% endif %} - {% if next_page %} + {% if current_page.has_next %} {% endif %} @@ -53,13 +53,13 @@ {% endif %} {% if threads %} - {% if prev_page %} + {% if current_page.has_previous %} @@ -69,9 +69,9 @@ {% cache 600 thread_short thread.id thread.thread.last_edit_time moderator LANGUAGE_CODE %}