2018-02-06 00:42:47 +08:00
|
|
|
import collections.abc
|
2018-08-07 11:13:07 +08:00
|
|
|
import inspect
|
2016-06-07 19:24:19 +08:00
|
|
|
import warnings
|
2008-07-28 13:30:35 +08:00
|
|
|
from math import ceil
|
|
|
|
|
2018-08-23 02:40:14 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango31Warning
|
2016-04-15 22:54:38 +08:00
|
|
|
from django.utils.functional import cached_property
|
2018-08-07 11:13:07 +08:00
|
|
|
from django.utils.inspect import method_has_no_args
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2012-11-10 02:37:50 +08:00
|
|
|
|
|
|
|
|
2016-06-07 19:24:19 +08:00
|
|
|
class UnorderedObjectListWarning(RuntimeWarning):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
class InvalidPage(Exception):
|
|
|
|
pass
|
|
|
|
|
2012-11-21 09:58:14 +08:00
|
|
|
|
2008-07-08 10:20:48 +08:00
|
|
|
class PageNotAnInteger(InvalidPage):
|
|
|
|
pass
|
|
|
|
|
2012-11-21 09:58:14 +08:00
|
|
|
|
2008-07-08 10:20:48 +08:00
|
|
|
class EmptyPage(InvalidPage):
|
|
|
|
pass
|
|
|
|
|
2012-11-21 09:58:14 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Paginator:
|
2012-11-21 09:58:14 +08:00
|
|
|
|
|
|
|
def __init__(self, object_list, per_page, orphans=0,
|
|
|
|
allow_empty_first_page=True):
|
2008-03-19 05:13:48 +08:00
|
|
|
self.object_list = object_list
|
2016-06-07 19:24:19 +08:00
|
|
|
self._check_object_list_is_ordered()
|
2011-04-22 20:03:03 +08:00
|
|
|
self.per_page = int(per_page)
|
|
|
|
self.orphans = int(orphans)
|
2008-03-19 05:13:48 +08:00
|
|
|
self.allow_empty_first_page = allow_empty_first_page
|
|
|
|
|
|
|
|
def validate_number(self, number):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Validate the given 1-based page number."""
|
2008-03-19 05:13:48 +08:00
|
|
|
try:
|
2018-05-09 18:25:39 +08:00
|
|
|
if isinstance(number, float) and not number.is_integer():
|
|
|
|
raise ValueError
|
2008-03-19 05:13:48 +08:00
|
|
|
number = int(number)
|
2011-04-13 19:28:42 +08:00
|
|
|
except (TypeError, ValueError):
|
2016-12-01 21:39:48 +08:00
|
|
|
raise PageNotAnInteger(_('That page number is not an integer'))
|
2008-03-19 05:13:48 +08:00
|
|
|
if number < 1:
|
2016-12-01 21:39:48 +08:00
|
|
|
raise EmptyPage(_('That page number is less than 1'))
|
2008-03-19 05:13:48 +08:00
|
|
|
if number > self.num_pages:
|
|
|
|
if number == 1 and self.allow_empty_first_page:
|
|
|
|
pass
|
|
|
|
else:
|
2016-12-01 21:39:48 +08:00
|
|
|
raise EmptyPage(_('That page contains no results'))
|
2008-03-19 05:13:48 +08:00
|
|
|
return number
|
|
|
|
|
2017-04-06 18:01:04 +08:00
|
|
|
def get_page(self, number):
|
|
|
|
"""
|
|
|
|
Return a valid page, even if the page argument isn't a number or isn't
|
|
|
|
in range.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
number = self.validate_number(number)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
number = 1
|
|
|
|
except EmptyPage:
|
|
|
|
number = self.num_pages
|
|
|
|
return self.page(number)
|
|
|
|
|
2008-03-19 05:13:48 +08:00
|
|
|
def page(self, number):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Return a Page object for the given 1-based page number."""
|
2008-03-19 05:13:48 +08:00
|
|
|
number = self.validate_number(number)
|
|
|
|
bottom = (number - 1) * self.per_page
|
|
|
|
top = bottom + self.per_page
|
|
|
|
if top + self.orphans >= self.count:
|
|
|
|
top = self.count
|
2012-11-21 09:58:14 +08:00
|
|
|
return self._get_page(self.object_list[bottom:top], number, self)
|
|
|
|
|
|
|
|
def _get_page(self, *args, **kwargs):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return an instance of a single page.
|
2012-11-21 09:58:14 +08:00
|
|
|
|
|
|
|
This hook can be used by subclasses to use an alternative to the
|
|
|
|
standard :cls:`Page` object.
|
|
|
|
"""
|
|
|
|
return Page(*args, **kwargs)
|
2008-03-19 05:13:48 +08:00
|
|
|
|
2016-04-15 22:54:38 +08:00
|
|
|
@cached_property
|
|
|
|
def count(self):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Return the total number of objects, across all pages."""
|
2018-08-07 11:13:07 +08:00
|
|
|
c = getattr(self.object_list, 'count', None)
|
|
|
|
if callable(c) and not inspect.isbuiltin(c) and method_has_no_args(c):
|
|
|
|
return c()
|
|
|
|
return len(self.object_list)
|
2016-04-15 22:54:38 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def num_pages(self):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Return the total number of pages."""
|
2016-04-15 22:54:38 +08:00
|
|
|
if self.count == 0 and not self.allow_empty_first_page:
|
|
|
|
return 0
|
|
|
|
hits = max(1, self.count - self.orphans)
|
2017-10-02 20:49:26 +08:00
|
|
|
return ceil(hits / self.per_page)
|
2008-03-19 05:13:48 +08:00
|
|
|
|
2016-04-15 22:54:38 +08:00
|
|
|
@property
|
|
|
|
def page_range(self):
|
2008-03-19 05:13:48 +08:00
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return a 1-based range of pages for iterating through within
|
2008-03-19 05:13:48 +08:00
|
|
|
a template for loop.
|
|
|
|
"""
|
2017-01-07 19:11:46 +08:00
|
|
|
return range(1, self.num_pages + 1)
|
2008-03-19 05:13:48 +08:00
|
|
|
|
2016-06-07 19:24:19 +08:00
|
|
|
def _check_object_list_is_ordered(self):
|
|
|
|
"""
|
|
|
|
Warn if self.object_list is unordered (typically a QuerySet).
|
|
|
|
"""
|
2017-06-09 00:29:13 +08:00
|
|
|
ordered = getattr(self.object_list, 'ordered', None)
|
|
|
|
if ordered is not None and not ordered:
|
|
|
|
obj_list_repr = (
|
|
|
|
'{} {}'.format(self.object_list.model, self.object_list.__class__.__name__)
|
|
|
|
if hasattr(self.object_list, 'model')
|
|
|
|
else '{!r}'.format(self.object_list)
|
|
|
|
)
|
2016-06-07 19:24:19 +08:00
|
|
|
warnings.warn(
|
|
|
|
'Pagination may yield inconsistent results with an unordered '
|
2017-06-09 00:29:13 +08:00
|
|
|
'object_list: {}.'.format(obj_list_repr),
|
2017-04-21 10:44:15 +08:00
|
|
|
UnorderedObjectListWarning,
|
|
|
|
stacklevel=3
|
2016-06-07 19:24:19 +08:00
|
|
|
)
|
|
|
|
|
2012-11-21 09:58:14 +08:00
|
|
|
|
2018-08-23 02:40:14 +08:00
|
|
|
class QuerySetPaginator(Paginator):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn(
|
|
|
|
'The QuerySetPaginator alias of Paginator is deprecated.',
|
|
|
|
RemovedInDjango31Warning, stacklevel=2,
|
|
|
|
)
|
|
|
|
super().__init__(*args, **kwargs)
|
2012-11-21 09:58:14 +08:00
|
|
|
|
2008-03-19 05:13:48 +08:00
|
|
|
|
2018-02-06 00:42:47 +08:00
|
|
|
class Page(collections.abc.Sequence):
|
2012-11-21 09:58:14 +08:00
|
|
|
|
2008-03-19 05:13:48 +08:00
|
|
|
def __init__(self, object_list, number, paginator):
|
|
|
|
self.object_list = object_list
|
|
|
|
self.number = number
|
|
|
|
self.paginator = paginator
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Page %s of %s>' % (self.number, self.paginator.num_pages)
|
|
|
|
|
2011-04-06 17:40:26 +08:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.object_list)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2016-12-29 23:27:49 +08:00
|
|
|
if not isinstance(index, (int, slice)):
|
2012-11-10 02:37:50 +08:00
|
|
|
raise TypeError
|
2011-04-06 17:40:26 +08:00
|
|
|
# The object_list is converted to a list so that if it was a QuerySet
|
|
|
|
# it won't be a database hit per __getitem__.
|
2013-03-19 04:29:37 +08:00
|
|
|
if not isinstance(self.object_list, list):
|
|
|
|
self.object_list = list(self.object_list)
|
|
|
|
return self.object_list[index]
|
2011-04-06 17:40:26 +08:00
|
|
|
|
2008-03-19 05:13:48 +08:00
|
|
|
def has_next(self):
|
|
|
|
return self.number < self.paginator.num_pages
|
|
|
|
|
|
|
|
def has_previous(self):
|
|
|
|
return self.number > 1
|
|
|
|
|
|
|
|
def has_other_pages(self):
|
|
|
|
return self.has_previous() or self.has_next()
|
|
|
|
|
|
|
|
def next_page_number(self):
|
2012-06-09 23:55:24 +08:00
|
|
|
return self.paginator.validate_number(self.number + 1)
|
2008-03-19 05:13:48 +08:00
|
|
|
|
|
|
|
def previous_page_number(self):
|
2012-06-09 23:55:24 +08:00
|
|
|
return self.paginator.validate_number(self.number - 1)
|
2008-03-19 05:13:48 +08:00
|
|
|
|
|
|
|
def start_index(self):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return the 1-based index of the first object on this page,
|
2008-03-19 05:13:48 +08:00
|
|
|
relative to total objects in the paginator.
|
|
|
|
"""
|
2008-07-28 13:30:35 +08:00
|
|
|
# Special case, return zero if no items.
|
|
|
|
if self.paginator.count == 0:
|
|
|
|
return 0
|
2008-03-19 05:13:48 +08:00
|
|
|
return (self.paginator.per_page * (self.number - 1)) + 1
|
|
|
|
|
|
|
|
def end_index(self):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return the 1-based index of the last object on this page,
|
2008-03-19 05:13:48 +08:00
|
|
|
relative to total objects found (hits).
|
|
|
|
"""
|
2008-07-28 13:30:35 +08:00
|
|
|
# Special case for the last page because there can be orphans.
|
2008-03-19 05:13:48 +08:00
|
|
|
if self.number == self.paginator.num_pages:
|
|
|
|
return self.paginator.count
|
|
|
|
return self.number * self.paginator.per_page
|