[1.6.x] Fixed #20767 -- Fixed ModelAdmin.preserve_filters for namespaced URLs.

Thanks Collin Anderson for the report.

Backport of 2fc6c9472c from master.
This commit is contained in:
Loic Bistuer 2013-07-19 02:17:40 +07:00 committed by Simon Charette
parent 579ca9ee8d
commit c660f0e985
5 changed files with 24 additions and 11 deletions

View File

@ -771,12 +771,10 @@ class ModelAdmin(BaseModelAdmin):
Returns the preserved filters querystring.
"""
# FIXME: We can remove that getattr as soon as #20619 is fixed.
match = getattr(request, 'resolver_match', None)
match = request.resolver_match
if self.preserve_filters and match:
opts = self.model._meta
current_url = '%s:%s' % (match.namespace, match.url_name)
current_url = '%s:%s' % (match.app_name, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if current_url == changelist_url:
preserved_filters = request.GET.urlencode()

View File

@ -38,7 +38,7 @@ def add_preserved_filters(context, url, popup=False):
except Resolver404:
pass
else:
current_url = '%s:%s' % (match.namespace, match.url_name)
current_url = '%s:%s' % (match.app_name, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if changelist_url == current_url and '_changelist_filters' in preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

View File

@ -777,3 +777,8 @@ from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
site.register(User, UserAdmin)
site.register(Group, GroupAdmin)
# Used to test URL namespaces
site2 = admin.AdminSite(name="namespaced_admin")
site2.register(User, UserAdmin)
site2.register(Group, GroupAdmin)

View File

@ -25,7 +25,6 @@ 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
from django.core.urlresolvers import reverse
from django.db import connection
from django.forms.util import ErrorList
from django.template.response import TemplateResponse
@ -51,6 +50,7 @@ from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
Simple, UndeletableObject, Choice, ShortMessage, Telegram)
from .admin import site, site2
ERROR_MESSAGE = "Please enter the correct username and password \
@ -4184,6 +4184,7 @@ class AdminUserMessageTest(TestCase):
class AdminKeepChangeListFiltersTests(TestCase):
urls = "admin_views.urls"
fixtures = ['admin-views-users.xml']
admin_site = site
def setUp(self):
self.client.login(username='super', password='secret')
@ -4207,13 +4208,15 @@ class AdminKeepChangeListFiltersTests(TestCase):
def get_changelist_url(self):
return '%s?%s' % (
reverse('admin:auth_user_changelist'),
reverse('admin:auth_user_changelist',
current_app=self.admin_site.name),
self.get_changelist_filters_querystring(),
)
def get_add_url(self):
return '%s?%s' % (
reverse('admin:auth_user_add'),
reverse('admin:auth_user_add',
current_app=self.admin_site.name),
self.get_preserved_filters_querystring(),
)
@ -4221,7 +4224,8 @@ class AdminKeepChangeListFiltersTests(TestCase):
if user_id is None:
user_id = self.get_sample_user_id()
return "%s?%s" % (
reverse('admin:auth_user_change', args=(user_id,)),
reverse('admin:auth_user_change', args=(user_id,),
current_app=self.admin_site.name),
self.get_preserved_filters_querystring(),
)
@ -4229,7 +4233,8 @@ class AdminKeepChangeListFiltersTests(TestCase):
if user_id is None:
user_id = self.get_sample_user_id()
return "%s?%s" % (
reverse('admin:auth_user_history', args=(user_id,)),
reverse('admin:auth_user_history', args=(user_id,),
current_app=self.admin_site.name),
self.get_preserved_filters_querystring(),
)
@ -4237,7 +4242,8 @@ class AdminKeepChangeListFiltersTests(TestCase):
if user_id is None:
user_id = self.get_sample_user_id()
return "%s?%s" % (
reverse('admin:auth_user_delete', args=(user_id,)),
reverse('admin:auth_user_delete', args=(user_id,),
current_app=self.admin_site.name),
self.get_preserved_filters_querystring(),
)
@ -4331,3 +4337,6 @@ class AdminKeepChangeListFiltersTests(TestCase):
# Test redirect on "Delete".
response = self.client.post(self.get_delete_url(), {'post': 'yes'})
self.assertRedirects(response, self.get_changelist_url())
class NamespacedAdminKeepChangeListFiltersTests(AdminKeepChangeListFiltersTests):
admin_site = site2

View File

@ -12,4 +12,5 @@ urlpatterns = patterns('',
(r'^test_admin/admin2/', include(customadmin.site.urls)),
(r'^test_admin/admin3/', include(admin.site.urls), dict(form_url='pony')),
(r'^test_admin/admin4/', include(customadmin.simple_site.urls)),
(r'^test_admin/admin5/', include(admin.site2.urls)),
)