forked from 00dani/lemoncurry
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:
parent
2d2159ee58
commit
2a65644813
5 changed files with 26 additions and 38 deletions
|
@ -11,14 +11,14 @@ def prefix(route):
|
||||||
return app_name + ':' + route
|
return app_name + ':' + route
|
||||||
|
|
||||||
|
|
||||||
before = '(?:/before/(?P<before>\d+))?'
|
page = '(?:/page/(?P<page>\d+))?'
|
||||||
slug = r'/(?P<slug>[^/]+)'
|
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(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')
|
crumbs.add(prefix('cat'), parent='home:index')
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ for k in kinds.all:
|
||||||
kind = k.plural
|
kind = k.plural
|
||||||
id = r'/(?P<id>\d+)'
|
id = r'/(?P<id>\d+)'
|
||||||
urlpatterns += (
|
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, '/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),
|
||||||
|
|
|
@ -1,24 +1,21 @@
|
||||||
from annoying.decorators import render_to
|
from annoying.decorators import render_to
|
||||||
|
from django.core.paginator import Paginator
|
||||||
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, before=None):
|
def index(request, kind, page):
|
||||||
entries = Entry.objects.filter(kind=kind.id)
|
paginator = Paginator(Entry.objects.filter(kind=kind.id), 10)
|
||||||
if before:
|
|
||||||
entries = entries.filter(id__lt=before)
|
|
||||||
entries = entries[:10]
|
|
||||||
|
|
||||||
next = None
|
# If we explicitly got /page/1 in the URL then redirect to the version with
|
||||||
if entries:
|
# no page suffix.
|
||||||
last = entries.last().id
|
if page == '1':
|
||||||
next = reverse('entries:' + kind.index, kwargs={'before': last})
|
return redirect('entries:' + kind.index, permanent=True)
|
||||||
|
entries = paginator.page(page or 1)
|
||||||
|
|
||||||
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,
|
||||||
|
@ -26,22 +23,15 @@ def index(request, kind, before=None):
|
||||||
|
|
||||||
|
|
||||||
@render_to('entries/index.html')
|
@render_to('entries/index.html')
|
||||||
def cat(request, slug, before=None):
|
def cat(request, slug, page):
|
||||||
cat = get_object_or_404(Cat, slug=slug)
|
cat = get_object_or_404(Cat, slug=slug)
|
||||||
entries = cat.entries.all()
|
paginator = Paginator(cat.entries.all(), 10)
|
||||||
if before:
|
if page == '1':
|
||||||
entries = entries.filter(id__lt=before)
|
return redirect('entries:cat', permanent=True, slug=slug)
|
||||||
entries = entries[:10]
|
entries = paginator.page(page or 1)
|
||||||
|
|
||||||
next = None
|
|
||||||
if entries:
|
|
||||||
next = reverse('entries:cat', kwargs={
|
|
||||||
'slug': slug,
|
|
||||||
'before': entries.last().id
|
|
||||||
})
|
|
||||||
return {
|
return {
|
||||||
'entries': entries,
|
'entries': entries,
|
||||||
'next': next,
|
|
||||||
'title': '#' + cat.name,
|
'title': '#' + cat.name,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,6 @@ from . import views
|
||||||
|
|
||||||
app_name = 'home'
|
app_name = 'home'
|
||||||
urlpatterns = [
|
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'),
|
url(r'^robots.txt$', views.robots, name='robots.txt'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from annoying.decorators import render_to
|
from annoying.decorators import render_to
|
||||||
|
from django.core.paginator import Paginator
|
||||||
from django.http import HttpResponse
|
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 django.urls import reverse
|
||||||
from users.models import User
|
from users.models import User
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
@ -12,22 +13,20 @@ breadcrumbs.add('home:index', 'home')
|
||||||
|
|
||||||
|
|
||||||
@render_to('home/index.html')
|
@render_to('home/index.html')
|
||||||
def index(request, before=None):
|
def index(request, page):
|
||||||
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)
|
paginator = Paginator(user.entries.filter(kind__in=kinds.on_home), 10)
|
||||||
if before:
|
|
||||||
entries = entries.filter(id__lt=before)
|
|
||||||
entries = entries[:10]
|
|
||||||
|
|
||||||
next = None
|
# If we explicitly got /page/1 in the URL then redirect to the version with
|
||||||
if entries:
|
# no page suffix.
|
||||||
next = reverse('home:index', kwargs={'before': entries.last().id})
|
if page == '1':
|
||||||
|
return redirect('home:index', permanent=True)
|
||||||
|
entries = paginator.page(page or 1)
|
||||||
|
|
||||||
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),
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
<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' %}" />
|
||||||
|
|
Loading…
Reference in a new issue