Installation de la version modifié de QBE dans src/
[auf_rh_dae.git] / src / qbe / django_qbe / templatetags / qbe_tags.py
1 # -*- coding: utf-8 -*-
2 from django import template
3 from django.utils.safestring import mark_safe
4
5 register = template.Library()
6
7
8 @register.simple_tag
9 def qbe_paginator(total_pages, rows_per_page, current_page):
10
11 def _get_range_markup(range_from, range_to):
12 out = []
13 for page in range(range_from, range_to):
14 if page == current_page:
15 html = u'<span class="this-page">%s</span>' % (page + 1)
16 elif page == 0:
17 html = u'<a class="start" href="?p=%s">%s</a>' % (page,
18 page + 1)
19 elif page == pages - 1:
20 html = u'<a class="end" href="?p=%s">%s</a>' % (page, page + 1)
21 else:
22 html = u'<a href="?p=%s">%s</a>' % (page, page + 1)
23 out.append(html)
24 return out
25
26 total_pages = int(total_pages)
27 rows_per_page = int(rows_per_page)
28 current_page = int(current_page)
29 if total_pages < rows_per_page or not rows_per_page:
30 pages = 1
31 else:
32 pages = (total_pages / rows_per_page)
33 output = []
34 if pages < 11:
35 output += _get_range_markup(0, pages)
36 elif current_page < 6:
37 output += _get_range_markup(0, current_page + 2)
38 output += ["..."]
39 output += _get_range_markup(pages - 2, pages)
40 elif current_page > pages - 6:
41 output += _get_range_markup(0, 2)
42 output += ["..."]
43 output += _get_range_markup(current_page - 2, pages)
44 else:
45 output += _get_range_markup(0, 2)
46 output += ["..."]
47 output += _get_range_markup(current_page - 2, current_page + 3)
48 output += ["..."]
49 output += _get_range_markup(pages - 2, pages)
50 return mark_safe(u"\n".join(output))