summaryrefslogtreecommitdiff
path: root/django/core/paginator.py
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2008-08-05 17:15:33 +0000
committerJustin Bronn <jbronn@gmail.com>2008-08-05 17:15:33 +0000
commitaa239e3e5405933af6a29dac3cf587b59a099927 (patch)
treeea2cbd139c9a8cf84c09e0b2008bff70e05927ef /django/core/paginator.py
parent45b73c9a4685809236f84046cc7ffd32a50db958 (diff)
downloaddjango-attic/gis.tar.gz
gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.archive/attic/gisattic/gis
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8215 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/paginator.py')
-rw-r--r--django/core/paginator.py98
1 files changed, 14 insertions, 84 deletions
diff --git a/django/core/paginator.py b/django/core/paginator.py
index 439b0bc717..495cdf2d76 100644
--- a/django/core/paginator.py
+++ b/django/core/paginator.py
@@ -1,3 +1,5 @@
+from math import ceil
+
class InvalidPage(Exception):
pass
@@ -42,10 +44,12 @@ class Paginator(object):
def _get_count(self):
"Returns the total number of objects, across all pages."
if self._count is None:
- from django.db.models.query import QuerySet
- if isinstance(self.object_list, QuerySet):
+ try:
self._count = self.object_list.count()
- else:
+ except (AttributeError, TypeError):
+ # AttributeError if object_list has no count() method.
+ # TypeError if object_list.count() requires arguments
+ # (i.e. is of type list).
self._count = len(self.object_list)
return self._count
count = property(_get_count)
@@ -53,13 +57,11 @@ class Paginator(object):
def _get_num_pages(self):
"Returns the total number of pages."
if self._num_pages is None:
- hits = self.count - 1 - self.orphans
- if hits < 1:
- hits = 0
- if hits == 0 and not self.allow_empty_first_page:
+ if self.count == 0 and not self.allow_empty_first_page:
self._num_pages = 0
else:
- self._num_pages = hits // self.per_page + 1
+ hits = max(1, self.count - self.orphans)
+ self._num_pages = int(ceil(hits / float(self.per_page)))
return self._num_pages
num_pages = property(_get_num_pages)
@@ -102,6 +104,9 @@ class Page(object):
Returns the 1-based index of the first object on this page,
relative to total objects in the paginator.
"""
+ # Special case, return zero if no items.
+ if self.paginator.count == 0:
+ return 0
return (self.paginator.per_page * (self.number - 1)) + 1
def end_index(self):
@@ -109,82 +114,7 @@ class Page(object):
Returns the 1-based index of the last object on this page,
relative to total objects found (hits).
"""
+ # Special case for the last page because there can be orphans.
if self.number == self.paginator.num_pages:
return self.paginator.count
return self.number * self.paginator.per_page
-
-class ObjectPaginator(Paginator):
- """
- Legacy ObjectPaginator class, for backwards compatibility.
-
- Note that each method on this class that takes page_number expects a
- zero-based page number, whereas the new API (Paginator/Page) uses one-based
- page numbers.
- """
- def __init__(self, query_set, num_per_page, orphans=0):
- Paginator.__init__(self, query_set, num_per_page, orphans)
- import warnings
- warnings.warn("The ObjectPaginator is deprecated. Use django.core.paginator.Paginator instead.", DeprecationWarning)
-
- # Keep these attributes around for backwards compatibility.
- self.query_set = query_set
- self.num_per_page = num_per_page
- self._hits = self._pages = None
-
- def validate_page_number(self, page_number):
- try:
- page_number = int(page_number) + 1
- except ValueError:
- raise PageNotAnInteger
- return self.validate_number(page_number)
-
- def get_page(self, page_number):
- try:
- page_number = int(page_number) + 1
- except ValueError:
- raise PageNotAnInteger
- return self.page(page_number).object_list
-
- def has_next_page(self, page_number):
- return page_number < self.pages - 1
-
- 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).
- """
- page_number = self.validate_page_number(page_number)
- return (self.num_per_page * (page_number - 1)) + 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).
- """
- page_number = self.validate_page_number(page_number)
- if page_number == self.num_pages:
- return self.count
- return page_number * self.num_per_page
-
- def _get_count(self):
- # The old API allowed for self.object_list to be either a QuerySet or a
- # list. Here, we handle both.
- if self._count is None:
- try:
- self._count = self.object_list.count()
- except (AttributeError, TypeError):
- # AttributeError if object_list has no count() method.
- # TypeError if object_list.count() requires arguments
- # (i.e. is of type list).
- self._count = len(self.object_list)
- return self._count
- count = property(_get_count)
-
- # The old API called it "hits" instead of "count".
- hits = count
-
- # The old API called it "pages" instead of "num_pages".
- pages = Paginator.num_pages