def inclusion_paginator name object_list per_page 10 frame_size def de

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def inclusion_paginator(name="object_list", per_page=10, frame_size=5):
def decorator(func):
def wrapper(context, *args, **kw):
output = func(context, *args, **kw)
paginate_qs = output.pop('object_list')
try:
page_num = int(context['request'].GET['page'])
except (ValueError, MultiValueDictKeyError):
page_num = 1
try:
new_per_page = int(context['request'].GET['per_page'])
except (ValueError, MultiValueDictKeyError):
new_per_page = per_page
paginator = Paginator(paginate_qs, new_per_page)
last_page = paginator.num_pages
side_size = int(frame_size / 2.0)
left_plus = max(0, side_size - (last_page - page_num))
right_plus = max(0, side_size - (page_num - 1))
prev_pages = range(1, page_num)[-1 * (side_size + left_plus):]
next_pages = range(page_num + 1, last_page + 1)[:side_size + right_plus]
page = paginator.page(page_num)
output[name] = page.object_list
output['pages'] = prev_pages + [page_num] + next_pages
output['page'] = page
output['per_page'] = new_per_page
assert False
return output
return wrapper
return decorator