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.
33 >>> from django.core.paginator import Paginator
34 >>> from linaro_django_pagination.templatetags.pagination_tags import paginate
35 >>> from django.template import Template, Context
37 >>> p = Paginator(range(15), 2)
38 >>> pg = paginate({'paginator': p, 'page_obj': p.page(1)})
40 [1, 2, 3, 4, 5, 6, 7, 8]
41 >>> pg['records']['first']
43 >>> pg['records']['last']
46 >>> p = Paginator(range(15), 2)
47 >>> pg = paginate({'paginator': p, 'page_obj': p.page(8)})
49 [1, 2, 3, 4, 5, 6, 7, 8]
50 >>> pg['records']['first']
52 >>> pg['records']['last']
55 >>> p = Paginator(range(17), 2)
56 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
57 [1, 2, 3, 4, 5, 6, 7, 8, 9]
61 # moving the window from 1 ... to end
62 # window size = 2, margin = 2
63 # [1] 2 3 4 5 ... 15, 16
64 # 1 [2] 3 4 5 ... 15, 16
65 # 1 2 [3] 4 5 ... 15, 16
66 # 1 2 3 [4] 5 6 ... 15, 16
67 # 1 2 3 4 [5] 6 7 ... 15, 16
68 # 1 2 3 4 5 [6] 7 8 ... 15, 16
69 # 1 2 ... 5 6 [7] 8 9 ... 15, 16
71 # window = 2 -> show 5 pages
72 >>> p = Paginator(range(31), 2)
73 >>> paginate({'paginator': p, 'page_obj': p.page(1)}, 2, 2)['pages']
74 [1, 2, 3, 4, 5, None, 15, 16]
76 >>> p = Paginator(range(31), 2)
77 >>> paginate({'paginator': p, 'page_obj': p.page(2)}, 2, 2)['pages']
78 [1, 2, 3, 4, 5, None, 15, 16]
80 >>> p = Paginator(range(31), 2)
81 >>> paginate({'paginator': p, 'page_obj': p.page(3)}, 2, 2)['pages']
82 [1, 2, 3, 4, 5, None, 15, 16]
84 >>> p = Paginator(range(31), 2)
85 >>> paginate({'paginator': p, 'page_obj': p.page(4)}, 2, 2)['pages']
86 [1, 2, 3, 4, 5, 6, None, 15, 16]
88 >>> p = Paginator(range(31), 2)
89 >>> paginate({'paginator': p, 'page_obj': p.page(5)}, 2, 2)['pages']
90 [1, 2, 3, 4, 5, 6, 7, None, 15, 16]
93 >>> p = Paginator(range(31), 2)
94 >>> paginate({'paginator': p, 'page_obj': p.page(7)}, 2, 2)['pages']
95 [1, 2, None, 5, 6, 7, 8, 9, None, 15, 16]
98 >>> p = Paginator(range(31), 2)
99 >>> paginate({'paginator': p, 'page_obj': p.page(16)}, 2, 2)['pages']
100 [1, 2, None, 12, 13, 14, 15, 16]
102 >>> p = Paginator(range(31), 2)
103 >>> paginate({'paginator': p, 'page_obj': p.page(13)}, 2, 2)['pages']
104 [1, 2, None, 11, 12, 13, 14, 15, 16]
107 >>> p = Paginator(range(0), 2)
108 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
114 >>> p = Paginator(range(31), 2)
115 >>> paginate({'paginator': p, 'page_obj': p.page(3)}, 2, 0)['pages']
116 [1, 2, 3, 4, 5, None]
118 >>> p = Paginator(range(31), 2)
119 >>> paginate({'paginator': p, 'page_obj': p.page(5)}, 2, 0)['pages']
120 [None, 3, 4, 5, 6, 7, None]
122 >>> p = Paginator(range(31), 2)
123 >>> paginate({'paginator': p, 'page_obj': p.page(16)}, 2, 0)['pages']
124 [None, 12, 13, 14, 15, 16]
128 # zero window, zero margin
129 >>> p = Paginator(range(31), 2)
130 >>> paginate({'paginator': p, 'page_obj': p.page(1)}, 0, 0)['pages']
133 >>> p = Paginator(range(31), 2)
134 >>> paginate({'paginator': p, 'page_obj': p.page(2)}, 0, 0)['pages']
137 >>> p = Paginator(range(31), 2)
138 >>> paginate({'paginator': p, 'page_obj': p.page(3)}, 0, 0)['pages']
141 >>> p = Paginator(range(31), 2)
142 >>> paginate({'paginator': p, 'page_obj': p.page(10)}, 0, 0)['pages']
145 >>> p = Paginator(range(31), 2)
146 >>> paginate({'paginator': p, 'page_obj': p.page(14)}, 0, 0)['pages']
149 >>> p = Paginator(range(31), 2)
150 >>> paginate({'paginator': p, 'page_obj': p.page(15)}, 0, 0)['pages']
153 >>> p = Paginator(range(31), 2)
154 >>> paginate({'paginator': p, 'page_obj': p.page(16)}, 0, 0)['pages']
157 >>> p = Paginator(range(31), 2)
158 >>> paginate({'paginator': p, 'page_obj': p.page(5)}, 0, 1)['pages']
159 [1, None, 5, None, 16]
161 >>> p = Paginator(range(21), 2, 1)
162 >>> paginate({'paginator': p, 'page_obj': p.page(1)}, 0, 4)['pages']
163 [1, 2, 3, 4, None, 7, 8, 9, 10]
165 # Testing template rendering
167 >>> t = Template("{% load pagination_tags %}{% autopaginate var 2 %}{% paginate %}")
169 >>> from django.http import HttpRequest as DjangoHttpRequest
170 >>> class HttpRequest(DjangoHttpRequest):
171 ... page = lambda self, suffix: 1
173 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
174 u'\\n...\\n...<div class="pagination">...
176 >>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
177 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
178 u'\\n...\\n...<div class="pagination">...
179 >>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
180 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
181 u'\\n...\\n...<div class="pagination">...
182 >>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
183 >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
184 u'\\n...\\n...<div class="pagination">...<a href="?page=2"...
185 >>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
186 >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
187 u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
189 >>> t = Template("{% load pagination_tags %}{% autopaginate var2 by as foo2 %}{% paginate %}{% autopaginate var by as foo %}{% paginate %}")
190 >>> t.render(Context({'var': range(21), 'var2': range(50, 121), 'by': 20, 'request': HttpRequest()}))
191 u'\\n...\\n...<div class="pagination">...<a href="?page_var2=2"...<a href="?page_var=2"...
194 # Testing InfinitePaginator
196 >>> from paginator import InfinitePaginator
198 >>> InfinitePaginator
199 <class 'linaro_django_pagination.paginator.InfinitePaginator'>
200 >>> p = InfinitePaginator(range(20), 2, link_template='/bacon/page/%d')
201 >>> p.validate_number(2)
212 >>> p3.has_previous()
214 >>> p.page(10).has_next()
216 >>> p.page(1).has_previous()
220 >>> p3.previous_link()
223 # Testing FinitePaginator
225 >>> from paginator import FinitePaginator
228 <class 'linaro_django_pagination.paginator.FinitePaginator'>
229 >>> p = FinitePaginator(range(20), 2, offset=10, link_template='/bacon/page/%d')
230 >>> p.validate_number(2)
243 >>> p3.has_previous()
247 >>> p3.previous_link()
250 >>> p = FinitePaginator(range(20), 20, offset=10, link_template='/bacon/page/%d')
256 >>> p3.has_previous()
260 >>> p2.previous_link()
263 >>> from linaro_django_pagination.middleware import PaginationMiddleware
264 >>> from django.core.handlers.wsgi import WSGIRequest
265 >>> from StringIO import StringIO
266 >>> middleware = PaginationMiddleware()
267 >>> request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart', 'wsgi.input': StringIO()})
268 >>> middleware.process_request(request)
269 >>> request.upload_handlers.append('asdf')