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