2 >>> from django.core.paginator import Paginator
3 >>> from linaro_django_pagination.templatetags.pagination_tags import paginate
4 >>> from django.template import Template, Context
6 >>> p = Paginator(range(15), 2)
7 >>> pg = paginate({'paginator': p, 'page_obj': p.page(1)})
9 [1, 2, 3, 4, 5, 6, 7, 8]
10 >>> pg['records']['first']
12 >>> pg['records']['last']
15 >>> p = Paginator(range(15), 2)
16 >>> pg = paginate({'paginator': p, 'page_obj': p.page(8)})
18 [1, 2, 3, 4, 5, 6, 7, 8]
19 >>> pg['records']['first']
21 >>> pg['records']['last']
24 >>> p = Paginator(range(17), 2)
25 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
26 [1, 2, 3, 4, 5, 6, 7, 8, 9]
30 # moving the window from 1 ... to end
31 # window size = 2, margin = 2
32 # [1] 2 3 4 5 ... 15, 16
33 # 1 [2] 3 4 5 ... 15, 16
34 # 1 2 [3] 4 5 ... 15, 16
35 # 1 2 3 [4] 5 6 ... 15, 16
36 # 1 2 3 4 [5] 6 7 ... 15, 16
37 # 1 2 3 4 5 [6] 7 8 ... 15, 16
38 # 1 2 ... 5 6 [7] 8 9 ... 15, 16
40 # window = 2 -> show 5 pages
41 >>> p = Paginator(range(31), 2)
42 >>> paginate({'paginator': p, 'page_obj': p.page(1)}, 2, 2)['pages']
43 [1, 2, 3, 4, 5, None, 15, 16]
45 >>> p = Paginator(range(31), 2)
46 >>> paginate({'paginator': p, 'page_obj': p.page(2)}, 2, 2)['pages']
47 [1, 2, 3, 4, 5, None, 15, 16]
49 >>> p = Paginator(range(31), 2)
50 >>> paginate({'paginator': p, 'page_obj': p.page(3)}, 2, 2)['pages']
51 [1, 2, 3, 4, 5, None, 15, 16]
53 >>> p = Paginator(range(31), 2)
54 >>> paginate({'paginator': p, 'page_obj': p.page(4)}, 2, 2)['pages']
55 [1, 2, 3, 4, 5, 6, None, 15, 16]
57 >>> p = Paginator(range(31), 2)
58 >>> paginate({'paginator': p, 'page_obj': p.page(5)}, 2, 2)['pages']
59 [1, 2, 3, 4, 5, 6, 7, None, 15, 16]
62 >>> p = Paginator(range(31), 2)
63 >>> paginate({'paginator': p, 'page_obj': p.page(7)}, 2, 2)['pages']
64 [1, 2, None, 5, 6, 7, 8, 9, None, 15, 16]
67 >>> p = Paginator(range(31), 2)
68 >>> paginate({'paginator': p, 'page_obj': p.page(16)}, 2, 2)['pages']
69 [1, 2, None, 12, 13, 14, 15, 16]
71 >>> p = Paginator(range(31), 2)
72 >>> paginate({'paginator': p, 'page_obj': p.page(13)}, 2, 2)['pages']
73 [1, 2, None, 11, 12, 13, 14, 15, 16]
76 >>> p = Paginator(range(0), 2)
77 >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
83 >>> p = Paginator(range(31), 2)
84 >>> paginate({'paginator': p, 'page_obj': p.page(3)}, 2, 0)['pages']
87 >>> p = Paginator(range(31), 2)
88 >>> paginate({'paginator': p, 'page_obj': p.page(5)}, 2, 0)['pages']
89 [None, 3, 4, 5, 6, 7, None]
91 >>> p = Paginator(range(31), 2)
92 >>> paginate({'paginator': p, 'page_obj': p.page(16)}, 2, 0)['pages']
93 [None, 12, 13, 14, 15, 16]
97 # zero window, zero margin
98 >>> p = Paginator(range(31), 2)
99 >>> paginate({'paginator': p, 'page_obj': p.page(1)}, 0, 0)['pages']
102 >>> p = Paginator(range(31), 2)
103 >>> paginate({'paginator': p, 'page_obj': p.page(2)}, 0, 0)['pages']
106 >>> p = Paginator(range(31), 2)
107 >>> paginate({'paginator': p, 'page_obj': p.page(3)}, 0, 0)['pages']
110 >>> p = Paginator(range(31), 2)
111 >>> paginate({'paginator': p, 'page_obj': p.page(10)}, 0, 0)['pages']
114 >>> p = Paginator(range(31), 2)
115 >>> paginate({'paginator': p, 'page_obj': p.page(14)}, 0, 0)['pages']
118 >>> p = Paginator(range(31), 2)
119 >>> paginate({'paginator': p, 'page_obj': p.page(15)}, 0, 0)['pages']
122 >>> p = Paginator(range(31), 2)
123 >>> paginate({'paginator': p, 'page_obj': p.page(16)}, 0, 0)['pages']
126 >>> p = Paginator(range(31), 2)
127 >>> paginate({'paginator': p, 'page_obj': p.page(5)}, 0, 1)['pages']
128 [1, None, 5, None, 16]
130 >>> p = Paginator(range(21), 2, 1)
131 >>> paginate({'paginator': p, 'page_obj': p.page(1)}, 0, 4)['pages']
132 [1, 2, 3, 4, None, 7, 8, 9, 10]
134 # Testing template rendering
136 >>> t = Template("{% load pagination_tags %}{% autopaginate var 2 %}{% paginate %}")
138 >>> from django.http import HttpRequest as DjangoHttpRequest
139 >>> class HttpRequest(DjangoHttpRequest):
140 ... page = lambda self, suffix: 1
142 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
143 u'\\n\\n<div class="pagination">...
145 >>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
146 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
147 u'\\n\\n<div class="pagination">...
148 >>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
149 >>> t.render(Context({'var': range(21), 'request': HttpRequest()}))
150 u'\\n\\n<div class="pagination">...
151 >>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
152 >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
153 u'\\n\\n<div class="pagination">...<a href="?page=2"...
154 >>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
155 >>> t.render(Context({'var': range(21), 'by': 20, 'request': HttpRequest()}))
156 u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
158 >>> t = Template("{% load pagination_tags %}{% autopaginate var2 by as foo2 %}{% paginate %}{% autopaginate var by as foo %}{% paginate %}")
159 >>> t.render(Context({'var': range(21), 'var2': range(50, 121), 'by': 20, 'request': HttpRequest()}))
160 u'\\n\\n<div class="pagination">...<a href="?page_var2=2"...<a href="?page_var=2"...
163 # Testing InfinitePaginator
165 >>> from paginator import InfinitePaginator
167 >>> InfinitePaginator
168 <class 'linaro_django_pagination.paginator.InfinitePaginator'>
169 >>> p = InfinitePaginator(range(20), 2, link_template='/bacon/page/%d')
170 >>> p.validate_number(2)
181 >>> p3.has_previous()
183 >>> p.page(10).has_next()
185 >>> p.page(1).has_previous()
189 >>> p3.previous_link()
192 # Testing FinitePaginator
194 >>> from paginator import FinitePaginator
197 <class 'linaro_django_pagination.paginator.FinitePaginator'>
198 >>> p = FinitePaginator(range(20), 2, offset=10, link_template='/bacon/page/%d')
199 >>> p.validate_number(2)
212 >>> p3.has_previous()
216 >>> p3.previous_link()
219 >>> p = FinitePaginator(range(20), 20, offset=10, link_template='/bacon/page/%d')
225 >>> p3.has_previous()
229 >>> p2.previous_link()
232 >>> from linaro_django_pagination.middleware import PaginationMiddleware
233 >>> from django.core.handlers.wsgi import WSGIRequest
234 >>> from StringIO import StringIO
235 >>> middleware = PaginationMiddleware()
236 >>> request = WSGIRequest({'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart', 'wsgi.input': StringIO()})
237 >>> middleware.process_request(request)
238 >>> request.upload_handlers.append('asdf')