]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/utils/pagination.py
resolves an issue with tags used count that can get out of control if called from...
[osqa.git] / forum / utils / pagination.py
index ca7c2728b51f4f8e641ca8f9ecdbb6b6b149db9b..67e2272b748efb47e0de69c82cc7ab001b4b8097 100644 (file)
@@ -3,12 +3,24 @@ 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.encoding import smart_unicode
+from django.utils.http import urlquote
 from django.utils.safestring import mark_safe
 from django.utils.safestring import mark_safe
-from django.utils.html import strip_tags
-
+from django.utils.html import strip_tags, escape
+from forum.utils.html import sanitize_html
 import logging
 
 import logging
 
+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, escape(urlquote(strip_tags(v)))) for v in l]
+        
+    return "&".join(all)
+
 class SortBase(object):
     def __init__(self, label, description=''):
         self.label = label
 class SortBase(object):
     def __init__(self, label, description=''):
         self.label = label
@@ -23,7 +35,10 @@ class SimpleSort(SortBase):
         return isinstance(self.order_by, (list, tuple)) and self.order_by or [self.order_by]
 
     def apply(self, objects):
         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())
+        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
@@ -107,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')
@@ -189,73 +204,79 @@ 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()
 
     total_pages = paginator.num_pages
 
     if total_pages > 1:
         return object_list
     paginator.page = get_page()
 
     total_pages = paginator.num_pages
 
     if total_pages > 1:
-        def page_nums():
-            total_pages = paginator.num_pages
 
 
-            has_previous = page > 1
-            has_next = page < total_pages
+        total_pages = paginator.num_pages
 
 
-            range_start = page - context.visible_page_range / 2
-            range_end = page + context.visible_page_range / 2
+        has_previous = page > 1
+        has_next = page < total_pages
 
 
+        range_start = page - context.visible_page_range / 2
+        range_end = page + context.visible_page_range / 2
+
+        if range_start < 1:
+            range_end = context.visible_page_range
+            range_start = 1
+
+        if range_end > total_pages:
+            range_start = total_pages - context.visible_page_range + 1
+            range_end = total_pages
             if range_start < 1:
             if range_start < 1:
-                range_end = context.visible_page_range
                 range_start = 1
 
                 range_start = 1
 
-            if range_end > total_pages:
-                range_start = total_pages - context.visible_page_range + 1
-                range_end = total_pages
-                if range_start < 1:
-                    range_start = 1
+        page_numbers = []
 
 
-            page_numbers = []
+        if sort:
+            url_builder = lambda n: mark_safe("%s%s%s=%s&amp;%s=%s" % (escape(base_path), url_joiner, context.SORT, sort, context.PAGE, n))
+        else:
+            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)])
+            page_numbers.append(None)
+        elif range_start > 1:
+            page_numbers.append([(n, url_builder(n)) for n in range(1, range_start)])
+
+        page_numbers.append([(n, url_builder(n)) for n in range(range_start, range_end + 1)])
+
+        if range_end < (total_pages - context.outside_page_range):
+            page_numbers.append(None)
+            page_numbers.append([(n, url_builder(n)) for n in range(total_pages - context.outside_page_range + 1, total_pages + 1)])
+        elif range_end < total_pages:
+            page_numbers.append([(n, url_builder(n)) for n in range(range_end + 1, total_pages + 1)])
+
+        page_numbers_context = {
+            'has_previous': has_previous,
+            'previous_url': has_previous and url_builder(page - 1) or None,
+            'has_next': has_next,
+            'next_url': has_next and url_builder(page + 1) or None,
+            'current': page,
+            'page_numbers': page_numbers
+        }
+
+        paginator.page_numbers_context = page_numbers_context
+
+        def page_nums():
+            return page_numbers_template.render(template.Context(page_numbers_context))
 
 
-            if sort:
-                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, 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)])
-                page_numbers.append(None)
-            elif range_start > 1:
-                page_numbers.append([(n, url_builder(n)) for n in range(1, range_start)])
-
-            page_numbers.append([(n, url_builder(n)) for n in range(range_start, range_end + 1)])
-
-            if range_end < (total_pages - context.outside_page_range):
-                page_numbers.append(None)
-                page_numbers.append([(n, url_builder(n)) for n in range(total_pages - context.outside_page_range + 1, total_pages + 1)])
-            elif range_end < total_pages:
-                page_numbers.append([(n, url_builder(n)) for n in range(range_end + 1, total_pages + 1)])
-
-            return page_numbers_template.render(template.Context({
-                'has_previous': has_previous,
-                'previous_url': has_previous and url_builder(page - 1) or None,
-                'has_next': has_next,
-                'next_url': has_next and url_builder(page + 1) or None,
-                'current': page,
-                'page_numbers': page_numbers
-            }))
         paginator.page_numbers = page_nums
     else:
         paginator.page_numbers = ''
         paginator.page_numbers = page_nums
     else:
         paginator.page_numbers = ''
@@ -263,9 +284,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&amp;%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]
 
@@ -280,11 +301,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))
+            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:
             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
+                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,
@@ -300,4 +321,4 @@ def _paginated(request, objects, context):
 
     context.set_preferences(request, session_prefs)
     objects.paginator = paginator
 
     context.set_preferences(request, session_prefs)
     objects.paginator = paginator
-    return objects
\ No newline at end of file
+    return objects