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

View File

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