forked from 00dani/lemoncurry
Refactor the pagination logic into a reusable module so I can go add it to the other feed pages
This commit is contained in:
parent
f9e6f1dde3
commit
4033837b91
3 changed files with 45 additions and 27 deletions
33
entries/pagination.py
Normal file
33
entries/pagination.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
from django.core.paginator import Paginator
|
||||||
|
from django.shortcuts import redirect
|
||||||
|
|
||||||
|
|
||||||
|
def paginate(queryset, reverse, page):
|
||||||
|
class Page:
|
||||||
|
def __init__(self, i):
|
||||||
|
self.i = i
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self):
|
||||||
|
return reverse(self.i)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current(self):
|
||||||
|
return self.i == entries.number
|
||||||
|
|
||||||
|
# If the first page was requested, redirect to the clean version of the URL
|
||||||
|
# with no page suffix.
|
||||||
|
if page == '1':
|
||||||
|
return redirect(Page(1).url)
|
||||||
|
|
||||||
|
paginator = Paginator(queryset, 10)
|
||||||
|
entries = paginator.page(page or 1)
|
||||||
|
|
||||||
|
entries.pages = tuple(Page(i) for i in paginator.page_range)
|
||||||
|
|
||||||
|
if entries.has_previous():
|
||||||
|
entries.prev = Page(entries.previous_page_number())
|
||||||
|
if entries.has_next():
|
||||||
|
entries.next = Page(entries.next_page_number())
|
||||||
|
|
||||||
|
return entries
|
|
@ -1,12 +1,11 @@
|
||||||
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, redirect
|
from django.shortcuts import get_object_or_404
|
||||||
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
|
||||||
|
|
||||||
from entries import kinds
|
from entries import kinds, pagination
|
||||||
from lemoncurry import breadcrumbs, utils
|
from lemoncurry import breadcrumbs, utils
|
||||||
|
|
||||||
breadcrumbs.add('home:index', 'home')
|
breadcrumbs.add('home:index', 'home')
|
||||||
|
@ -14,33 +13,17 @@ breadcrumbs.add('home:index', 'home')
|
||||||
|
|
||||||
@render_to('home/index.html')
|
@render_to('home/index.html')
|
||||||
def index(request, page):
|
def index(request, page):
|
||||||
|
def url(page):
|
||||||
|
kwargs = {'page': page} if page != 1 else {}
|
||||||
|
return reverse('home:index', kwargs=kwargs)
|
||||||
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)
|
||||||
paginator = Paginator(user.entries.filter(kind__in=kinds.on_home), 10)
|
entries = user.entries.filter(kind__in=kinds.on_home)
|
||||||
|
|
||||||
# If we explicitly got /page/1 in the URL then redirect to the version with
|
entries = pagination.paginate(queryset=entries, reverse=url, page=page)
|
||||||
# no page suffix.
|
# If we got a valid HTTP response, just return it without rendering.
|
||||||
if page == '1':
|
if hasattr(entries, 'content'):
|
||||||
return redirect('home:index', permanent=True)
|
return entries
|
||||||
entries = paginator.page(page or 1)
|
|
||||||
|
|
||||||
class Page:
|
|
||||||
def __init__(self, i):
|
|
||||||
self.i = i
|
|
||||||
|
|
||||||
@property
|
|
||||||
def url(self):
|
|
||||||
return reverse('home:index', kwargs={'page': self.i})
|
|
||||||
|
|
||||||
@property
|
|
||||||
def current(self):
|
|
||||||
return self.i == entries.number
|
|
||||||
|
|
||||||
entries.pages = tuple(Page(i) for i in paginator.page_range)
|
|
||||||
if entries.has_previous():
|
|
||||||
entries.prev = Page(entries.previous_page_number())
|
|
||||||
if entries.has_next():
|
|
||||||
entries.next = Page(entries.next_page_number())
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'user': user,
|
'user': user,
|
||||||
|
|
|
@ -67,6 +67,7 @@
|
||||||
<footer>
|
<footer>
|
||||||
<p>all content licensed under <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">cc by-sa 4.0</a></p>
|
<p>all content licensed under <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">cc by-sa 4.0</a></p>
|
||||||
|
|
||||||
|
{% if entries.has_other_pages %}
|
||||||
<nav>
|
<nav>
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
{% if entries.prev %}
|
{% if entries.prev %}
|
||||||
|
@ -98,6 +99,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% get_package_json as package %}
|
{% get_package_json as package %}
|
||||||
<p>powered by <a rel="code-repository" href="{{ package.repository }}/tree/v{{ package.version }}">{{ package.name }} {{ package.version }}</a></p>
|
<p>powered by <a rel="code-repository" href="{{ package.repository }}/tree/v{{ package.version }}">{{ package.name }} {{ package.version }}</a></p>
|
||||||
|
|
Loading…
Reference in a new issue