]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/utils/pagination.py
OSQA-525, use old style syntax for the if-statements, adds a bit of backwards compati...
[osqa.git] / forum / utils / pagination.py
index 641c549747b9f1910493ed492f324c0f6ba71bb7..d605d5f0788b4a43f67537a0414c45d8d8e8f3c5 100644 (file)
@@ -3,31 +3,42 @@ from django.utils.datastructures import SortedDict
 from django import template
 from django.core.paginator import Paginator, EmptyPage
 from django.utils.translation import ugettext as _
 from django import template
 from django.core.paginator import Paginator, EmptyPage
 from django.utils.translation import ugettext as _
+from django.utils.html import escape
 from django.http import Http404
 from django.http import Http404
-from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_unicode
 from django.utils.http import urlquote
 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
 
 import logging
 
-class SimpleSort(object):
-    def __init__(self, label, order_by, description=''):
-        self.label = label
-        self.description = description
-        self.order_by = order_by
+def generate_uri(querydict, exclude=None):
+    all = []
 
 
-    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)
+    for k, l in querydict.iterlists():
+        if (not exclude) or (not k in exclude):
+            all += ["%s=%s" % (k, escape(strip_tags(v))) for v in l]
+        
+    return "&".join(all)
 
 
-class DummySort(object):
+class SortBase(object):
     def __init__(self, label, description=''):
         self.label = label
         self.description = description
 
     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 _get_order_by(self):
+        return isinstance(self.order_by, (list, tuple)) and self.order_by or [self.order_by]
+
     def apply(self, objects):
     def apply(self, objects):
-        return objects
+        if self.order_by:
+            return objects.order_by(*self._get_order_by())
 
 
+        return objects
 
 class PaginatorContext(object):
     visible_page_range = 5
 
 class PaginatorContext(object):
     visible_page_range = 5
@@ -35,7 +46,7 @@ class PaginatorContext(object):
 
     base_path = None
 
 
     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:
                  pagesizes=None, default_pagesize=None, prefix=''):
         self.id = id
         if sort_methods:
@@ -62,15 +73,30 @@ class PaginatorContext(object):
             self.has_pagesize = False
 
         self.force_sort = force_sort
             self.has_pagesize = False
 
         self.force_sort = force_sort
-        self.sticky_sort = sticky_sort
         self.prefix = prefix
 
         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:
 
     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:
 
 
         if self.has_pagesize:
@@ -96,7 +122,7 @@ class PaginatorContext(object):
 
     def page(self, request):
         try:
 
     def page(self, request):
         try:
-            return int(request.GET.get(self.PAGE, 1))
+            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')
         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')
@@ -105,16 +131,19 @@ class PaginatorContext(object):
 
     def sort(self, request, session_prefs=None):
         if not session_prefs:
 
     def sort(self, request, session_prefs=None):
         if not session_prefs:
-            session_prefs = self.session_preferences(request)
+            session_prefs = self.preferences(request)
 
         sort = None
 
         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.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:
                     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
 
             if not sort in self.sort_methods:
                 sort = self.default_sort
@@ -155,7 +184,7 @@ def paginated(request, paginators, tpl_context):
     return tpl_context
 
 def _paginated(request, objects, 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)
 
     pagesize = context.pagesize(request, session_prefs)
     page = context.page(request)
@@ -175,19 +204,19 @@ def _paginated(request, objects, context):
         base_path = context.base_path
     else:
         base_path = request.path
         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 (context.PAGE, context.PAGESIZE, context.SORT)]
+        get_params = generate_uri(request.GET, (context.PAGE, context.PAGESIZE, context.SORT))
 
         if get_params:
 
         if get_params:
-            base_path += "?" + "&".join(get_params)
+            base_path += "?" + get_params
 
 
-    url_joiner = "?" in base_path and "&" or "?"
+    url_joiner = "?" in base_path and "&" or "?"
 
 
     def get_page():
         object_list = page_obj.object_list
 
 
 
     def get_page():
         object_list = page_obj.object_list
 
-        if hasattr(object_list, 'lazy'):
-            return object_list.lazy()
+        #if hasattr(object_list, 'lazy'):
+        #    return object_list.lazy()
         return object_list
     paginator.page = get_page()
 
         return object_list
     paginator.page = get_page()
 
@@ -216,9 +245,9 @@ def _paginated(request, objects, context):
             page_numbers = []
 
             if sort:
             page_numbers = []
 
             if sort:
-                url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.PAGE, n))
+                url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (escape(base_path), url_joiner, context.SORT, sort, context.PAGE, n))
             else:
             else:
-                url_builder = lambda n: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.PAGE, n))
+                url_builder = lambda n: mark_safe("%s%s%s=%s" % (escape(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)])
 
             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)])
@@ -249,9 +278,9 @@ def _paginated(request, objects, context):
     if pagesize:
         def page_sizes():
             if sort:
     if pagesize:
         def page_sizes():
             if sort:
-                url_builder = lambda s: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.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:
             else:
-                url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.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 = [(s, url_builder(s)) for s in context.pagesizes]
 
@@ -266,8 +295,11 @@ def _paginated(request, objects, context):
 
     if sort:
         def sort_tabs():
 
     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()]
+            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] = smart_unicode(url)
 
             return sort_tabs_template.render(template.Context({
                 'current': sort,
 
             return sort_tabs_template.render(template.Context({
                 'current': sort,
@@ -275,9 +307,12 @@ def _paginated(request, objects, context):
                 'sticky': session_prefs.get('sticky_sort', False)
             }))
         paginator.sort_tabs = sort_tabs()
                 '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:
     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
     objects.paginator = paginator
-    return objects
\ No newline at end of file
+    return objects