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

This commit is contained in:
Danielle McLean 2017-12-12 18:35:13 +11:00
parent ea241577f1
commit 2d2159ee58
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 44 additions and 8 deletions

View file

@ -11,20 +11,24 @@ def prefix(route):
return app_name + ':' + route return app_name + ':' + route
before = '(?:/before/(?P<before>\d+))?'
slug = r'/(?P<slug>[^/]+)'
app_name = 'entries' app_name = 'entries'
urlpatterns = [ urlpatterns = [
url('^atom$', feeds.AtomHomeEntries(), name='atom'), url('^atom$', feeds.AtomHomeEntries(), name='atom'),
url('^rss$', feeds.RssHomeEntries(), name='rss'), url('^rss$', feeds.RssHomeEntries(), name='rss'),
url('^cats/(?P<slug>.+)$', views.cat, name='cat'), url(to_pat('cats', slug, before), views.cat, name='cat'),
] ]
crumbs.add(prefix('cat'), parent='home:index') crumbs.add(prefix('cat'), parent='home:index')
slug = '(?:' + slug + ')?'
for k in kinds.all: for k in kinds.all:
kind = k.plural kind = k.plural
id = r'/(?P<id>\d+)' id = r'/(?P<id>\d+)'
slug = r'(?:/(?P<slug>.+))?'
urlpatterns += ( 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, '/atom'), feeds.AtomByKind(k), name=k.atom),
url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss), url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss),
url(to_pat(kind, id, slug), views.entry, name=k.entry), url(to_pat(kind, id, slug), views.entry, name=k.entry),

View file

@ -1,13 +1,24 @@
from annoying.decorators import render_to from annoying.decorators import render_to
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from .models import Entry, Cat from .models import Entry, Cat
@render_to('entries/index.html') @render_to('entries/index.html')
def index(request, kind): def index(request, kind, before=None):
entries = Entry.objects.filter(kind=kind.id) 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 { return {
'entries': entries, 'entries': entries,
'next': next,
'atom': 'entries:' + kind.atom, 'atom': 'entries:' + kind.atom,
'rss': 'entries:' + kind.rss, 'rss': 'entries:' + kind.rss,
'title': kind.plural, 'title': kind.plural,
@ -15,10 +26,22 @@ def index(request, kind):
@render_to('entries/index.html') @render_to('entries/index.html')
def cat(request, slug): def cat(request, slug, before=None):
cat = get_object_or_404(Cat, slug=slug) 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 { return {
'entries': cat.entries.all(), 'entries': entries,
'next': next,
'title': '#' + cat.name, 'title': '#' + cat.name,
} }

View file

@ -4,6 +4,6 @@ from . import views
app_name = 'home' app_name = 'home'
urlpatterns = [ urlpatterns = [
url(r'^$', views.index, name='index'), url(r'^(?:before/(?P<before>\d+))?$', views.index, name='index'),
url(r'^robots.txt$', views.robots, name='robots.txt'), url(r'^robots.txt$', views.robots, name='robots.txt'),
] ]

View file

@ -12,14 +12,22 @@ breadcrumbs.add('home:index', 'home')
@render_to('home/index.html') @render_to('home/index.html')
def index(request): def index(request, before=None):
query = User.objects.prefetch_related('entries', 'profiles', 'keys') query = User.objects.prefetch_related('entries', 'profiles', 'keys')
user = get_object_or_404(query, pk=1) user = get_object_or_404(query, pk=1)
entries = user.entries.filter(kind__in=kinds.on_home) 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 { return {
'user': user, 'user': user,
'entries': entries, 'entries': entries,
'next': next,
'atom': 'entries:atom', 'atom': 'entries:atom',
'rss': 'entries:rss', 'rss': 'entries:rss',
'meta': user.as_meta(request), 'meta': user.as_meta(request),

View file

@ -10,6 +10,7 @@
<link rel="canonical" href="{{ uri }}" /> <link rel="canonical" href="{{ uri }}" />
{% if atom %}<link rel="alternate" type="application/atom+xml" href="{% url atom %}" />{% endif %} {% if atom %}<link rel="alternate" type="application/atom+xml" href="{% url atom %}" />{% endif %}
{% if rss %}<link rel="alternate" type="application/rss+xml" href="{% url rss %}" /> {% endif %} {% if rss %}<link rel="alternate" type="application/rss+xml" href="{% url rss %}" /> {% endif %}
{% if next %}<link rel="next" href="{{ next }}" />{% endif %}
{% block head %}{% endblock %} {% block head %}{% endblock %}
<link rel="authorization_endpoint" href="{{ origin }}{% url 'lemonauth:indie' %}" /> <link rel="authorization_endpoint" href="{{ origin }}{% url 'lemonauth:indie' %}" />