Fixed #720 -- Added first_on_page() and last_on_page() methods to ObjectPaginator. Thanks, m@bagai.com
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3040 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5077f9ceaf
commit
cb9cb2045b
|
@ -54,6 +54,26 @@ class ObjectPaginator:
|
||||||
def has_previous_page(self, page_number):
|
def has_previous_page(self, page_number):
|
||||||
return page_number > 0
|
return page_number > 0
|
||||||
|
|
||||||
|
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).
|
||||||
|
"""
|
||||||
|
if page_number == 0:
|
||||||
|
return 1
|
||||||
|
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).
|
||||||
|
"""
|
||||||
|
if page_number == 0 and self.num_per_page >= self._hits:
|
||||||
|
return self._hits
|
||||||
|
elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits:
|
||||||
|
return self._hits
|
||||||
|
return (page_number + 1) * self.num_per_page
|
||||||
|
|
||||||
def _get_hits(self):
|
def _get_hits(self):
|
||||||
if self._hits is None:
|
if self._hits is None:
|
||||||
self._hits = self.query_set.count()
|
self._hits = self.query_set.count()
|
||||||
|
|
|
@ -55,4 +55,13 @@ False
|
||||||
>>> paginator.has_previous_page(1)
|
>>> paginator.has_previous_page(1)
|
||||||
True
|
True
|
||||||
|
|
||||||
|
>>> paginator.first_on_page(0)
|
||||||
|
1
|
||||||
|
>>> paginator.first_on_page(1)
|
||||||
|
6
|
||||||
|
>>> paginator.last_on_page(0)
|
||||||
|
5
|
||||||
|
>>> paginator.last_on_page(1)
|
||||||
|
9
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue