Ticket #4805: 4805-alt.diff

File 4805-alt.diff, 2.3 KB (added by nick.lane.au@…, 12 years ago)

Alternate implementation based on #2576

  • django/core/paginator.py

    === modified file 'django/core/paginator.py'
     
    8686
    8787    hits = property(_get_hits)
    8888    pages = property(_get_pages)
     89
     90class ObjectPaginatorForPage(ObjectPaginator):
     91    """
     92    This class is an extension of the ObjectPaginator class with an embedded
     93    current page number, which is useful in templates.
     94
     95    When creating an ``ObjectPaginatorForPage`` object you can also pass it a
     96    ``page_number`` argument to specify which page number the paginator should
     97    use. ``page_number`` should use a zero-based index and defaults to ``0``
     98    (the first page).
     99
     100    You can also pass it a ``base`` argument, which determines if the
     101    ``page_number``, ``next_page_number`` and ``previous_page_number``
     102    properties use zero or one based page indexing. ``base`` defaults to
     103    ``1``.
     104    """
     105    def __init__(self, query_set, num_per_page, orphans=0, page_number=0, base=1):
     106        super(ObjectPaginatorForPage, self).__init__(query_set, num_per_page, orphans)
     107        self._page_number = page_number
     108        self.base = base
     109
     110    def validate_page_number(self):
     111        return super(ObjectPaginatorForPage, self).validate_page_number(self._page_number)
     112
     113    def get_page(self):
     114        return super(ObjectPaginatorForPage, self).get_page(self._page_number)
     115
     116    def has_next_page(self):
     117        return super(ObjectPaginatorForPage, self).has_next_page(self._page_number)
     118
     119    def has_previous_page(self):
     120        return super(ObjectPaginatorForPage, self).has_previous_page(self._page_number)
     121
     122    def first_on_page(self):
     123        return super(ObjectPaginatorForPage, self).first_on_page(self._page_number)
     124
     125    def last_on_page(self):
     126        return super(ObjectPaginatorForPage, self).last_on_page(self._page_number)
     127
     128    def _get_page_number(self):
     129        return base + self._page_number
     130    page_number = property(_get_page_number)
     131
     132    def _get_next_page_number(self):
     133        if self.has_next_page():
     134            return self.page_number + 1
     135    next_page_number = property(_get_next_page_number)
     136
     137    def _get_previous_page_number(self):
     138        if self.has_previous_page():
     139            return self.page_number - 1
     140    previous_page_number = property(_get_previous_page_number)
Back to Top