From f400be3a7904debca0bd5d527b29f5b437a166b6 Mon Sep 17 00:00:00 2001 From: Skia Date: Wed, 15 Mar 2017 18:34:22 +0100 Subject: [PATCH] Refactor topic to use paginator instead of custom paging --- forum/templates/forum/topic.jinja | 8 ++++---- forum/views.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/forum/templates/forum/topic.jinja b/forum/templates/forum/topic.jinja index e05a96ac..f6d88fef 100644 --- a/forum/templates/forum/topic.jinja +++ b/forum/templates/forum/topic.jinja @@ -39,8 +39,8 @@

{% trans %}Reply{% endtrans %}

- {% for p in range(1, page_count + 1) %} - {{ p }} + {% for p in msgs.paginator.page_range %} + {{ p }} {% endfor %}

@@ -58,8 +58,8 @@

{% trans %}Reply{% endtrans %}

- {% for p in range(1, page_count + 1) %} - {{ p }} + {% for p in msgs.paginator.page_range %} + {{ p }} {% endfor %}

{% endblock %} diff --git a/forum/views.py b/forum/views.py index e5ebc989..34296606 100644 --- a/forum/views.py +++ b/forum/views.py @@ -9,6 +9,7 @@ from django.conf import settings from django import forms from django.db import models from django.core.exceptions import PermissionDenied +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from ajax_select import make_ajax_form, make_ajax_field @@ -139,16 +140,15 @@ class ForumTopicDetailView(CanViewMixin, DetailView): kwargs['first_unread_message_id'] = msg.id except: kwargs['first_unread_message_id'] = float("inf") - page_length = settings.SITH_FORUM_PAGE_LENGTH + paginator = Paginator(self.object.messages.select_related('author__avatar_pict').all(), + settings.SITH_FORUM_PAGE_LENGTH) + page = self.request.GET.get('page') try: - page = int(self.request.GET['page']) - 1 - if page < 0: - page = 0 - except: - page = 0 - kwargs["msgs"] = self.object.messages.select_related('author__avatar_pict').all()[page*page_length:page*page_length+page_length] - kwargs["page_count"] = int(self.object.messages.count() / page_length) + 1 - kwargs["current_page"] = page + 1 + kwargs["msgs"] = paginator.page(page) + except PageNotAnInteger: + kwargs["msgs"] = paginator.page(1) + except EmptyPage: + kwargs["msgs"] = paginator.page(paginator.num_pages) return kwargs class ForumMessageEditView(CanEditMixin, UpdateView):