def test_custom_tags(self): settings.MESSAGE_TAGS = { constants.INFO: "info", constants.DEBUG: "", constants.WARNING: "", constants.ERROR: "bad", 29: "custom", } # LEVEL_TAGS is a constant defined in the # django_messages_framework.storage.base module, so after changing # settings.MESSAGE_TAGS, we need to update that constant too. base.LEVEL_TAGS = utils.get_level_tags() try: storage = self.get_storage() storage.level = 0 add_level_messages(storage) tags = [msg.tags for msg in storage] self.assertEqual(tags, ["info", "custom", "extra-tag", "", "bad", "success"]) finally: # Ensure the level tags constant is put back like we found it. self.restore_setting("MESSAGE_TAGS") base.LEVEL_TAGS = utils.get_level_tags()
from django.conf import settings from django.utils.encoding import force_unicode, StrAndUnicode from django_messages_framework import constants, utils LEVEL_TAGS = utils.get_level_tags() class Message(StrAndUnicode): """ Represents an actual message that can be stored in any of the supported storage classes (typically session- or cookie-based) and rendered in a view or template. """ def __init__(self, level, message, extra_tags=None): self.level = int(level) self.message = message self.extra_tags = extra_tags def _prepare(self): """ Prepares the message for serialization by forcing the ``message`` and ``extra_tags`` to unicode in case they are lazy translations. Known "safe" types (None, int, etc.) are not converted (see Django's ``force_unicode`` implementation for details). """ self.message = force_unicode(self.message, strings_only=True) self.extra_tags = force_unicode(self.extra_tags, strings_only=True)