1 from django.core.paginator import Paginator, Page, PageNotAnInteger, EmptyPage
3 class InfinitePaginator(Paginator):
5 Paginator designed for cases when it's not important to know how many total
6 pages. This is useful for any object_list that has no count() method or can
7 be used to improve performance for MySQL by removing counts.
9 The orphans parameter has been removed for simplicity and there's a link
10 template string for creating the links to the next and previous pages.
13 def __init__(self, object_list, per_page, allow_empty_first_page=True,
14 link_template='/page/%d/'):
15 orphans = 0 # no orphans
16 super(InfinitePaginator, self).__init__(object_list, per_page, orphans,
17 allow_empty_first_page)
18 # no count or num pages
19 del self._num_pages, self._count
21 self.link_template = link_template
23 def validate_number(self, number):
25 Validates the given 1-based page number.
30 raise PageNotAnInteger('That page number is not an integer')
32 raise EmptyPage('That page number is less than 1')
35 def page(self, number):
37 Returns a Page object for the given 1-based page number.
39 number = self.validate_number(number)
40 bottom = (number - 1) * self.per_page
41 top = bottom + self.per_page
42 page_items = self.object_list[bottom:top]
43 # check moved from validate_number
45 if number == 1 and self.allow_empty_first_page:
48 raise EmptyPage('That page contains no results')
49 return InfinitePage(page_items, number, self)
53 Returns the total number of objects, across all pages.
55 raise NotImplementedError
56 count = property(_get_count)
58 def _get_num_pages(self):
60 Returns the total number of pages.
62 raise NotImplementedError
63 num_pages = property(_get_num_pages)
65 def _get_page_range(self):
67 Returns a 1-based range of pages for iterating through within
70 raise NotImplementedError
71 page_range = property(_get_page_range)
74 class InfinitePage(Page):
77 return '<Page %s>' % self.number
81 Checks for one more item than last on this page.
84 next_item = self.paginator.object_list[
85 self.number * self.paginator.per_page]
92 Returns the 1-based index of the last object on this page,
93 relative to total objects found (hits).
95 return ((self.number - 1) * self.paginator.per_page +
96 len(self.object_list))
98 #Bonus methods for creating links
102 return self.paginator.link_template % (self.number + 1)
105 def previous_link(self):
106 if self.has_previous():
107 return self.paginator.link_template % (self.number - 1)
110 class FinitePaginator(InfinitePaginator):
112 Paginator for cases when the list of items is already finite.
114 A good example is a list generated from an API call. This is a subclass
115 of InfinitePaginator because we have no idea how many items exist in the
118 To accurately determine if the next page exists, a FinitePaginator MUST be
119 created with an object_list_plus that may contain more items than the
120 per_page count. Typically, you'll have an object_list_plus with one extra
121 item (if there's a next page). You'll also need to supply the offset from
122 the full collection in order to get the page start_index.
124 This is a very silly class but useful if you love the Django pagination
128 def __init__(self, object_list_plus, per_page, offset=None,
129 allow_empty_first_page=True, link_template='/page/%d/'):
130 super(FinitePaginator, self).__init__(object_list_plus, per_page,
131 allow_empty_first_page, link_template)
134 def validate_number(self, number):
135 super(FinitePaginator, self).validate_number(number)
136 # check for an empty list to see if the page exists
137 if not self.object_list:
138 if number == 1 and self.allow_empty_first_page:
141 raise EmptyPage('That page contains no results')
144 def page(self, number):
146 Returns a Page object for the given 1-based page number.
148 number = self.validate_number(number)
149 # remove the extra item(s) when creating the page
150 page_items = self.object_list[:self.per_page]
151 return FinitePage(page_items, number, self)
153 class FinitePage(InfinitePage):
157 Checks for one more item than last on this page.
160 next_item = self.paginator.object_list[self.paginator.per_page]
165 def start_index(self):
167 Returns the 1-based index of the first object on this page,
168 relative to total objects in the paginator.
170 ## TODO should this holler if you haven't defined the offset?
171 return self.paginator.offset