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
11 class SimpleSort(object):
12 def __init__(self, label, order_by, description=''):
14 self.description = description
15 self.order_by = order_by
17 def apply(self, objects):
18 return objects.order_by(self.order_by)
20 class DummySort(object):
21 def __init__(self, label, description=''):
23 self.description = description
25 def apply(self, objects):
29 class PaginatorContext(object):
30 visible_page_range = 5
31 outside_page_range = 1
35 def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None, sticky_sort=False,
36 pagesizes=None, default_pagesize=None, prefix=''):
40 self.sort_methods = SortedDict(data=sort_methods)
43 default_sort = sort_methods[0][0]
45 self.default_sort = default_sort
51 self.has_pagesize = True
52 self.pagesizes = pagesizes
54 if not default_pagesize:
55 self.default_pagesize = pagesizes[int(math.ceil(float(len(pagesizes)) / 2)) - 1]
57 self.default_pagesize = default_pagesize
59 self.has_pagesize = False
61 self.force_sort = force_sort
62 self.sticky_sort = sticky_sort
65 def session_preferences(self, request):
66 return request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
68 def pagesize(self, request, session_prefs=None):
70 session_prefs = self.session_preferences(request)
74 if request.GET.get(self.PAGESIZE, None):
76 pagesize = int(request.GET[self.PAGESIZE])
78 logging.error('Found invalid page size "%s", loading %s, refered by %s' % (
79 request.GET.get(self.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
83 session_prefs[self.PAGESIZE] = pagesize
85 pagesize = session_prefs.get(self.PAGESIZE, self.default_pagesize)
87 if not pagesize in self.pagesizes:
88 pagesize = self.default_pagesize
94 def page(self, request):
96 return int(request.GET.get(self.PAGE, 1))
98 logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
99 request.GET.get(self.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
103 def sort(self, request, session_prefs=None):
104 if not session_prefs:
105 session_prefs = self.session_preferences(request)
109 if request.GET.get(self.SORT, None):
110 sort = request.GET[self.SORT]
111 if self.sticky_sort or session_prefs.get('sticky_sort', False):
112 session_prefs[self.SORT] = sort
114 sort = self.force_sort or session_prefs.get(self.SORT, self.default_sort)
116 if not sort in self.sort_methods:
117 sort = self.default_sort
121 def sorted(self, objects, request, session_prefs=None):
122 sort = self.sort(request, session_prefs)
125 objects = self.sort_methods[sort].apply(objects)
131 return self.prefix and "%s_%s" % (self.prefix, _('pagesize')) or _('pagesize')
135 return self.prefix and "%s_%s" % (self.prefix, _('page')) or _('page')
139 return self.prefix and "%s_%s" % (self.prefix, _('sort')) or _('sort')
141 page_numbers_template = template.loader.get_template('paginator/page_numbers.html')
142 page_sizes_template = template.loader.get_template('paginator/page_sizes.html')
143 sort_tabs_template = template.loader.get_template('paginator/sort_tabs.html')
145 def paginated(request, paginators, tpl_context):
146 if len(paginators) == 2 and isinstance(paginators[0], basestring):
147 paginators = (paginators,)
149 for list_name, context in paginators:
150 tpl_context[list_name] = _paginated(request, tpl_context[list_name], context)
154 def _paginated(request, objects, context):
155 session_prefs = context.session_preferences(request)
157 pagesize = context.pagesize(request, session_prefs)
158 page = context.page(request)
159 sort, objects = context.sorted(objects, request, session_prefs)
161 paginator = Paginator(objects, pagesize)
164 page_obj = paginator.page(page)
166 logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
167 request.GET.get(context.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
171 if context.base_path:
172 base_path = context.base_path
174 base_path = request.path
175 get_params = ["%s=%s" % (k, v) for k, v in request.GET.items() if not k in (context.PAGE, context.PAGESIZE, context.SORT)]
178 base_path += "?" + "&".join(get_params)
180 url_joiner = "?" in base_path and "&" or "?"
184 object_list = page_obj.object_list
186 if hasattr(object_list, 'lazy'):
187 return object_list.lazy()
189 paginator.page = get_page()
191 total_pages = paginator.num_pages
195 total_pages = paginator.num_pages
197 has_previous = page > 1
198 has_next = page < total_pages
200 range_start = page - context.visible_page_range / 2
201 range_end = page + context.visible_page_range / 2
204 range_end = context.visible_page_range
207 if range_end > total_pages:
208 range_start = total_pages - context.visible_page_range + 1
209 range_end = total_pages
216 url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.PAGE, n))
218 url_builder = lambda n: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.PAGE, n))
220 if range_start > (context.outside_page_range + 1):
221 page_numbers.append([(n, url_builder(n)) for n in range(1, context.outside_page_range + 1)])
222 page_numbers.append(None)
223 elif range_start > 1:
224 page_numbers.append([(n, url_builder(n)) for n in range(1, range_start)])
226 page_numbers.append([(n, url_builder(n)) for n in range(range_start, range_end + 1)])
228 if range_end < (total_pages - context.outside_page_range):
229 page_numbers.append(None)
230 page_numbers.append([(n, url_builder(n)) for n in range(total_pages - context.outside_page_range + 1, total_pages + 1)])
231 elif range_end < total_pages:
232 page_numbers.append([(n, url_builder(n)) for n in range(range_end + 1, total_pages + 1)])
234 return page_numbers_template.render(template.Context({
235 'has_previous': has_previous,
236 'previous_url': has_previous and url_builder(page - 1) or None,
237 'has_next': has_next,
238 'next_url': has_next and url_builder(page + 1) or None,
240 'page_numbers': page_numbers
242 paginator.page_numbers = page_nums
244 paginator.page_numbers = ''
249 url_builder = lambda s: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.PAGESIZE, s))
251 url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.PAGESIZE, s))
253 sizes = [(s, url_builder(s)) for s in context.pagesizes]
255 return page_sizes_template.render(template.Context({
260 paginator.page_sizes = page_sizes
262 paginator.page_sizes = ''
266 url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.SORT, s))
267 sorts = [(n, s.label, url_builder(n), s.description) for n, s in context.sort_methods.items()]
269 return sort_tabs_template.render(template.Context({
272 'sticky': session_prefs.get('sticky_sort', False)
274 paginator.sort_tabs = sort_tabs()
276 paginator.sort_tabs = ''
278 request.session['paginator_%s' % context.id] = session_prefs
279 objects.paginator = paginator