[3.1.x] Fixed #31575 -- Added system check for admin sidebar request context processor dependency.

Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>

Backport of d522b51c40 from master
This commit is contained in:
Jon Dufresne 2020-05-14 10:27:35 +02:00 committed by Mariusz Felisiak
parent c9a9d042e5
commit 1c2d0fdf3e
7 changed files with 52 additions and 3 deletions

View File

@ -59,6 +59,7 @@ def check_dependencies(**kwargs):
"""
Check that the admin's dependencies are correctly installed.
"""
from django.contrib.admin.sites import all_sites
if not apps.is_installed('django.contrib.admin'):
return []
errors = []
@ -105,6 +106,15 @@ def check_dependencies(**kwargs):
"the admin application.",
id='admin.E404',
))
sidebar_enabled = any(site.enable_nav_sidebar for site in all_sites)
if (sidebar_enabled and 'django.template.context_processors.request'
not in django_templates_instance.context_processors):
errors.append(checks.Warning(
"'django.template.context_processors.request' must be enabled "
"in DjangoTemplates (TEMPLATES) in order to use the admin "
"navigation sidebar.",
id='admin.W411',
))
if not _contains_subclass('django.contrib.auth.middleware.AuthenticationMiddleware', settings.MIDDLEWARE):
errors.append(checks.Error(

View File

@ -734,6 +734,9 @@ The following checks are performed on the default
must be in :setting:`MIDDLEWARE` in order to use the admin application.
* **admin.E410**: :class:`django.contrib.sessions.middleware.SessionMiddleware`
must be in :setting:`MIDDLEWARE` in order to use the admin application.
* **admin.W411**: ``django.template.context_processors.request`` must be
enabled in :class:`~django.template.backends.django.DjangoTemplates`
(:setting:`TEMPLATES`) in order to use the admin navigation sidebar.
``auth``
--------

View File

@ -134,6 +134,12 @@ class SystemChecksTestCase(SimpleTestCase):
"be enabled in DjangoTemplates (TEMPLATES) in order to use "
"the admin application.",
id='admin.E404',
),
checks.Warning(
"'django.template.context_processors.request' must be enabled "
"in DjangoTemplates (TEMPLATES) in order to use the admin "
"navigation sidebar.",
id='admin.W411',
)
]
self.assertEqual(admin.checks.check_dependencies(), expected)
@ -150,7 +156,10 @@ class SystemChecksTestCase(SimpleTestCase):
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': ['django.contrib.messages.context_processors.messages'],
'context_processors': [
'django.template.context_processors.request',
'django.contrib.messages.context_processors.messages',
],
},
}],
)
@ -177,6 +186,7 @@ class SystemChecksTestCase(SimpleTestCase):
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],

View File

@ -1124,6 +1124,7 @@ class ManageCheck(AdminScriptTestCase):
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],

View File

@ -51,9 +51,31 @@ class AdminSidebarTests(TestCase):
self.assertNotContains(response, '<nav class="sticky" id="nav-sidebar">')
def test_sidebar_aria_current_page(self):
response = self.client.get(reverse('test_with_sidebar:auth_user_changelist'))
url = reverse('test_with_sidebar:auth_user_changelist')
response = self.client.get(url)
self.assertContains(response, '<nav class="sticky" id="nav-sidebar">')
self.assertContains(response, 'aria-current="page">Users</a>')
self.assertContains(response, '<a href="%s" aria-current="page">Users</a>' % url)
@override_settings(
TEMPLATES=[{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
}]
)
def test_sidebar_aria_current_page_missing_without_request_context_processor(self):
url = reverse('test_with_sidebar:auth_user_changelist')
response = self.client.get(url)
self.assertContains(response, '<nav class="sticky" id="nav-sidebar">')
# Does not include aria-current attribute.
self.assertContains(response, '<a href="%s">Users</a>' % url)
self.assertNotContains(response, 'aria-current')
@override_settings(ROOT_URLCONF='admin_views.test_nav_sidebar')

View File

@ -1425,6 +1425,7 @@ def get_perm(Model, codename):
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
@ -2424,6 +2425,7 @@ class AdminViewPermissionsTest(TestCase):
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],

View File

@ -11,6 +11,7 @@ AUTH_TEMPLATES = [{
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],