From 2d2159ee58b61f72a7be3803db185c1b2f3028bb Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Tue, 12 Dec 2017 18:35:13 +1100 Subject: [PATCH] Enable simple entry pagination - each h-feed page has a rel=next pointing to the next page of the feed, but there's no visible link yet --- entries/urls.py | 10 ++++--- entries/views.py | 29 ++++++++++++++++++--- home/urls.py | 2 +- home/views.py | 10 ++++++- lemoncurry/templates/lemoncurry/layout.html | 1 + 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/entries/urls.py b/entries/urls.py index 4821c1c..455692b 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -11,20 +11,24 @@ def prefix(route): return app_name + ':' + route +before = '(?:/before/(?P\d+))?' +slug = r'/(?P[^/]+)' + app_name = 'entries' urlpatterns = [ url('^atom$', feeds.AtomHomeEntries(), name='atom'), url('^rss$', feeds.RssHomeEntries(), name='rss'), - url('^cats/(?P.+)$', views.cat, name='cat'), + url(to_pat('cats', slug, before), views.cat, name='cat'), ] crumbs.add(prefix('cat'), parent='home:index') +slug = '(?:' + slug + ')?' + for k in kinds.all: kind = k.plural id = r'/(?P\d+)' - slug = r'(?:/(?P.+))?' urlpatterns += ( - url(to_pat(kind), views.index, name=k.index, kwargs={'kind': k}), + url(to_pat(kind, before), 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 89fcc04..c172bcd 100644 --- a/entries/views.py +++ b/entries/views.py @@ -1,13 +1,24 @@ from annoying.decorators import render_to 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): +def index(request, kind, before=None): entries = Entry.objects.filter(kind=kind.id) + if before: + entries = entries.filter(id__lt=before) + entries = entries[:10] + + next = None + if entries: + last = entries.last().id + next = reverse('entries:' + kind.index, kwargs={'before': last}) + return { 'entries': entries, + 'next': next, 'atom': 'entries:' + kind.atom, 'rss': 'entries:' + kind.rss, 'title': kind.plural, @@ -15,10 +26,22 @@ def index(request, kind): @render_to('entries/index.html') -def cat(request, slug): +def cat(request, slug, before=None): cat = get_object_or_404(Cat, slug=slug) + entries = cat.entries.all() + if before: + entries = entries.filter(id__lt=before) + entries = entries[:10] + + next = None + if entries: + next = reverse('entries:cat', kwargs={ + 'slug': slug, + 'before': entries.last().id + }) return { - 'entries': cat.entries.all(), + 'entries': entries, + 'next': next, 'title': '#' + cat.name, } diff --git a/home/urls.py b/home/urls.py index f8f9663..9c48792 100644 --- a/home/urls.py +++ b/home/urls.py @@ -4,6 +4,6 @@ from . import views app_name = 'home' urlpatterns = [ - url(r'^$', views.index, name='index'), + url(r'^(?:before/(?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 2534a23..9a7e557 100644 --- a/home/views.py +++ b/home/views.py @@ -12,14 +12,22 @@ breadcrumbs.add('home:index', 'home') @render_to('home/index.html') -def index(request): +def index(request, before=None): 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] + + next = None + if entries: + next = reverse('home:index', kwargs={'before': entries.last().id}) 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 8fe6618..ea5568f 100644 --- a/lemoncurry/templates/lemoncurry/layout.html +++ b/lemoncurry/templates/lemoncurry/layout.html @@ -10,6 +10,7 @@ {% if atom %}{% endif %} {% if rss %} {% endif %} + {% if next %}{% endif %} {% block head %}{% endblock %}