Skip to content

Commit

Permalink
Fixed #20610: Added a message level dict to contrib.message context p…
Browse files Browse the repository at this point in the history
…rocessor.
  • Loading branch information
bmispelon committed Oct 30, 2013
1 parent e9a356a commit 9fde42a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions django/contrib/messages/constants.py
Expand Up @@ -11,3 +11,11 @@
WARNING: 'warning', WARNING: 'warning',
ERROR: 'error', ERROR: 'error',
} }

DEFAULT_LEVELS = {
'DEBUG': DEBUG,
'INFO': INFO,
'SUCCESS': SUCCESS,
'WARNING': WARNING,
'ERROR': ERROR,
}
6 changes: 5 additions & 1 deletion django/contrib/messages/context_processors.py
@@ -1,8 +1,12 @@
from django.contrib.messages.api import get_messages from django.contrib.messages.api import get_messages
from django.contrib.messages.constants import DEFAULT_LEVELS




def messages(request): def messages(request):
""" """
Returns a lazy 'messages' context variable. Returns a lazy 'messages' context variable.
""" """
return {'messages': get_messages(request)} return {
'messages': get_messages(request),
'DEFAULT_MESSAGE_LEVELS': DEFAULT_LEVELS,
}
8 changes: 8 additions & 0 deletions django/contrib/messages/tests/base.py
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings, global_settings from django.conf import settings, global_settings
from django.contrib.messages import constants, utils, get_level, set_level from django.contrib.messages import constants, utils, get_level, set_level
from django.contrib.messages.api import MessageFailure from django.contrib.messages.api import MessageFailure
from django.contrib.messages.constants import DEFAULT_LEVELS
from django.contrib.messages.storage import default_storage, base from django.contrib.messages.storage import default_storage, base
from django.contrib.messages.storage.base import Message from django.contrib.messages.storage.base import Message
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -189,6 +190,13 @@ def test_with_template_response(self):
for msg in data['messages']: for msg in data['messages']:
self.assertNotContains(response, msg) self.assertNotContains(response, msg)


def test_context_processor_message_levels(self):
show_url = reverse('django.contrib.messages.tests.urls.show_template_response')
response = self.client.get(show_url)

self.assertTrue('DEFAULT_MESSAGE_LEVELS' in response.context)
self.assertEqual(response.context['DEFAULT_MESSAGE_LEVELS'], DEFAULT_LEVELS)

@override_settings(MESSAGE_LEVEL=constants.DEBUG) @override_settings(MESSAGE_LEVEL=constants.DEBUG)
def test_multiple_posts(self): def test_multiple_posts(self):
""" """
Expand Down
16 changes: 16 additions & 0 deletions docs/ref/contrib/messages.txt
Expand Up @@ -196,6 +196,22 @@ Even if you know there is only just one message, you should still iterate over
the ``messages`` sequence, because otherwise the message storage will not be cleared the ``messages`` sequence, because otherwise the message storage will not be cleared
for the next request. for the next request.


.. versionadded:: 1.7

The context processor also provides a ``DEFAULT_MESSAGE_LEVELS`` variable which
is a mapping of the message level names to their numeric value::

{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}Important: {% endif %}
{{ message }}
</li>
{% endfor %}
</ul>
{% endif %}

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


Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.7.txt
Expand Up @@ -203,6 +203,9 @@ Minor features
follow the :setting:`SESSION_COOKIE_SECURE` and follow the :setting:`SESSION_COOKIE_SECURE` and
:setting:`SESSION_COOKIE_HTTPONLY` settings. :setting:`SESSION_COOKIE_HTTPONLY` settings.


* The :ref:`messages context processor <message-displaying>` now adds a
dictionary of default levels under the name ``DEFAULT_MESSAGE_LEVELS``.

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


Expand Down

0 comments on commit 9fde42a

Please sign in to comment.