2005-07-13 09:25:57 +08:00
|
|
|
class InvalidPage(Exception):
|
|
|
|
pass
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class ObjectPaginator(object):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2006-11-07 12:44:27 +08:00
|
|
|
This class makes pagination easy. Feed it a QuerySet or list, plus the number
|
|
|
|
of objects you want on each page. Then read the hits and pages properties to
|
2006-05-02 09:31:56 +08:00
|
|
|
see how many pages it involves. Call get_page with a page number (starting
|
2005-07-13 09:25:57 +08:00
|
|
|
at 0) to get back a list of objects for that page.
|
|
|
|
|
|
|
|
Finally, check if a page number has a next/prev page using
|
|
|
|
has_next_page(page_number) and has_previous_page(page_number).
|
2006-11-07 12:44:27 +08:00
|
|
|
|
|
|
|
Use orphans to avoid small final pages. For example:
|
|
|
|
13 records, num_per_page=10, orphans=2 --> pages==2, len(self.get_page(0))==10
|
|
|
|
12 records, num_per_page=10, orphans=2 --> pages==1, len(self.get_page(0))==12
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2006-11-07 12:44:27 +08:00
|
|
|
def __init__(self, query_set, num_per_page, orphans=0):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.query_set = query_set
|
2005-07-13 09:25:57 +08:00
|
|
|
self.num_per_page = num_per_page
|
2006-11-07 12:44:27 +08:00
|
|
|
self.orphans = orphans
|
|
|
|
self._hits = self._pages = None
|
2007-09-14 09:01:02 +08:00
|
|
|
self._page_range = None
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-11-07 12:44:27 +08:00
|
|
|
def validate_page_number(self, page_number):
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
page_number = int(page_number)
|
|
|
|
except ValueError:
|
|
|
|
raise InvalidPage
|
2006-11-07 12:44:27 +08:00
|
|
|
if page_number < 0 or page_number > self.pages - 1:
|
2005-07-13 09:25:57 +08:00
|
|
|
raise InvalidPage
|
2006-11-07 12:44:27 +08:00
|
|
|
return page_number
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-11-07 12:44:27 +08:00
|
|
|
def get_page(self, page_number):
|
|
|
|
page_number = self.validate_page_number(page_number)
|
|
|
|
bottom = page_number * self.num_per_page
|
|
|
|
top = bottom + self.num_per_page
|
|
|
|
if top + self.orphans >= self.hits:
|
|
|
|
top = self.hits
|
|
|
|
return self.query_set[bottom:top]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def has_next_page(self, page_number):
|
|
|
|
"Does page $page_number have a 'next' page?"
|
2006-11-07 12:44:27 +08:00
|
|
|
return page_number < self.pages - 1
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def has_previous_page(self, page_number):
|
|
|
|
return page_number > 0
|
|
|
|
|
2006-06-01 12:29:39 +08:00
|
|
|
def first_on_page(self, page_number):
|
|
|
|
"""
|
|
|
|
Returns the 1-based index of the first object on the given page,
|
|
|
|
relative to total objects found (hits).
|
|
|
|
"""
|
2006-11-07 12:44:27 +08:00
|
|
|
page_number = self.validate_page_number(page_number)
|
2006-06-01 12:29:39 +08:00
|
|
|
return (self.num_per_page * page_number) + 1
|
|
|
|
|
|
|
|
def last_on_page(self, page_number):
|
|
|
|
"""
|
|
|
|
Returns the 1-based index of the last object on the given page,
|
|
|
|
relative to total objects found (hits).
|
|
|
|
"""
|
2006-11-07 12:44:27 +08:00
|
|
|
page_number = self.validate_page_number(page_number)
|
|
|
|
page_number += 1 # 1-base
|
|
|
|
if page_number == self.pages:
|
|
|
|
return self.hits
|
|
|
|
return page_number * self.num_per_page
|
2006-06-01 12:29:39 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def _get_hits(self):
|
|
|
|
if self._hits is None:
|
2006-11-07 12:44:27 +08:00
|
|
|
# Try .count() or fall back to len().
|
|
|
|
try:
|
|
|
|
self._hits = int(self.query_set.count())
|
|
|
|
except (AttributeError, TypeError, ValueError):
|
|
|
|
# AttributeError if query_set has no object count.
|
|
|
|
# TypeError if query_set.count() required arguments.
|
|
|
|
# ValueError if int() fails.
|
|
|
|
self._hits = len(self.query_set)
|
2005-07-13 09:25:57 +08:00
|
|
|
return self._hits
|
|
|
|
|
|
|
|
def _get_pages(self):
|
|
|
|
if self._pages is None:
|
2006-11-07 12:44:27 +08:00
|
|
|
hits = (self.hits - 1 - self.orphans)
|
|
|
|
if hits < 1:
|
|
|
|
hits = 0
|
|
|
|
self._pages = hits // self.num_per_page + 1
|
2005-07-13 09:25:57 +08:00
|
|
|
return self._pages
|
2007-09-14 09:01:02 +08:00
|
|
|
|
|
|
|
def _get_page_range(self):
|
|
|
|
"""
|
|
|
|
Returns a 1-based range of pages for iterating through within
|
|
|
|
a template for loop.
|
|
|
|
"""
|
|
|
|
if self._page_range is None:
|
2007-11-19 14:10:23 +08:00
|
|
|
self._page_range = range(1, self.pages + 1)
|
2007-09-14 09:01:02 +08:00
|
|
|
return self._page_range
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
hits = property(_get_hits)
|
|
|
|
pages = property(_get_pages)
|
2007-09-14 09:01:02 +08:00
|
|
|
page_range = property(_get_page_range)
|