Gentoo Archives: gentoo-commits

From: Slava Bacherikov <slava@××××××××××××××.ua>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/gentoo-packages:master commit in: gpackages/templates/, gpackages/apps/generic/templatetags/
Date: Sat, 30 Jun 2012 21:02:53
Message-Id: 1341063745.df8d133bba12d578af64c5e3f97d39f972d11424.bacher09@gentoo
1 commit: df8d133bba12d578af64c5e3f97d39f972d11424
2 Author: Slava Bacherikov <slava <AT> bacher09 <DOT> org>
3 AuthorDate: Sat Jun 30 13:42:25 2012 +0000
4 Commit: Slava Bacherikov <slava <AT> bacherikov <DOT> org <DOT> ua>
5 CommitDate: Sat Jun 30 13:42:25 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoo-packages.git;a=commit;h=df8d133b
7
8 Add more cool paginator
9
10 ---
11 gpackages/apps/generic/templatetags/paginator.py | 83 ++++++++++++++++++++++
12 gpackages/templates/ebuilds.html | 3 +-
13 gpackages/templates/maintainers.html | 5 +-
14 gpackages/templates/packages.html | 3 +-
15 gpackages/templates/paginator.html | 18 ++++-
16 5 files changed, 106 insertions(+), 6 deletions(-)
17
18 diff --git a/gpackages/apps/generic/templatetags/paginator.py b/gpackages/apps/generic/templatetags/paginator.py
19 new file mode 100644
20 index 0000000..109154b
21 --- /dev/null
22 +++ b/gpackages/apps/generic/templatetags/paginator.py
23 @@ -0,0 +1,83 @@
24 +from django import template
25 +register = template.Library()
26 +
27 +
28 +class MyPage(object):
29 + __slots__ = ('num', 'is_active')
30 +
31 + def __init__(self, num, current):
32 + self.num = num
33 + if num == current:
34 + self.is_active = True
35 + else:
36 + self.is_active = False
37 +
38 + def active(self):
39 + return 'active' if self.is_active else ''
40 +
41 + def link(self):
42 + return '?page=%s' % self.num
43 +
44 +def get_middle_range(current, page_num, num):
45 + start = (current - num) if current > num else 1
46 + end = (current + num)
47 + if end > page_num:
48 + end = page_num
49 +
50 + return start, end
51 +
52 +def get_pages_in_range(start, end, current):
53 + return (MyPage(n, current) for n in range(start, end+1))
54 +
55 +def get_l_ragne(m_start, num):
56 + if m_start <= 1:
57 + return 1, 0, False
58 +
59 + l_end = num
60 + l_gap = True
61 + if l_end >= m_start:
62 + l_end = m_start - 1
63 +
64 + if l_end + 1 >= m_start:
65 + l_gap = False
66 +
67 + return 1, l_end, l_gap
68 +
69 +def get_r_range(m_end, num_pages, num):
70 + if m_end >= num_pages:
71 + return num_pages, 0, False
72 +
73 + r_start = num_pages - num + 1
74 + r_gap = True
75 + if r_start <= m_end:
76 + r_start = m_end + 1
77 +
78 + if r_start -1 <= m_end:
79 + r_gap = False
80 +
81 + return r_start, num_pages, r_gap
82 +
83 +@××××××××.inclusion_tag('paginator.html')
84 +def paginator(page, num_middle, num_first, all_num = 0):
85 + paginator = page.paginator
86 + num_pages = paginator.num_pages
87 + if all_num >= num_pages:
88 + m_start, m_end = 1, num_pages
89 + first, last = (), ()
90 + l_gap, r_gap = False, False
91 + else:
92 + m_start, m_end = get_middle_range(page.number, num_pages, num_middle)
93 + l_start, l_end, l_gap = get_l_ragne(m_start, num_first)
94 + r_start, r_end, r_gap = get_r_range(m_end, num_pages, num_first)
95 + first = get_pages_in_range(l_start, l_end, page.number)
96 + last = get_pages_in_range(r_start, r_end, page.number)
97 + middle = get_pages_in_range(m_start, m_end, page.number)
98 +
99 + return {'page_obj': page,
100 + 'is_paginated': bool(page),
101 + 'paginator': paginator,
102 + 'middle_pages': middle,
103 + 'first_pages': first,
104 + 'last_pages': last,
105 + 'left_gap': l_gap,
106 + 'right_gap': r_gap}
107
108 diff --git a/gpackages/templates/ebuilds.html b/gpackages/templates/ebuilds.html
109 index aa28c09..3995554 100644
110 --- a/gpackages/templates/ebuilds.html
111 +++ b/gpackages/templates/ebuilds.html
112 @@ -1,9 +1,10 @@
113 {% extends "base.html" %}
114 {% load packages %}
115 +{% load paginator %}
116
117 {% block content %}
118 {% for ebuild in ebuilds %}
119 {% include 'ebuild_object.html' %}
120 {% endfor %}
121 -{% include 'paginator.html' %}
122 +{% paginator page_obj 3 2 %}
123 {% endblock content %}
124
125 diff --git a/gpackages/templates/maintainers.html b/gpackages/templates/maintainers.html
126 index 9c01184..6e77b76 100644
127 --- a/gpackages/templates/maintainers.html
128 +++ b/gpackages/templates/maintainers.html
129 @@ -1,6 +1,7 @@
130 {% extends "base.html" %}
131 -{% load packages %}
132 {% load url from future %}
133 +{% load packages %}
134 +{% load paginator %}
135
136 {% block content %}
137
138 @@ -25,5 +26,5 @@
139 {% endfor %}
140 </tbody>
141 </table>
142 -{% include 'paginator.html' %}
143 +{% paginator page_obj 3 2 %}
144 {% endblock content %}
145
146 diff --git a/gpackages/templates/packages.html b/gpackages/templates/packages.html
147 index f7ac01a..f66dca5 100644
148 --- a/gpackages/templates/packages.html
149 +++ b/gpackages/templates/packages.html
150 @@ -1,10 +1,11 @@
151 {% extends "base.html" %}
152 {% load url from future %}
153 {% load packages %}
154 +{% load paginator %}
155
156 {% block content %}
157 {% for package in packages %}
158 {% include 'package_object.html' %}
159 {% endfor %}
160 -{% include 'paginator.html' %}
161 +{% paginator page_obj 3 2 %}
162 {% endblock content %}
163
164 diff --git a/gpackages/templates/paginator.html b/gpackages/templates/paginator.html
165 index b957cf9..61aa0c3 100644
166 --- a/gpackages/templates/paginator.html
167 +++ b/gpackages/templates/paginator.html
168 @@ -1,12 +1,26 @@
169 {% if is_paginated %}
170 -<div class="pagination pagination-centered">
171 +<div class="pagination pagination-centered" id="paginator">
172 <ul>
173 {% if page_obj.has_previous %}
174 <li>
175 <a href="?page={{ page_obj.previous_page_number }}">&larr; Prev</a>
176 </li>
177 {% endif %}
178 - <li class="active"> <a href="#">{{ page_obj.number }}</a></li>
179 + {% for page in first_pages %}
180 + <li class="{{ page.active }}"><a href="{{ page.link }}">{{ page.num }}</a></li>
181 + {% endfor %}
182 + {% if left_gap %}
183 + <li class="disabled"><a href="#paginator">...</a></li>
184 + {% endif %}
185 + {% for page in middle_pages %}
186 + <li class="{{ page.active }}"><a href="{{ page.link }}">{{ page.num }}</a></li>
187 + {% endfor %}
188 + {% if right_gap %}
189 + <li class="disabled"><a href="#paginator">...</a></li>
190 + {% endif %}
191 + {% for page in last_pages %}
192 + <li class="{{ page.active }}"><a href="{{ page.link }}">{{ page.num }}</a></li>
193 + {% endfor %}
194 {% if page_obj.has_next %}
195 <li>
196 <a href="?page={{ page_obj.next_page_number }}">Next &rarr;</a>