Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-mm committed Oct 24, 2023
2 parents 545e752 + 311718f commit 561be33
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion django/contrib/messages/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from django.contrib.messages.storage import base
from django.contrib.messages.utils import get_level_tags
from django.core.signals import setting_changed
from django.utils.functional import SimpleLazyObject
from django.utils.translation import gettext_lazy as _


def update_level_tags(setting, **kwargs):
if setting == "MESSAGE_TAGS":
base.LEVEL_TAGS = get_level_tags()
base.LEVEL_TAGS = SimpleLazyObject(get_level_tags)


class MessagesConfig(AppConfig):
Expand Down
3 changes: 2 additions & 1 deletion django/contrib/messages/storage/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf import settings
from django.contrib.messages import constants, utils
from django.utils.functional import SimpleLazyObject

LEVEL_TAGS = utils.get_level_tags()
LEVEL_TAGS = SimpleLazyObject(utils.get_level_tags)


class Message:
Expand Down
3 changes: 2 additions & 1 deletion django/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ def __call__(self, value):
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.allowed_extensions == other.allowed_extensions
and set(self.allowed_extensions or [])
== set(other.allowed_extensions or [])
and self.message == other.message
and self.code == other.code
)
Expand Down
21 changes: 21 additions & 0 deletions tests/messages_tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import mock

from django.conf import settings
from django.contrib.messages import Message, add_message, constants
from django.contrib.messages.storage import base
from django.contrib.messages.test import MessagesTestMixin
Expand Down Expand Up @@ -63,6 +64,26 @@ class TestLevelTags(SimpleTestCase):
def test_override_settings_level_tags(self):
self.assertEqual(base.LEVEL_TAGS, self.message_tags)

def test_lazy(self):
# Don't use @override_settings to avoid calling the setting_changed
# signal.
old_message_tags = getattr(settings, "MESSAGE_TAGS", None)
settings.MESSAGE_TAGS = {constants.ERROR: "bad"}
try:
self.assertEqual(base.LEVEL_TAGS[constants.ERROR], "bad")
finally:
if old_message_tags is None:
del settings.MESSAGE_TAGS
else:
settings.MESSAGE_TAGS = old_message_tags

@override_settings(MESSAGE_TAGS=message_tags)
def test_override_settings_lazy(self):
# The update_level_tags handler has been called at least once before
# running this code when using @override_settings.
settings.MESSAGE_TAGS = {constants.ERROR: "very-bad"}
self.assertEqual(base.LEVEL_TAGS[constants.ERROR], "very-bad")


class FakeResponse:
def __init__(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/validators/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,10 @@ def test_file_extension_equality(self):
FileExtensionValidator(["TXT", "png"]),
FileExtensionValidator(["txt", "png"]),
)
self.assertEqual(
FileExtensionValidator(["jpg", "png", "txt"]),
FileExtensionValidator(["txt", "jpg", "png"]),
)
self.assertEqual(
FileExtensionValidator(["txt"]),
FileExtensionValidator(["txt"], code="invalid_extension"),
Expand Down

0 comments on commit 561be33

Please sign in to comment.