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:
Adrian Holovaty 2006-06-01 04:29:39 +00:00
parent 5077f9ceaf
commit cb9cb2045b
2 changed files with 29 additions and 0 deletions

View File

@ -54,6 +54,26 @@ class ObjectPaginator:
def has_previous_page(self, page_number):
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):
if self._hits is None:
self._hits = self.query_set.count()

View File

@ -55,4 +55,13 @@ False
>>> paginator.has_previous_page(1)
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
"""