2017-12-14 20:25:55 -05:00
|
|
|
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.
|
2018-05-03 22:13:52 -04:00
|
|
|
if page == 1:
|
2017-12-14 20:25:55 -05:00
|
|
|
return redirect(Page(1).url)
|
|
|
|
|
2017-12-14 20:34:36 -05:00
|
|
|
paginator = Paginator(queryset, 10)
|
2017-12-14 20:25:55 -05:00
|
|
|
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
|