summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorSjoerd Langkemper <sjoerd@byte.nl>2013-11-07 16:50:51 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2013-11-11 17:58:02 +0100
commitd87127655f540747e7dc83badc015ea520b880f5 (patch)
treee1f8ff285a3a0f49c3b1f11d745a311a8e28db87 /django
parentf67cce0434907ef00db25577d46fdd04c0ad765d (diff)
Fixed #21421 -- Added level_tag attribute on messages.
Exposing the level name (e.g. "info") makes it possible to prepend something to the class name. For example, Twitter Bootstrap has an alert-info class. This class can now be added to the message using `class="alert-{{ message.level_tag }}". Because the level_tag was on the end of the `tags` property, it could not be used in this fashion when extra_tags were given.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/messages/storage/base.py14
-rw-r--r--django/contrib/messages/tests/base.py9
2 files changed, 17 insertions, 6 deletions
diff --git a/django/contrib/messages/storage/base.py b/django/contrib/messages/storage/base.py
index f682a9bbc3..43dd3dbdd2 100644
--- a/django/contrib/messages/storage/base.py
+++ b/django/contrib/messages/storage/base.py
@@ -40,18 +40,20 @@ class Message(object):
return force_text(self.message)
def _get_tags(self):
- label_tag = force_text(LEVEL_TAGS.get(self.level, ''),
- strings_only=True)
extra_tags = force_text(self.extra_tags, strings_only=True)
- if extra_tags and label_tag:
- return ' '.join([extra_tags, label_tag])
+ if extra_tags and self.level_tag:
+ return ' '.join([extra_tags, self.level_tag])
elif extra_tags:
return extra_tags
- elif label_tag:
- return label_tag
+ elif self.level_tag:
+ return self.level_tag
return ''
tags = property(_get_tags)
+ @property
+ def level_tag(self):
+ return force_text(LEVEL_TAGS.get(self.level, ''), strings_only=True)
+
class BaseStorage(object):
"""
diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py
index cae2e09412..1b1a9c0048 100644
--- a/django/contrib/messages/tests/base.py
+++ b/django/contrib/messages/tests/base.py
@@ -361,6 +361,15 @@ class BaseTests(object):
['info', '', 'extra-tag debug', 'warning', 'error',
'success'])
+ def test_level_tag(self):
+ storage = self.get_storage()
+ storage.level = 0
+ add_level_messages(storage)
+ tags = [msg.level_tag for msg in storage]
+ self.assertEqual(tags,
+ ['info', '', 'debug', 'warning', 'error',
+ 'success'])
+
@override_settings_tags(MESSAGE_TAGS={
constants.INFO: 'info',
constants.DEBUG: '',