]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/pagination.py
use latest jQuery version
[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
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         page_numbers_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
275         paginator.page_numbers_context = page_numbers_context
276
277         def page_nums():
278             return page_numbers_template.render(template.Context(page_numbers_context))
279
280         paginator.page_numbers = page_nums
281     else:
282         paginator.page_numbers = ''
283
284     if pagesize:
285         def page_sizes():
286             if sort:
287                 url_builder = lambda s: mark_safe("%s%s%s=%s&amp;%s=%s" % (escape(base_path), url_joiner, context.SORT, sort, context.PAGESIZE, s))
288             else:
289                 url_builder = lambda s: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.PAGESIZE, s))
290
291             sizes = [(s, url_builder(s)) for s in context.pagesizes]
292
293             return page_sizes_template.render(template.Context({
294                 'current': pagesize,
295                 'sizes': sizes
296             }))
297
298         paginator.page_sizes = page_sizes
299     else:
300         paginator.page_sizes = ''
301
302     if sort:
303         def sort_tabs():
304             url_builder = lambda s: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.SORT, s))
305             sorts = [(n, s.label, url_builder(n), strip_tags(s.description)) for n, s in context.sort_methods.items()]
306
307             for name, label, url, descr in sorts:
308                 paginator.__dict__['%s_sort_link' % name] = smart_unicode(url)
309
310             return sort_tabs_template.render(template.Context({
311                 'current': sort,
312                 'sorts': sorts,
313                 'sticky': session_prefs.get('sticky_sort', False)
314             }))
315         paginator.sort_tabs = sort_tabs()
316         paginator.sort_description = mark_safe(context.sort_methods[sort].description)
317         paginator.current_sort = sort
318     else:
319         paginator.sort_tabs = paginator.sort_description = ''
320         paginator.current_sort = ''
321
322     context.set_preferences(request, session_prefs)
323     objects.paginator = paginator
324     return objects