def paginate_style name records_list per_page 10 frame_size def decora

 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
35
36
37
def paginate_style(name='records_list', per_page=10, frame_size=5):
def decorator(func):
def wrapper(request, *args, **kw):
output = func(request, *args, **kw)
paginate_qs = output.pop('paginate_qs')
try:
page_num = int(request.GET['page'])
except (ValueError, MultiValueDictKeyError):
page_num = 1
paginator = Paginator(paginate_qs, 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]
try:
page = paginator.page(page_num)
except InvalidPage:
raise Http404()
objects = page.object_list
for num, obj in enumerate(objects):
obj.paginate_number = (page_num - 1) * per_page + num
output[name] = objects
output['pages'] = prev_pages + [page_num] + next_pages
output['pages_count'] = len(output['pages'])
output['page'] = page
return output
return wrapper
return decorator