from django.core.paginator import Paginator, EmptyPage
from django.utils.translation import ugettext as _
from django.http import Http404
-from django.utils.safestring import mark_safe
from django.utils.http import urlquote
+from django.utils.safestring import mark_safe
+from django.utils.html import strip_tags, escape
+from forum.utils.html import sanitize_html
import logging
-class SimpleSort(object):
- def __init__(self, label, order_by, description=''):
+def generate_uri(querydict, exclude=None):
+ all = []
+
+ for k, l in querydict.iterlists():
+ if (not exclude) or (not k in exclude):
+ all += ["%s=%s" % (k, urlquote(strip_tags(v))) for v in l]
+
+ return "&".join(all)
+
+class SortBase(object):
+ def __init__(self, label, description=''):
self.label = label
self.description = description
+
+class SimpleSort(SortBase):
+ def __init__(self, label, order_by, description=''):
+ super(SimpleSort, self) .__init__(label, description)
self.order_by = order_by
- def apply(self, objects):
- return objects.order_by(self.order_by)
+ def _get_order_by(self):
+ return isinstance(self.order_by, (list, tuple)) and self.order_by or [self.order_by]
+ def apply(self, objects):
+ return objects.order_by(*self._get_order_by())
class PaginatorContext(object):
visible_page_range = 5
base_path = None
- def __init__(self, id, sort_methods=None, default_sort=None, pagesizes=None, default_pagesize=None):
+ def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None,
+ pagesizes=None, default_pagesize=None, prefix=''):
self.id = id
if sort_methods:
self.has_sort = True
else:
self.has_pagesize = False
- self.force_sort = None
- self.sticky_sort = False
+ self.force_sort = force_sort
+ self.prefix = prefix
+
+ def preferences(self, request):
+ if request.user.is_authenticated():
+ if request.user.prop.pagination:
+ preferences = request.user.prop.pagination.get(self.id, {})
+ else:
+ preferences = {}
+ else:
+ preferences = request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
+
+ return preferences
+
+ def set_preferences(self, request, preferences):
+ if request.user.is_authenticated():
+ all_preferences = request.user.prop.pagination or {}
+ all_preferences[self.id] = preferences
+ request.user.prop.pagination = all_preferences
+ else:
+ request.session['paginator_%s%s' % (self.prefix, self.id)] = preferences
+
+ def pagesize(self, request, session_prefs=None):
+ if not session_prefs:
+ session_prefs = self.preferences(request)
+
+
+ if self.has_pagesize:
+ if request.GET.get(self.PAGESIZE, None):
+ try:
+ pagesize = int(request.GET[self.PAGESIZE])
+ except ValueError:
+ logging.error('Found invalid page size "%s", loading %s, refered by %s' % (
+ request.GET.get(self.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+ ))
+ raise Http404()
+
+ session_prefs[self.PAGESIZE] = pagesize
+ else:
+ pagesize = session_prefs.get(self.PAGESIZE, self.default_pagesize)
+
+ if not pagesize in self.pagesizes:
+ pagesize = self.default_pagesize
+ else:
+ pagesize = 30
+ return pagesize
+ def page(self, request):
+ try:
+ return int(request.GET.get(self.PAGE, "1").strip())
+ except ValueError:
+ logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
+ request.GET.get(self.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+ ))
+ raise Http404()
-class labels(object):
- PAGESIZE = _('pagesize')
- PAGE = _('page')
- SORT = _('sort')
+ def sort(self, request, session_prefs=None):
+ if not session_prefs:
+ session_prefs = self.preferences(request)
+
+ sort = None
+ sticky = request.user.is_authenticated() and request.user.prop.preferences and request.user.prop.preferences.get('sticky_sorts', False)
+
+ if self.has_sort:
+ if request.GET.get(self.SORT, None):
+ sort = request.GET[self.SORT]
+
+ if sticky:
+ session_prefs[self.SORT] = sort
+ else:
+ sort = self.force_sort or (sticky and session_prefs.get(self.SORT, None)) or self.default_sort
+
+ if not sort in self.sort_methods:
+ sort = self.default_sort
+
+ return sort
+
+ def sorted(self, objects, request, session_prefs=None):
+ sort = self.sort(request, session_prefs)
+
+ if sort:
+ objects = self.sort_methods[sort].apply(objects)
+
+ return sort, objects
+
+ @property
+ def PAGESIZE(self):
+ return self.prefix and "%s_%s" % (self.prefix, _('pagesize')) or _('pagesize')
+
+ @property
+ def PAGE(self):
+ return self.prefix and "%s_%s" % (self.prefix, _('page')) or _('page')
+
+ @property
+ def SORT(self):
+ return self.prefix and "%s_%s" % (self.prefix, _('sort')) or _('sort')
page_numbers_template = template.loader.get_template('paginator/page_numbers.html')
page_sizes_template = template.loader.get_template('paginator/page_sizes.html')
sort_tabs_template = template.loader.get_template('paginator/sort_tabs.html')
-def paginated(request, list_name, context, tpl_context):
- session_prefs = request.session.get('paginator_%s' % context.id, {})
- objects = tpl_context[list_name]
-
- if context.has_pagesize:
- if request.GET.get(labels.PAGESIZE, None):
- try:
- pagesize = int(request.GET[labels.PAGESIZE])
- except ValueError:
- logging.error('Found invalid page size "%s", loading %s, refered by %s' % (
- request.GET.get(labels.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
- ))
- raise Http404()
-
- session_prefs[labels.PAGESIZE] = pagesize
- else:
- pagesize = session_prefs.get(labels.PAGESIZE, context.default_pagesize)
+def paginated(request, paginators, tpl_context):
+ if len(paginators) == 2 and isinstance(paginators[0], basestring):
+ paginators = (paginators,)
- if not pagesize in context.pagesizes:
- pagesize = context.default_pagesize
- else:
- pagesize = 30
-
- try:
- page = int(request.GET.get(labels.PAGE, 1))
- except ValueError:
- logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
- request.GET.get(labels.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
- ))
- raise Http404()
+ for list_name, context in paginators:
+ tpl_context[list_name] = _paginated(request, tpl_context[list_name], context)
- sort = None
- if context.has_sort:
- if request.GET.get(labels.SORT, None):
- sort = request.GET[labels.SORT]
- if context.sticky_sort or session_prefs.get('sticky_sort', False):
- session_prefs[labels.SORT] = sort
- else:
- sort = context.force_sort or session_prefs.get(labels.SORT, context.default_sort)
+ return tpl_context
- if not sort in context.sort_methods:
- sort = context.default_sort
+def _paginated(request, objects, context):
+ session_prefs = context.preferences(request)
- objects = context.sort_methods[sort].apply(objects)
+ pagesize = context.pagesize(request, session_prefs)
+ page = context.page(request)
+ sort, objects = context.sorted(objects, request, session_prefs)
paginator = Paginator(objects, pagesize)
page_obj = paginator.page(page)
except EmptyPage:
logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
- request.GET.get(labels.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+ request.GET.get(context.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
))
raise Http404()
base_path = context.base_path
else:
base_path = request.path
- get_params = ["%s=%s" % (k, v) for k, v in request.GET.items() if not k in (labels.PAGE, labels.PAGESIZE, labels.SORT)]
+ get_params = generate_uri(request.GET, (context.PAGE, context.PAGESIZE, context.SORT))
if get_params:
- base_path += "?" + "&".join(get_params)
+ base_path += "?" + get_params
url_joiner = "?" in base_path and "&" or "?"
if hasattr(object_list, 'lazy'):
return object_list.lazy()
- return page_obj.object_list
- objects.page = get_page
+ return object_list
+ paginator.page = get_page()
total_pages = paginator.num_pages
page_numbers = []
if sort:
- url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, labels.SORT, sort, labels.PAGE, n))
+ url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.PAGE, n))
else:
- url_builder = lambda n: mark_safe("%s%s%s=%s" % (base_path, url_joiner, labels.PAGE, n))
+ url_builder = lambda n: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.PAGE, n))
if range_start > (context.outside_page_range + 1):
page_numbers.append([(n, url_builder(n)) for n in range(1, context.outside_page_range + 1)])
'current': page,
'page_numbers': page_numbers
}))
- objects.page_numbers = page_nums
+ paginator.page_numbers = page_nums
else:
- objects.page_numbers = ''
+ paginator.page_numbers = ''
if pagesize:
def page_sizes():
if sort:
- url_builder = lambda s: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, labels.SORT, sort, labels.PAGESIZE, s))
+ url_builder = lambda s: mark_safe("%s%s%s=%s&%s=%s" % (escape(base_path), url_joiner, context.SORT, sort, context.PAGESIZE, s))
else:
- url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, labels.PAGESIZE, s))
+ url_builder = lambda s: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.PAGESIZE, s))
sizes = [(s, url_builder(s)) for s in context.pagesizes]
'sizes': sizes
}))
- objects.page_sizes = page_sizes
+ paginator.page_sizes = page_sizes
else:
- objects.page_sizes = ''
+ paginator.page_sizes = ''
if sort:
def sort_tabs():
- url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, labels.SORT, s))
- sorts = [(n, s.label, url_builder(n), s.description) for n, s in context.sort_methods.items()]
+ url_builder = lambda s: mark_safe("%s%s%s=%s" % (escape(base_path), url_joiner, context.SORT, s))
+ sorts = [(n, s.label, url_builder(n), strip_tags(s.description)) for n, s in context.sort_methods.items()]
+
+ for name, label, url, descr in sorts:
+ paginator.__dict__['%s_sort_link' % name] = url
return sort_tabs_template.render(template.Context({
'current': sort,
'sorts': sorts,
'sticky': session_prefs.get('sticky_sort', False)
}))
- objects.sort_tabs = sort_tabs()
+ paginator.sort_tabs = sort_tabs()
+ paginator.sort_description = mark_safe(context.sort_methods[sort].description)
+ paginator.current_sort = sort
else:
- objects.sort_tabs = ''
+ paginator.sort_tabs = paginator.sort_description = ''
+ paginator.current_sort = ''
- request.session['paginator_%s' % context.id] = session_prefs
- tpl_context[list_name] = objects
- return tpl_context
\ No newline at end of file
+ context.set_preferences(request, session_prefs)
+ objects.paginator = paginator
+ return objects