From 5268d71f18d12c362d74010210309c1cec8e8a1a Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Fri, 28 Mar 2014 21:42:58 +0700 Subject: [PATCH] [1.7.x] Fixed #21795 -- Made add_preserved_filters account for url prefixes. Thanks to trac username honyczek for the report. Refs #6903. Backport of 4339e9a92d from master --- .../contrib/admin/templatetags/admin_urls.py | 5 ++- docs/releases/1.6.3.txt | 4 ++ tests/admin_views/tests.py | 38 ++++++++++++++++--- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/django/contrib/admin/templatetags/admin_urls.py b/django/contrib/admin/templatetags/admin_urls.py index 68c7083ecf..350057a117 100644 --- a/django/contrib/admin/templatetags/admin_urls.py +++ b/django/contrib/admin/templatetags/admin_urls.py @@ -5,7 +5,7 @@ except ImportError: from django import template from django.contrib.admin.utils import quote -from django.core.urlresolvers import resolve, Resolver404 +from django.core.urlresolvers import Resolver404, get_script_prefix, resolve from django.utils.http import urlencode register = template.Library() @@ -33,8 +33,9 @@ def add_preserved_filters(context, url, popup=False, to_field=None): if opts and preserved_filters: preserved_filters = dict(parse_qsl(preserved_filters)) + match_url = '/%s' % url.partition(get_script_prefix())[2] try: - match = resolve(url) + match = resolve(match_url) except Resolver404: pass else: diff --git a/docs/releases/1.6.3.txt b/docs/releases/1.6.3.txt index d729534bd7..863cbf2ab2 100644 --- a/docs/releases/1.6.3.txt +++ b/docs/releases/1.6.3.txt @@ -30,5 +30,9 @@ several bugs in 1.6.2: * Fixed a regression in the :mod:`django.contrib.gis` SQL compiler for non-concrete fields (`#22250 `_). +* Fixed :attr:`ModelAdmin.preserve_filters + ` when running a site with + a URL prefix (`#21795 `_). + Additionally, Django's vendored version of six, :mod:`django.utils.six` has been upgraded to the latest release (1.6.1). diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 39a79c0614..50119c2133 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -11,16 +11,18 @@ from django.core import mail from django.core.checks import Error from django.core.files import temp as tempfile from django.core.exceptions import ImproperlyConfigured -from django.core.urlresolvers import reverse, NoReverseMatch +from django.core.urlresolvers import (NoReverseMatch, + get_script_prefix, reverse, set_script_prefix) # Register auth models with the admin. from django.contrib.auth import get_permission_codename from django.contrib.admin import ModelAdmin from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.contrib.admin.models import LogEntry, DELETION +from django.contrib.admin.templatetags.admin_urls import add_preserved_filters +from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase from django.contrib.admin.utils import quote from django.contrib.admin.validation import ModelAdminValidator from django.contrib.admin.views.main import IS_POPUP_VAR -from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.models import Group, User, Permission from django.contrib.contenttypes.models import ContentType @@ -4440,11 +4442,14 @@ class AdminKeepChangeListFiltersTests(TestCase): def tearDown(self): self.client.logout() - def get_changelist_filters_querystring(self): - return urlencode({ + def get_changelist_filters(self): + return { 'is_superuser__exact': 0, 'is_staff__exact': 0, - }) + } + + def get_changelist_filters_querystring(self): + return urlencode(self.get_changelist_filters()) def get_preserved_filters_querystring(self): return urlencode({ @@ -4586,6 +4591,29 @@ class AdminKeepChangeListFiltersTests(TestCase): response = self.client.post(self.get_delete_url(), {'post': 'yes'}) self.assertRedirects(response, self.get_changelist_url()) + def test_url_prefix(self): + context = { + 'preserved_filters': self.get_preserved_filters_querystring(), + 'opts': User._meta, + } + + url = reverse('admin:auth_user_changelist', current_app=self.admin_site.name) + self.assertEqual( + self.get_changelist_url(), + add_preserved_filters(context, url), + ) + + original_prefix = get_script_prefix() + try: + set_script_prefix('/prefix/') + url = reverse('admin:auth_user_changelist', current_app=self.admin_site.name) + self.assertEqual( + self.get_changelist_url(), + add_preserved_filters(context, url), + ) + finally: + set_script_prefix(original_prefix) + class NamespacedAdminKeepChangeListFiltersTests(AdminKeepChangeListFiltersTests): admin_site = site2