From 2a656448130fe9aa64ee99f7a4d25986a27ac564 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Wed, 13 Dec 2017 09:56:18 +1100 Subject: [PATCH] Switch from custom pagination to django.core.paginator, since it can do things like counts and 'is there a next page' more easily --- entries/urls.py | 6 ++-- entries/views.py | 36 ++++++++------------- home/urls.py | 2 +- home/views.py | 19 ++++++----- lemoncurry/templates/lemoncurry/layout.html | 1 - 5 files changed, 26 insertions(+), 38 deletions(-) diff --git a/entries/urls.py b/entries/urls.py index 455692b..60b2c56 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -11,14 +11,14 @@ def prefix(route): return app_name + ':' + route -before = '(?:/before/(?P\d+))?' +page = '(?:/page/(?P\d+))?' slug = r'/(?P[^/]+)' app_name = 'entries' urlpatterns = [ url('^atom$', feeds.AtomHomeEntries(), name='atom'), url('^rss$', feeds.RssHomeEntries(), name='rss'), - url(to_pat('cats', slug, before), views.cat, name='cat'), + url(to_pat('cats', slug, page), views.cat, name='cat'), ] crumbs.add(prefix('cat'), parent='home:index') @@ -28,7 +28,7 @@ for k in kinds.all: kind = k.plural id = r'/(?P\d+)' urlpatterns += ( - url(to_pat(kind, before), views.index, name=k.index, kwargs={'kind': k}), + url(to_pat(kind, page), views.index, name=k.index, kwargs={'kind': k}), url(to_pat(kind, '/atom'), feeds.AtomByKind(k), name=k.atom), url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss), url(to_pat(kind, id, slug), views.entry, name=k.entry), diff --git a/entries/views.py b/entries/views.py index c172bcd..a69a3ba 100644 --- a/entries/views.py +++ b/entries/views.py @@ -1,24 +1,21 @@ from annoying.decorators import render_to +from django.core.paginator import Paginator from django.shortcuts import get_object_or_404, redirect -from django.urls import reverse from .models import Entry, Cat @render_to('entries/index.html') -def index(request, kind, before=None): - entries = Entry.objects.filter(kind=kind.id) - if before: - entries = entries.filter(id__lt=before) - entries = entries[:10] +def index(request, kind, page): + paginator = Paginator(Entry.objects.filter(kind=kind.id), 10) - next = None - if entries: - last = entries.last().id - next = reverse('entries:' + kind.index, kwargs={'before': last}) + # If we explicitly got /page/1 in the URL then redirect to the version with + # no page suffix. + if page == '1': + return redirect('entries:' + kind.index, permanent=True) + entries = paginator.page(page or 1) return { 'entries': entries, - 'next': next, 'atom': 'entries:' + kind.atom, 'rss': 'entries:' + kind.rss, 'title': kind.plural, @@ -26,22 +23,15 @@ def index(request, kind, before=None): @render_to('entries/index.html') -def cat(request, slug, before=None): +def cat(request, slug, page): cat = get_object_or_404(Cat, slug=slug) - entries = cat.entries.all() - if before: - entries = entries.filter(id__lt=before) - entries = entries[:10] + paginator = Paginator(cat.entries.all(), 10) + if page == '1': + return redirect('entries:cat', permanent=True, slug=slug) + entries = paginator.page(page or 1) - next = None - if entries: - next = reverse('entries:cat', kwargs={ - 'slug': slug, - 'before': entries.last().id - }) return { 'entries': entries, - 'next': next, 'title': '#' + cat.name, } diff --git a/home/urls.py b/home/urls.py index 9c48792..14b4ced 100644 --- a/home/urls.py +++ b/home/urls.py @@ -4,6 +4,6 @@ from . import views app_name = 'home' urlpatterns = [ - url(r'^(?:before/(?P\d+))?$', views.index, name='index'), + url(r'^(?:page/(?P\d+))?$', views.index, name='index'), url(r'^robots.txt$', views.robots, name='robots.txt'), ] diff --git a/home/views.py b/home/views.py index 9a7e557..1d23f5c 100644 --- a/home/views.py +++ b/home/views.py @@ -1,6 +1,7 @@ from annoying.decorators import render_to +from django.core.paginator import Paginator from django.http import HttpResponse -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect from django.urls import reverse from users.models import User from urllib.parse import urljoin @@ -12,22 +13,20 @@ breadcrumbs.add('home:index', 'home') @render_to('home/index.html') -def index(request, before=None): +def index(request, page): query = User.objects.prefetch_related('entries', 'profiles', 'keys') user = get_object_or_404(query, pk=1) - entries = user.entries.filter(kind__in=kinds.on_home) - if before: - entries = entries.filter(id__lt=before) - entries = entries[:10] + paginator = Paginator(user.entries.filter(kind__in=kinds.on_home), 10) - next = None - if entries: - next = reverse('home:index', kwargs={'before': entries.last().id}) + # If we explicitly got /page/1 in the URL then redirect to the version with + # no page suffix. + if page == '1': + return redirect('home:index', permanent=True) + entries = paginator.page(page or 1) return { 'user': user, 'entries': entries, - 'next': next, 'atom': 'entries:atom', 'rss': 'entries:rss', 'meta': user.as_meta(request), diff --git a/lemoncurry/templates/lemoncurry/layout.html b/lemoncurry/templates/lemoncurry/layout.html index ea5568f..8fe6618 100644 --- a/lemoncurry/templates/lemoncurry/layout.html +++ b/lemoncurry/templates/lemoncurry/layout.html @@ -10,7 +10,6 @@ {% if atom %}{% endif %} {% if rss %} {% endif %} - {% if next %}{% endif %} {% block head %}{% endblock %}