Skip to content

Commit

Permalink
[2.2.x] Fixed #30312 -- Relaxed admin check from django.contrib.sessi…
Browse files Browse the repository at this point in the history
…ons to SessionMiddleware subclasses.

Backport of efeceba from master
  • Loading branch information
akx authored and felixxm committed Apr 26, 2019
1 parent 3c3df7d commit a4095da
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
7 changes: 6 additions & 1 deletion django/contrib/admin/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def check_dependencies(**kwargs):
('django.contrib.contenttypes', 401),
('django.contrib.auth', 405),
('django.contrib.messages', 406),
('django.contrib.sessions', 407),
)
for app_name, error_code in app_dependencies:
if not apps.is_installed(app_name):
Expand Down Expand Up @@ -121,6 +120,12 @@ def check_dependencies(**kwargs):
"be in MIDDLEWARE in order to use the admin application.",
id='admin.E409',
))
if not _contains_subclass('django.contrib.sessions.middleware.SessionMiddleware', settings.MIDDLEWARE):
errors.append(checks.Error(
"'django.contrib.sessions.middleware.SessionMiddleware' must "
"be in MIDDLEWARE in order to use the admin application.",
id='admin.E410',
))
return errors


Expand Down
4 changes: 2 additions & 2 deletions docs/ref/checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -657,13 +657,13 @@ The following checks are performed on the default
:setting:`INSTALLED_APPS` in order to use the admin application.
* **admin.E406**: :mod:`django.contrib.messages` must be in
:setting:`INSTALLED_APPS` in order to use the admin application.
* **admin.E407**: :mod:`django.contrib.sessions` must be in
:setting:`INSTALLED_APPS` in order to use the admin application.
* **admin.E408**:
:class:`django.contrib.auth.middleware.AuthenticationMiddleware` must be in
:setting:`MIDDLEWARE` in order to use the admin application.
* **admin.E409**: :class:`django.contrib.messages.middleware.MessageMiddleware`
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.

``auth``
--------
Expand Down
6 changes: 6 additions & 0 deletions docs/releases/2.2.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ Bugfixes

* Fixed a regression in Django 2.2 that caused an exception to be raised when
a custom error handler could not be imported (:ticket:`30318`).

* Relaxed the system check added in Django 2.2 for the admin app's dependencies
to reallow use of
:class:`~django.contrib.sessions.middleware.SessionMiddleware` subclasses,
rather than requiring :mod:`django.contrib.sessions` to be in
:setting:`INSTALLED_APPS` (:ticket:`30312`).
20 changes: 13 additions & 7 deletions tests/admin_checks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth.middleware import AuthenticationMiddleware
from django.contrib.contenttypes.admin import GenericStackedInline
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.core import checks
from django.test import SimpleTestCase, override_settings

Expand Down Expand Up @@ -52,13 +53,16 @@ class ModelBackendSubclass(ModelBackend):
pass


class SessionMiddlewareSubclass(SessionMiddleware):
pass


@override_settings(
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
INSTALLED_APPS=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'admin_checks',
],
Expand Down Expand Up @@ -93,11 +97,6 @@ def test_apps_dependencies(self):
"to use the admin application.",
id='admin.E406',
),
checks.Error(
"'django.contrib.sessions' must be in INSTALLED_APPS in order "
"to use the admin application.",
id='admin.E407',
)
]
self.assertEqual(errors, expected)

Expand Down Expand Up @@ -201,13 +200,19 @@ def test_middleware_dependencies(self):
"'django.contrib.messages.middleware.MessageMiddleware' "
"must be in MIDDLEWARE in order to use the admin application.",
id='admin.E409',
)
),
checks.Error(
"'django.contrib.sessions.middleware.SessionMiddleware' "
"must be in MIDDLEWARE in order to use the admin application.",
id='admin.E410',
),
]
self.assertEqual(errors, expected)

@override_settings(MIDDLEWARE=[
'admin_checks.tests.AuthenticationMiddlewareSubclass',
'admin_checks.tests.MessageMiddlewareSubclass',
'admin_checks.tests.SessionMiddlewareSubclass',
])
def test_middleware_subclasses(self):
self.assertEqual(admin.checks.check_dependencies(), [])
Expand All @@ -216,6 +221,7 @@ def test_middleware_subclasses(self):
'django.contrib.does.not.Exist',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
])
def test_admin_check_ignores_import_error_in_middleware(self):
self.assertEqual(admin.checks.check_dependencies(), [])
Expand Down
2 changes: 1 addition & 1 deletion tests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,13 +1173,13 @@ def test_complex_app(self):
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
],
sdict={
'DEBUG': True,
'MIDDLEWARE': [
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
],
'TEMPLATES': [
{
Expand Down

0 comments on commit a4095da

Please sign in to comment.