Switch from custom pagination to django.core.paginator, since it can do things like counts and 'is there a next page' more easily

This commit is contained in:
Danielle McLean 2017-12-13 09:56:18 +11:00
parent 2d2159ee58
commit 2a65644813
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 26 additions and 38 deletions

View file

@ -11,14 +11,14 @@ def prefix(route):
return app_name + ':' + route
before = '(?:/before/(?P<before>\d+))?'
page = '(?:/page/(?P<page>\d+))?'
slug = r'/(?P<slug>[^/]+)'
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<id>\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),

View file

@ -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,
}

View file

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

View file

@ -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),

View file

@ -10,7 +10,6 @@
<link rel="canonical" href="{{ uri }}" />
{% 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 next %}<link rel="next" href="{{ next }}" />{% endif %}
{% block head %}{% endblock %}
<link rel="authorization_endpoint" href="{{ origin }}{% url 'lemonauth:indie' %}" />