X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/5c7287b435f2e68257bce3b76ee435d9b07f87f9..8b75f5c11b8a6fb017584a3926a01464a4040ce7:/forum/utils/pagination.py diff --git a/forum/utils/pagination.py b/forum/utils/pagination.py index 641c549..ca7c272 100644 --- a/forum/utils/pagination.py +++ b/forum/utils/pagination.py @@ -5,29 +5,25 @@ 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.html import strip_tags + import logging -class SimpleSort(object): - def __init__(self, label, order_by, description=''): +class SortBase(object): + def __init__(self, label, description=''): self.label = label self.description = description - self.order_by = order_by - def apply(self, objects): - if isinstance(self.order_by, (list, tuple)): - return objects.order_by(*self.order_by) - else: - return objects.order_by(self.order_by) +class SimpleSort(SortBase): + def __init__(self, label, order_by, description=''): + super(SimpleSort, self) .__init__(label, description) + self.order_by = order_by -class DummySort(object): - def __init__(self, label, description=''): - self.label = label - self.description = description + 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 - + return objects.order_by(*self._get_order_by()) class PaginatorContext(object): visible_page_range = 5 @@ -35,7 +31,7 @@ class PaginatorContext(object): base_path = None - def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None, sticky_sort=False, + 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: @@ -62,15 +58,30 @@ class PaginatorContext(object): self.has_pagesize = False self.force_sort = force_sort - self.sticky_sort = sticky_sort self.prefix = prefix - def session_preferences(self, request): - return request.session.get('paginator_%s%s' % (self.prefix, self.id), {}) + 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.session_preferences(request) + session_prefs = self.preferences(request) if self.has_pagesize: @@ -105,16 +116,19 @@ class PaginatorContext(object): def sort(self, request, session_prefs=None): if not session_prefs: - session_prefs = self.session_preferences(request) + 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 self.sticky_sort or session_prefs.get('sticky_sort', False): + + if sticky: session_prefs[self.SORT] = sort else: - sort = self.force_sort or session_prefs.get(self.SORT, self.default_sort) + 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 @@ -155,7 +169,7 @@ def paginated(request, paginators, tpl_context): return tpl_context def _paginated(request, objects, context): - session_prefs = context.session_preferences(request) + session_prefs = context.preferences(request) pagesize = context.pagesize(request, session_prefs) page = context.page(request) @@ -267,7 +281,10 @@ def _paginated(request, objects, context): if sort: def sort_tabs(): url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.SORT, s)) - sorts = [(n, s.label, url_builder(n), s.description) for n, s in context.sort_methods.items()] + 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, @@ -275,9 +292,12 @@ def _paginated(request, objects, context): 'sticky': session_prefs.get('sticky_sort', False) })) paginator.sort_tabs = sort_tabs() + paginator.sort_description = mark_safe(context.sort_methods[sort].description) + paginator.current_sort = sort else: - paginator.sort_tabs = '' + paginator.sort_tabs = paginator.sort_description = '' + paginator.current_sort = '' - request.session['paginator_%s' % context.id] = session_prefs + context.set_preferences(request, session_prefs) objects.paginator = paginator return objects \ No newline at end of file