X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/cefbe813946f9f9e0108193156eedfd265befc33..6b394aaf645c46996908bb1d11b0755dc8ca4067:/forum/utils/pagination.py diff --git a/forum/utils/pagination.py b/forum/utils/pagination.py index 2b71dbd..67e2272 100644 --- a/forum/utils/pagination.py +++ b/forum/utils/pagination.py @@ -5,6 +5,7 @@ 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.utils.encoding import smart_unicode from django.utils.http import urlquote from django.utils.safestring import mark_safe from django.utils.html import strip_tags, escape @@ -16,7 +17,7 @@ def generate_uri(querydict, exclude=None): 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] + all += ["%s=%s" % (k, escape(urlquote(strip_tags(v)))) for v in l] return "&".join(all) @@ -34,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 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 @@ -219,54 +223,60 @@ def _paginated(request, objects, context): 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 + + 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 - 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: - 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: - range_start = 1 + page_numbers = [] - page_numbers = [] + if sort: + url_builder = lambda n: mark_safe("%s%s%s=%s&%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" % (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)]) - - 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 = '' @@ -295,7 +305,7 @@ def _paginated(request, objects, context): 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,