def test_custom_tags(self): settings.NOTIFICATIONS_TAGS = { constants.INFO: 'info', constants.DEBUG: '', constants.WARNING: '', constants.ERROR: 'bad', 29: 'magic' } # LEVEL_TAGS is a constant defined in django_notify.storage.base # module, so after changing settings.NOTIFICATIONS_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', 'magic', 'extra-tag', '', 'bad', 'success']) finally: # Ensure the level tags constant is put back like we found it. self.restore_setting('NOTIFICATIONS_TAGS') base.LEVEL_TAGS = utils.get_level_tags()
def test_custom_tags(self): settings.NOTIFICATIONS_TAGS = { constants.INFO: 'info', constants.DEBUG: '', constants.WARNING: '', constants.ERROR: 'bad', 29: 'magic' } # LEVEL_TAGS is a constant defined in django_notify.storage.base # module, so after changing settings.NOTIFICATIONS_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', 'magic', 'extra-tag', '', 'bad', 'success']) finally: # Ensure the level tags constant is put back like we found it. self.restore_setting('NOTIFICATIONS_TAGS') base.LEVEL_TAGS = utils.get_level_tags()
from django.conf import settings from django.utils.encoding import force_unicode, StrAndUnicode from django_notify import constants, utils LEVEL_TAGS = utils.get_level_tags() class Notification(StrAndUnicode): """ A notification message. """ def __init__(self, message, level=None, extra_tags=None): self.message = message self.extra_tags = extra_tags if level is None: self.level = constants.INFO else: self.level = int(level) def _prepare(self): """ Prepare the notification for serialization by forcing the ``message`` and ``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)