Fixed #33303 -- Changed messages' level tags on MESSAGE_TAGS setting change.
This commit is contained in:
parent
9a6e2df3a8
commit
24b316536a
|
@ -1,7 +1,18 @@
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from django.contrib.messages.storage import base
|
||||||
|
from django.contrib.messages.utils import get_level_tags
|
||||||
|
from django.test.signals import setting_changed
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
def update_level_tags(setting, **kwargs):
|
||||||
|
if setting == 'MESSAGE_TAGS':
|
||||||
|
base.LEVEL_TAGS = get_level_tags()
|
||||||
|
|
||||||
|
|
||||||
class MessagesConfig(AppConfig):
|
class MessagesConfig(AppConfig):
|
||||||
name = 'django.contrib.messages'
|
name = 'django.contrib.messages'
|
||||||
verbose_name = _("Messages")
|
verbose_name = _("Messages")
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
setting_changed.connect(update_level_tags)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from django.contrib.messages import constants, get_level, set_level, utils
|
from django.contrib.messages import constants, get_level, set_level
|
||||||
from django.contrib.messages.api import MessageFailure
|
from django.contrib.messages.api import MessageFailure
|
||||||
from django.contrib.messages.constants import DEFAULT_LEVELS
|
from django.contrib.messages.constants import DEFAULT_LEVELS
|
||||||
from django.contrib.messages.storage import base, default_storage
|
from django.contrib.messages.storage import default_storage
|
||||||
from django.contrib.messages.storage.base import Message
|
from django.contrib.messages.storage.base import Message
|
||||||
from django.http import HttpRequest, HttpResponse
|
from django.http import HttpRequest, HttpResponse
|
||||||
from django.test import modify_settings, override_settings
|
from django.test import modify_settings, override_settings
|
||||||
|
@ -22,20 +22,6 @@ def add_level_messages(storage):
|
||||||
storage.add(constants.SUCCESS, 'This was a triumph.')
|
storage.add(constants.SUCCESS, 'This was a triumph.')
|
||||||
|
|
||||||
|
|
||||||
class override_settings_tags(override_settings):
|
|
||||||
def enable(self):
|
|
||||||
super().enable()
|
|
||||||
# LEVEL_TAGS is a constant defined in the
|
|
||||||
# django.contrib.messages.storage.base module, so after changing
|
|
||||||
# settings.MESSAGE_TAGS, update that constant also.
|
|
||||||
self.old_level_tags = base.LEVEL_TAGS
|
|
||||||
base.LEVEL_TAGS = utils.get_level_tags()
|
|
||||||
|
|
||||||
def disable(self):
|
|
||||||
super().disable()
|
|
||||||
base.LEVEL_TAGS = self.old_level_tags
|
|
||||||
|
|
||||||
|
|
||||||
class BaseTests:
|
class BaseTests:
|
||||||
storage_class = default_storage
|
storage_class = default_storage
|
||||||
levels = {
|
levels = {
|
||||||
|
@ -47,7 +33,7 @@ class BaseTests:
|
||||||
}
|
}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.settings_override = override_settings_tags(
|
self.settings_override = override_settings(
|
||||||
TEMPLATES=[{
|
TEMPLATES=[{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [],
|
||||||
|
@ -368,7 +354,7 @@ class BaseTests:
|
||||||
tags = [msg.level_tag for msg in storage]
|
tags = [msg.level_tag for msg in storage]
|
||||||
self.assertEqual(tags, ['info', '', 'debug', 'warning', 'error', 'success'])
|
self.assertEqual(tags, ['info', '', 'debug', 'warning', 'error', 'success'])
|
||||||
|
|
||||||
@override_settings_tags(MESSAGE_TAGS={
|
@override_settings(MESSAGE_TAGS={
|
||||||
constants.INFO: 'info',
|
constants.INFO: 'info',
|
||||||
constants.DEBUG: '',
|
constants.DEBUG: '',
|
||||||
constants.WARNING: '',
|
constants.WARNING: '',
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from django.contrib.messages import constants
|
from django.contrib.messages import constants
|
||||||
|
from django.contrib.messages.storage import base
|
||||||
from django.contrib.messages.storage.base import Message
|
from django.contrib.messages.storage.base import Message
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase, override_settings
|
||||||
|
|
||||||
|
|
||||||
class MessageTests(SimpleTestCase):
|
class MessageTests(SimpleTestCase):
|
||||||
|
@ -15,3 +16,18 @@ class MessageTests(SimpleTestCase):
|
||||||
self.assertNotEqual(msg_1, msg_2)
|
self.assertNotEqual(msg_1, msg_2)
|
||||||
self.assertNotEqual(msg_1, msg_3)
|
self.assertNotEqual(msg_1, msg_3)
|
||||||
self.assertNotEqual(msg_2, msg_3)
|
self.assertNotEqual(msg_2, msg_3)
|
||||||
|
|
||||||
|
|
||||||
|
class TestLevelTags(SimpleTestCase):
|
||||||
|
message_tags = {
|
||||||
|
constants.INFO: 'info',
|
||||||
|
constants.DEBUG: '',
|
||||||
|
constants.WARNING: '',
|
||||||
|
constants.ERROR: 'bad',
|
||||||
|
constants.SUCCESS: '',
|
||||||
|
12: 'custom',
|
||||||
|
}
|
||||||
|
|
||||||
|
@override_settings(MESSAGE_TAGS=message_tags)
|
||||||
|
def test_override_settings_level_tags(self):
|
||||||
|
self.assertEqual(base.LEVEL_TAGS, self.message_tags)
|
||||||
|
|
Loading…
Reference in New Issue