]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/pagination.py
OSQA-525, use old style syntax for the if-statements, adds a bit of backwards compati...
[osqa.git] / forum / utils / pagination.py
1 import math
2 from django.utils.datastructures import SortedDict
3 from django import template
4 from django.core.paginator import Paginator, EmptyPage
5 from django.utils.translation import ugettext as _
6 from django.utils.html import escape
7 from django.http import Http404
8 from django.utils.encoding import smart_unicode
9 from django.utils.http import urlquote
10 from django.utils.safestring import mark_safe
11 from django.utils.html import strip_tags, escape
12 from forum.utils.html import sanitize_html
13 import logging
14
15 def generate_uri(querydict, exclude=None):
16     all = []
17
18     for k, l in querydict.iterlists():
19         if (not exclude) or (not k in exclude):
20             all += ["%s=%s" % (k, escape(strip_tags(v))) for v in l]
21         
22     return "&".join(all)
23
24 class SortBase(object):
25     def __init__(self, label, description=''):
26         self.label = label
27         self.description = description
28
29 class SimpleSort(SortBase):
30     def __init__(self, label, order_by, description=''):
31         super(SimpleSort, self) .__init__(label, description)
32         self.order_by = order_by
33
34     def _get_order_by(self):
35         return isinstance(self.order_by, (list, tuple)) and self.order_by or [self.order_by]
36
37     def apply(self, objects):
38         if self.order_by:
39             return objects.order_by(*self._get_order_by())
40
41         return objects
42
43 class PaginatorContext(object):
44     visible_page_range = 5
45     outside_page_range = 1
46
47     base_path = None
48
49     def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None,
50                  pagesizes=None, default_pagesize=None, prefix=''):
51         self.id = id
52         if sort_methods:
53             self.has_sort = True
54             self.sort_methods = SortedDict(data=sort_methods)
55
56             if not default_sort:
57                 default_sort = sort_methods[0][0]
58
59             self.default_sort = default_sort
60         else:
61             self.has_sort = False
62
63
64         if pagesizes:
65             self.has_pagesize = True
66             self.pagesizes = pagesizes
67
68             if not default_pagesize:
69                 self.default_pagesize = pagesizes[int(math.ceil(float(len(pagesizes)) / 2)) - 1]
70             else:
71                 self.default_pagesize = default_pagesize
72         else:
73             self.has_pagesize = False
74
75         self.force_sort = force_sort
76         self.prefix = prefix
77
78     def preferences(self, request):
79         if request.user.is_authenticated():
80             if request.user.prop.pagination:
81                 preferences = request.user.prop.pagination.get(self.id, {})
82             else:
83                 preferences = {}
84         else:
85             preferences = request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
86
87         return preferences
88
89     def set_preferences(self, request, preferences):
90         if request.user.is_authenticated():
91             all_preferences = request.user.prop.pagination or {}
92             all_preferences[self.id] = preferences
93             request.user.prop.pagination = all_preferences
94         else:
95             request.session['paginator_%s%s' % (self.prefix, self.id)] = preferences
96
97     def pagesize(self, request, session_prefs=None):
98         if not session_prefs:
99             session_prefs = self.preferences(request)
100
101
102         if self.has_pagesize:
103             if request.GET.get(self.PAGESIZE, None):
104                 try:
105                     pagesize = int(request.GET[self.PAGESIZE])
106                 except ValueError:
107                     logging.error('Found invalid page size "%s", loading %s, refered by %s' % (
108                         request.GET.get(self.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
109                     ))
110                     raise Http404()
111
112                 session_prefs[self.PAGESIZE] = pagesize
113             else:
114                 pagesize = session_prefs.get(self.PAGESIZE, self.default_pagesize)
115
116             if not pagesize in self.pagesizes:
117                 pagesize = self.default_pagesize
118         else:
119             pagesize = 30
120
121         return pagesize
122
123     def page(self, request):
124         try:
125             return int(request.GET.get(self.PAGE, "1").strip())
126         except ValueError:
127             logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
128                 request.GET.get(self.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
129             ))
130             raise Http404()
131
132     def sort(self, request, session_prefs=None):
133         if not session_prefs:
134             session_prefs = self.preferences(request)
135
136         sort = None
137         sticky = request.user.is_authenticated() and request.user.prop.preferences and request.user.prop.preferences.get('sticky_sorts', False)
138
139         if self.has_sort:
140             if request.GET.get(self.SORT, None):
141                 sort = request.GET[self.SORT]
142
143                 if sticky:
144                     session_prefs[self.SORT] = sort
145             else:
146                 sort = self.force_sort or (sticky and session_prefs.get(self.SORT, None)) or self.default_sort
147
148             if not sort in self.sort_methods:
149                 sort = self.default_sort
150
151         return sort
152
153     def sorted(self, objects, request, session_prefs=None):
154         sort = self.sort(request, session_prefs)
155
156         if sort:
157             objects = self.sort_methods[sort].apply(objects)
158
159         return sort, objects
160
161     @property
162     def PAGESIZE(self):
163         return self.prefix and "%s_%s" % (self.prefix, _('pagesize')) or _('pagesize')
164
165     @property
166     def PAGE(self):
167         return self.prefix and "%s_%s" % (self.prefix, _('page')) or _('page')
168
169     @property
170     def SORT(self):
171         return self.prefix and "%s_%s" % (self.prefix, _('sort')) or _('sort')
172
173 page_numbers_template = template.loader.get_template('paginator/page_numbers.html')
174 page_sizes_template = template.loader.get_template('paginator/page_sizes.html')
175 sort_tabs_template = template.loader.get_template('paginator/sort_tabs.html')
176
177 def paginated(request, paginators, tpl_context):
178     if len(paginators) == 2 and isinstance(paginators[0], basestring):
179         paginators = (paginators,)
180
181     for list_name, context in paginators:
182         tpl_context[list_name] = _paginated(request, tpl_context[list_name], context)
183
184     return tpl_context
185
186 def _paginated(request, objects, context):
187     session_prefs = context.preferences(request)
188
189     pagesize = context.pagesize(request, session_prefs)
190     page = context.page(request)
191     sort, objects = context.sorted(objects, request, session_prefs)
192
193     paginator = Paginator(objects, pagesize)
194
195     try:
196         page_obj = paginator.page(page)
197     except EmptyPage:
198         logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
199             request.GET.get(context.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
200         ))
201         raise Http404()
202
203     if context.base_path:
204         base_path = context.base_path
205     else:
206         base_path = request.path
207         get_params = generate_uri(request.GET, (context.PAGE, context.PAGESIZE, context.SORT))
208
209         if get_params:
210             base_path += "?" + get_params
211
212     url_joiner = "?" in base_path and "&" or "?"
213
214
215     def get_page():
216         object_list = page_obj.object_list
217
218         #if hasattr(object_list, 'lazy'):
219         #    return object_list.lazy()
220         return object_list
221     paginator.page = get_page()
222
223     total_pages = paginator.num_pages
224
225     if total_pages > 1:
226         def page_nums():
227             total_pages = paginator.num_pages
228
229             has_previous = page > 1
230             has_next = page < total_pages
231
232             range_start = page - context.visible_page_range / 2
233             range_end = page + context.visible_page_range / 2
234
235             if range_start < 1:
236                 range_end = context.visible_page_range
237                 range_start = 1
238
239             if range_end > total_pages:
240                 range_start = total_pages - context.visible_page_range + 1
241                 range_end = total_pages
242                 if range_start < 1:
243                     range_start = 1
244
245             page_numbers = []
246
247             if sort:
248                 url_builder = lambda n: mark_safe("%s%s%s=%s&amp;%s=%s" % (escape(base_path), url_joiner, context.SORT, sort, context.PAGE, n))
249             else:
250                 url_builder = lambda n: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.PAGE, n))
251
252             if range_start > (context.outside_page_range + 1):
253                 page_numbers.append([(n, url_builder(n)) for n in range(1, context.outside_page_range + 1)])
254                 page_numbers.append(None)
255             elif range_start > 1:
256                 page_numbers.append([(n, url_builder(n)) for n in range(1, range_start)])
257
258             page_numbers.append([(n, url_builder(n)) for n in range(range_start, range_end + 1)])
259
260             if range_end < (total_pages - context.outside_page_range):
261                 page_numbers.append(None)
262                 page_numbers.append([(n, url_builder(n)) for n in range(total_pages - context.outside_page_range + 1, total_pages + 1)])
263             elif range_end < total_pages:
264                 page_numbers.append([(n, url_builder(n)) for n in range(range_end + 1, total_pages + 1)])
265
266             return page_numbers_template.render(template.Context({
267                 'has_previous': has_previous,
268                 'previous_url': has_previous and url_builder(page - 1) or None,
269                 'has_next': has_next,
270                 'next_url': has_next and url_builder(page + 1) or None,
271                 'current': page,
272                 'page_numbers': page_numbers
273             }))
274         paginator.page_numbers = page_nums
275     else:
276         paginator.page_numbers = ''
277
278     if pagesize:
279         def page_sizes():
280             if sort:
281                 url_builder = lambda s: mark_safe("%s%s%s=%s&amp;%s=%s" % (escape(base_path), url_joiner, context.SORT, sort, context.PAGESIZE, s))
282             else:
283                 url_builder = lambda s: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.PAGESIZE, s))
284
285             sizes = [(s, url_builder(s)) for s in context.pagesizes]
286
287             return page_sizes_template.render(template.Context({
288                 'current': pagesize,
289                 'sizes': sizes
290             }))
291
292         paginator.page_sizes = page_sizes
293     else:
294         paginator.page_sizes = ''
295
296     if sort:
297         def sort_tabs():
298             url_builder = lambda s: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.SORT, s))
299             sorts = [(n, s.label, url_builder(n), strip_tags(s.description)) for n, s in context.sort_methods.items()]
300
301             for name, label, url, descr in sorts:
302                 paginator.__dict__['%s_sort_link' % name] = smart_unicode(url)
303
304             return sort_tabs_template.render(template.Context({
305                 'current': sort,
306                 'sorts': sorts,
307                 'sticky': session_prefs.get('sticky_sort', False)
308             }))
309         paginator.sort_tabs = sort_tabs()
310         paginator.sort_description = mark_safe(context.sort_methods[sort].description)
311         paginator.current_sort = sort
312     else:
313         paginator.sort_tabs = paginator.sort_description = ''
314         paginator.current_sort = ''
315
316     context.set_preferences(request, session_prefs)
317     objects.paginator = paginator
318     return objects