1 | class PaginatorPage(object):
|
---|
2 | """
|
---|
3 | This class is used to work with a page of a paginator object.
|
---|
4 | Pass a paginator object, and a valid page number.
|
---|
5 | It uses the same methods as a paginator, except you do not need
|
---|
6 | to use a page number, and also provides some other useful
|
---|
7 | properties.
|
---|
8 |
|
---|
9 | `page_number` is always 0-based to match the Paginator object.
|
---|
10 | `base` allows next_page_number, previous_page_number and
|
---|
11 | page_number return their numbers using a different base.
|
---|
12 |
|
---|
13 | For example:
|
---|
14 | >>> page = PaginatorPage(paginator, 0, base=1)
|
---|
15 | >>> page.next_page_number()
|
---|
16 | 2
|
---|
17 | >>> page.page_number
|
---|
18 | 1
|
---|
19 | """
|
---|
20 | def __init__(self, paginator, page_number, base=0):
|
---|
21 | self.paginator = paginator
|
---|
22 | # _page_number remains the base-0 page number
|
---|
23 | self._page_number = paginator.validate_page_number(page_number)
|
---|
24 | self.base = int(base)
|
---|
25 |
|
---|
26 | def get_page(self):
|
---|
27 | if not hasattr(self, '_page'):
|
---|
28 | self._page = self.paginator.get_page(self._page_number)
|
---|
29 | return self._page
|
---|
30 |
|
---|
31 | def has_next_page(self):
|
---|
32 | return self.paginator.has_next_page(self._page_number)
|
---|
33 |
|
---|
34 | def has_previous_page(self):
|
---|
35 | return self.paginator.has_previous_page(self._page_number)
|
---|
36 |
|
---|
37 | def first_on_page(self):
|
---|
38 | return self.paginator.first_on_page(self._page_number)
|
---|
39 |
|
---|
40 | def last_on_page(self):
|
---|
41 | return self.paginator.last_on_page(self._page_number)
|
---|
42 |
|
---|
43 | def _get_hits(self):
|
---|
44 | return self.paginator.hits
|
---|
45 | hits = property(_get_hits)
|
---|
46 |
|
---|
47 | def _get_pages(self):
|
---|
48 | return self.paginator.pages
|
---|
49 | pages = property(_get_pages)
|
---|
50 |
|
---|
51 | def _get_page_number(self):
|
---|
52 | self._page_number + base
|
---|
53 | page_number = property(_get_page_number)
|
---|
54 |
|
---|
55 | def _get_next_page_number(self):
|
---|
56 | if self.has_next_page():
|
---|
57 | # Uses page_number so that we return the correct base
|
---|
58 | return self.page_number + 1
|
---|
59 | next_page_number = property(_get_next_page_number)
|
---|
60 |
|
---|
61 | def _get_previous_page_number(self):
|
---|
62 | if self.has_previous_page():
|
---|
63 | # Uses page_number so that we return the correct base
|
---|
64 | return self.page_number - 1
|
---|
65 | previous_page_number = property(_get_previous_page_number)
|
---|