1 # Copyright (c) 2008, Eric Florenzano
2 # Copyright (c) 2010, 2011 Linaro Limited
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials provided
14 # with the distribution.
15 # * Neither the name of the author nor the names of other
16 # contributors may be used to endorse or promote products derived
17 # from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 from django.core.paginator import Paginator, Page, PageNotAnInteger, EmptyPage
35 class InfinitePaginator(Paginator):
37 Paginator designed for cases when it's not important to know how many total
38 pages. This is useful for any object_list that has no count() method or
39 can be used to improve performance for MySQL by removing counts.
41 The orphans parameter has been removed for simplicity and there's a link
42 template string for creating the links to the next and previous pages.
45 def __init__(self, object_list, per_page, allow_empty_first_page=True,
46 link_template='/page/%d/'):
47 orphans = 0 # no orphans
48 super(InfinitePaginator, self).__init__(object_list, per_page, orphans,
49 allow_empty_first_page)
50 # no count or num pages
52 del self._num_pages, self._count
53 except AttributeError:
54 # These are just cached properties anyway.
57 self.link_template = link_template
59 def validate_number(self, number):
61 Validates the given 1-based page number.
66 raise PageNotAnInteger('That page number is not an integer')
68 raise EmptyPage('That page number is less than 1')
71 def page(self, number):
73 Returns a Page object for the given 1-based page number.
75 number = self.validate_number(number)
76 bottom = (number - 1) * self.per_page
77 top = bottom + self.per_page
78 page_items = self.object_list[bottom:top]
79 # check moved from validate_number
81 if number == 1 and self.allow_empty_first_page:
84 raise EmptyPage('That page contains no results')
85 return InfinitePage(page_items, number, self)
89 Returns the total number of objects, across all pages.
91 raise NotImplementedError
92 count = property(_get_count)
94 def _get_num_pages(self):
96 Returns the total number of pages.
98 raise NotImplementedError
99 num_pages = property(_get_num_pages)
101 def _get_page_range(self):
103 Returns a 1-based range of pages for iterating through within
106 raise NotImplementedError
107 page_range = property(_get_page_range)
110 class InfinitePage(Page):
113 return '<Page %s>' % self.number
117 Checks for one more item than last on this page.
120 self.paginator.object_list[self.number * self.paginator.per_page]
127 Returns the 1-based index of the last object on this page,
128 relative to total objects found (hits).
130 return ((self.number - 1) * self.paginator.per_page +
131 len(self.object_list))
133 #Bonus methods for creating links
137 return self.paginator.link_template % (self.number + 1)
140 def previous_link(self):
141 if self.has_previous():
142 return self.paginator.link_template % (self.number - 1)
146 class FinitePaginator(InfinitePaginator):
148 Paginator for cases when the list of items is already finite.
150 A good example is a list generated from an API call. This is a subclass
151 of InfinitePaginator because we have no idea how many items exist in the
154 To accurately determine if the next page exists, a FinitePaginator MUST be
155 created with an object_list_plus that may contain more items than the
156 per_page count. Typically, you'll have an object_list_plus with one extra
157 item (if there's a next page). You'll also need to supply the offset from
158 the full collection in order to get the page start_index.
160 This is a very silly class but useful if you love the Django pagination
164 def __init__(self, object_list_plus, per_page, offset=None,
165 allow_empty_first_page=True, link_template='/page/%d/'):
166 super(FinitePaginator, self).__init__(object_list_plus, per_page,
167 allow_empty_first_page, link_template)
170 def validate_number(self, number):
171 super(FinitePaginator, self).validate_number(number)
172 # check for an empty list to see if the page exists
173 if not self.object_list:
174 if number == 1 and self.allow_empty_first_page:
177 raise EmptyPage('That page contains no results')
180 def page(self, number):
182 Returns a Page object for the given 1-based page number.
184 number = self.validate_number(number)
185 # remove the extra item(s) when creating the page
186 page_items = self.object_list[:self.per_page]
187 return FinitePage(page_items, number, self)
190 class FinitePage(InfinitePage):
194 Checks for one more item than last on this page.
197 self.paginator.object_list[self.paginator.per_page]
202 def start_index(self):
204 Returns the 1-based index of the first object on this page,
205 relative to total objects in the paginator.
207 ## TODO should this holler if you haven't defined the offset?
208 return self.paginator.offset