Skip to content

Commit

Permalink
Fixed #21421 -- Added level_tag attribute on messages.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sjoerd Langkemper authored and bmispelon committed Nov 11, 2013
1 parent f67cce0 commit d871276
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
14 changes: 8 additions & 6 deletions django/contrib/messages/storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ def __str__(self):
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):
"""
Expand Down
9 changes: 9 additions & 0 deletions django/contrib/messages/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ def test_tags(self):
['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: '',
Expand Down
26 changes: 26 additions & 0 deletions docs/ref/contrib/messages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ is a mapping of the message level names to their numeric value::
</ul>
{% endif %}

The ``Message`` class
---------------------

.. class:: storage.base.Message

When you loop over the list of messages in a template, what you get are
instances of the ``Message`` class. It's quite a simple object, with only a
few attributes:

* ``message``: The actual text of the message.

* ``level``: An integer describing the type of the message (see the
`message levels`_ section above).

* ``tags``: A string combining all the message's tags (``extra_tags`` and
``level_tag``) separated by spaces.

* ``extra_tags``: A string containing custom tags for this message,
separated by spaces. It's empty by default.

.. versionadded:: 1.7

* ``level_tag``: The string representation of the level. By default, it's
the lowercase version of the name of the associated constant, but this
can be changed if you need by using the :setting:`MESSAGE_TAGS` setting.

Creating custom message levels
------------------------------

Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ Minor features
* The :ref:`messages context processor <message-displaying>` now adds a
dictionary of default levels under the name ``DEFAULT_MESSAGE_LEVELS``.

* :class:`~django.contrib.messages.storage.base.Message` objects now have a
``level_tag`` attribute that contains the string representation of the
message level.

:mod:`django.contrib.redirects`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down

0 comments on commit d871276

Please sign in to comment.