From cb9cb2045b6c92166f27239cd94f725afdcd63d7 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 1 Jun 2006 04:29:39 +0000 Subject: [PATCH] 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 --- django/core/paginator.py | 20 ++++++++++++++++++++ tests/modeltests/pagination/models.py | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/django/core/paginator.py b/django/core/paginator.py index 6e01c1ccec..f4941cb678 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -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() diff --git a/tests/modeltests/pagination/models.py b/tests/modeltests/pagination/models.py index fa45616b52..3667c84d69 100644 --- a/tests/modeltests/pagination/models.py +++ b/tests/modeltests/pagination/models.py @@ -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 + """