Refactor topic to use paginator instead of custom paging

This commit is contained in:
Skia 2017-03-15 18:34:22 +01:00
parent 701f23b539
commit f400be3a79
2 changed files with 13 additions and 13 deletions

View File

@ -39,8 +39,8 @@
<p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p> <p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p>
<p style="text-align: right; background: #d8e7f3;"> <p style="text-align: right; background: #d8e7f3;">
{% for p in range(1, page_count + 1) %} {% for p in msgs.paginator.page_range %}
<span class="ib" style="background: {% if p == current_page %}white{% endif %}; margin: 0;"><a href="?page={{ p }}">{{ p }}</a></span> <span class="ib" style="background: {% if p == msgs.number %}white{% endif %}; margin: 0;"><a href="?page={{ p }}">{{ p }}</a></span>
{% endfor %} {% endfor %}
</p> </p>
@ -58,8 +58,8 @@
<p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p> <p><a href="{{ url('forum:new_message', topic_id=topic.id) }}">{% trans %}Reply{% endtrans %}</a></p>
<p style="text-align: right; background: #d8e7f3;"> <p style="text-align: right; background: #d8e7f3;">
{% for p in range(1, page_count + 1) %} {% for p in msgs.paginator.page_range %}
<span class="ib" style="background: {% if p == current_page %}white{% endif %}; margin: 0;"><a href="?page={{ p }}">{{ p }}</a></span> <span class="ib" style="background: {% if p == msgs.number %}white{% endif %}; margin: 0;"><a href="?page={{ p }}">{{ p }}</a></span>
{% endfor %} {% endfor %}
</p> </p>
{% endblock %} {% endblock %}

View File

@ -9,6 +9,7 @@ from django.conf import settings
from django import forms from django import forms
from django.db import models from django.db import models
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from ajax_select import make_ajax_form, make_ajax_field 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 kwargs['first_unread_message_id'] = msg.id
except: except:
kwargs['first_unread_message_id'] = float("inf") 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: try:
page = int(self.request.GET['page']) - 1 kwargs["msgs"] = paginator.page(page)
if page < 0: except PageNotAnInteger:
page = 0 kwargs["msgs"] = paginator.page(1)
except: except EmptyPage:
page = 0 kwargs["msgs"] = paginator.page(paginator.num_pages)
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
return kwargs return kwargs
class ForumMessageEditView(CanEditMixin, UpdateView): class ForumMessageEditView(CanEditMixin, UpdateView):