Refs #32508 -- Raised ImproperlyConfigured instead of using "assert" in SessionStorage.

This commit is contained in:
Daniyal 2021-03-16 12:34:06 +05:30 committed by Mariusz Felisiak
parent 330bc402a8
commit 54d9179540
2 changed files with 9 additions and 5 deletions

View File

@ -4,6 +4,7 @@ from django.contrib.messages.storage.base import BaseStorage
from django.contrib.messages.storage.cookie import (
MessageDecoder, MessageEncoder,
)
from django.core.exceptions import ImproperlyConfigured
class SessionStorage(BaseStorage):
@ -13,10 +14,12 @@ class SessionStorage(BaseStorage):
session_key = '_messages'
def __init__(self, request, *args, **kwargs):
assert hasattr(request, 'session'), "The session-based temporary "\
"message storage requires session middleware to be installed, "\
"and come before the message middleware in the "\
"MIDDLEWARE list."
if not hasattr(request, 'session'):
raise ImproperlyConfigured(
'The session-based temporary message storage requires session '
'middleware to be installed, and come before the message '
'middleware in the MIDDLEWARE list.'
)
super().__init__(request, *args, **kwargs)
def _get(self, *args, **kwargs):

View File

@ -1,6 +1,7 @@
from django.contrib.messages import constants
from django.contrib.messages.storage.base import Message
from django.contrib.messages.storage.session import SessionStorage
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.test import TestCase
from django.utils.safestring import SafeData, mark_safe
@ -41,7 +42,7 @@ class SessionTests(BaseTests, TestCase):
'middleware to be installed, and come before the message '
'middleware in the MIDDLEWARE list.'
)
with self.assertRaisesMessage(AssertionError, msg):
with self.assertRaisesMessage(ImproperlyConfigured, msg):
self.storage_class(HttpRequest())
def test_get(self):