From 51bcecf368760f107aea62a2d570e5e443226936 Mon Sep 17 00:00:00 2001 From: Bojan Mihelac Date: Wed, 7 Sep 2016 17:55:43 +0200 Subject: [PATCH] Fixes Images / documents pagination (#2700) Refactored pagination_nav template to allow empty linkurl in which case url is created replacing page in QUERY_STRING. --- CHANGELOG.txt | 1 + docs/releases/1.7.rst | 1 + wagtail/utils/pagination.py | 20 +++++++++++++++++++ .../wagtailadmin/shared/pagination_nav.html | 13 +++++++++++- .../templatetags/wagtailadmin_tags.py | 11 +++++++++- .../wagtaildocs/documents/results.html | 2 +- .../wagtailimages/images/results.html | 2 +- 7 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f65c3c6f7d..e29ee3843f 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -30,6 +30,7 @@ Changelog * Fix: `InlinePanel` now accepts a `classname` parameter as per the documentation (emg36, Matt Westcott) * Fix: Disabled use of escape key to revert content of rich text fields, which could cause accidental data loss (Matt Westcott) * Fix: Setting `USE_THOUSAND_SEPARATOR = True` no longer breaks the rendering of numbers in JS code for InlinePanel (Mattias Loverot, Matt Westcott) + * Fix: Images / documents pagination now preserves GET parameters (Bojan Mihelac) 1.6.3 (30.09.2016) diff --git a/docs/releases/1.7.rst b/docs/releases/1.7.rst index 7af821236f..533cf3ad96 100644 --- a/docs/releases/1.7.rst +++ b/docs/releases/1.7.rst @@ -65,6 +65,7 @@ Bug fixes * ``InlinePanel`` now accepts a ``classname`` parameter as per the documentation (emg36, Matt Westcott) * Disabled use of escape key to revert content of rich text fields, which could cause accidental data loss (Matt Westcott) * Setting ``USE_THOUSAND_SEPARATOR = True`` no longer breaks the rendering of numbers in JS code for InlinePanel (Mattias Loverot, Matt Westcott) + * Images / documents pagination now preserves GET parameters (Bojan Mihelac) Upgrade considerations diff --git a/wagtail/utils/pagination.py b/wagtail/utils/pagination.py index f144025be2..061396e59d 100644 --- a/wagtail/utils/pagination.py +++ b/wagtail/utils/pagination.py @@ -1,6 +1,9 @@ from __future__ import absolute_import, unicode_literals from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator +from django.utils.http import urlencode +from django.utils.six.moves.urllib.parse import parse_qs + DEFAULT_PAGE_KEY = 'p' @@ -17,3 +20,20 @@ def paginate(request, items, page_key=DEFAULT_PAGE_KEY, per_page=20): page = paginator.page(paginator.num_pages) return paginator, page + + +def replace_page_in_query(query, page_number, page_key=DEFAULT_PAGE_KEY): + """ + Replaces ``page_key`` from query string with ``page_number``. + + >>> replace_page_in_query("p=1&key=value", 2) + 'p=2&key=value' + >>> replace_page_in_query("p=1&key=value", None) + 'key=value' + """ + getvars = parse_qs(query) + if page_number is None: + getvars.pop(page_key, None) + else: + getvars[page_key] = page_number + return urlencode(getvars, True) diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/shared/pagination_nav.html b/wagtail/wagtailadmin/templates/wagtailadmin/shared/pagination_nav.html index ca3e4b073f..cf84af4b9c 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/shared/pagination_nav.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/shared/pagination_nav.html @@ -1,4 +1,5 @@ {% load i18n %} +{% load wagtailadmin_tags %} {% if not is_ajax %} {% comment %} HACK: This template expects to be passed a 'linkurl' parameter, containing a URL name @@ -8,9 +9,15 @@ of the form "?q=123", implicitly preserving the current URL path. Using the {% url ... as ... %} form of the tag ensures that this fails silently, rather than throwing a NoReverseMatch exception. + + If 'linkurl' is not passed, it will instead preserve the current URL and parameters, + just replacing the 'p' parameter. {% endcomment %} - {% url linkurl as url_to_use %} + {% if linkurl %} + {% url linkurl as url_to_use %} + {% endif %} {% endif %} +