diff options
| author | Chris Beaven <smileychris@gmail.com> | 2012-11-21 14:58:14 +1300 |
|---|---|---|
| committer | Chris Beaven <smileychris@gmail.com> | 2012-11-21 15:19:44 +1300 |
| commit | fbfa654a15487f9a49e9a5d34a4e9694fe878a8f (patch) | |
| tree | 6c922e07069e20efc75344804f47bd71f30d26b0 /django | |
| parent | 8fdb28219df22245d7e50b4e8374d8567ca84f0f (diff) | |
Paginator._get_page hook
This allows for Paginator subclasses to easily override the Page class
that gets used.
Took the opportunity to also do some non-invasive PEP8 tidying of the
paginator module.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/paginator.py | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/django/core/paginator.py b/django/core/paginator.py index d22e16c452..9ccff51a34 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -7,14 +7,19 @@ from django.utils import six class InvalidPage(Exception): pass + class PageNotAnInteger(InvalidPage): pass + class EmptyPage(InvalidPage): pass + class Paginator(object): - def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True): + + def __init__(self, object_list, per_page, orphans=0, + allow_empty_first_page=True): self.object_list = object_list self.per_page = int(per_page) self.orphans = int(orphans) @@ -22,7 +27,9 @@ class Paginator(object): self._num_pages = self._count = None def validate_number(self, number): - "Validates the given 1-based page number." + """ + Validates the given 1-based page number. + """ try: number = int(number) except (TypeError, ValueError): @@ -37,16 +44,29 @@ class Paginator(object): return number def page(self, number): - "Returns a Page object for the given 1-based page number." + """ + Returns a Page object for the given 1-based page number. + """ number = self.validate_number(number) bottom = (number - 1) * self.per_page top = bottom + self.per_page if top + self.orphans >= self.count: top = self.count - return Page(self.object_list[bottom:top], number, self) + return self._get_page(self.object_list[bottom:top], number, self) + + def _get_page(self, *args, **kwargs): + """ + Returns an instance of a single page. + + This hook can be used by subclasses to use an alternative to the + standard :cls:`Page` object. + """ + return Page(*args, **kwargs) def _get_count(self): - "Returns the total number of objects, across all pages." + """ + Returns the total number of objects, across all pages. + """ if self._count is None: try: self._count = self.object_list.count() @@ -59,7 +79,9 @@ class Paginator(object): count = property(_get_count) def _get_num_pages(self): - "Returns the total number of pages." + """ + Returns the total number of pages. + """ if self._num_pages is None: if self.count == 0 and not self.allow_empty_first_page: self._num_pages = 0 @@ -77,9 +99,12 @@ class Paginator(object): return range(1, self.num_pages + 1) page_range = property(_get_page_range) -QuerySetPaginator = Paginator # For backwards-compatibility. + +QuerySetPaginator = Paginator # For backwards-compatibility. + class Page(collections.Sequence): + def __init__(self, object_list, number, paginator): self.object_list = object_list self.number = number |
