Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed #20610: Added a message level dict to contrib.message context proc... #1822

Closed
wants to merge 1 commit into from

2 participants

@bmispelon
Collaborator

...essor.

@timgraham timgraham commented on the diff
django/contrib/messages/constants.py
@@ -11,3 +11,11 @@
WARNING: 'warning',
ERROR: 'error',
}
+
+DEFAULT_LEVELS = {
@timgraham Owner

do you think it would make sense to do something like:

DEFAULT_LEVELS = dict((v.upper(), k) for (k, v) in DEFAULT_TAGS.items())
@bmispelon Collaborator

Technically that would work but I don't think it's correct.

The DEFAULT_TAGS are used for CSS classes and there's no guarantee that they match the name of the variable.
It would make it a bit more "DRY" but I think the small size of this file combined with the proximity of the two variables makes it OK to be a bit more verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff
docs/ref/contrib/messages.txt
@@ -196,6 +196,20 @@ 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
for the next request.
+The context processor also provides a ``DEFAULT_MESSAGE_LEVELS`` variable which
@timgraham Owner

.. versionadded:: 1.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bmispelon
Collaborator

Merged in 9fde42a.

@bmispelon bmispelon closed this
@bmispelon bmispelon deleted the bmispelon:ticket-20610 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2013
  1. @bmispelon
This page is out of date. Refresh to see the latest.
View
8 django/contrib/messages/constants.py
@@ -11,3 +11,11 @@
WARNING: 'warning',
ERROR: 'error',
}
+
+DEFAULT_LEVELS = {
@timgraham Owner

do you think it would make sense to do something like:

DEFAULT_LEVELS = dict((v.upper(), k) for (k, v) in DEFAULT_TAGS.items())
@bmispelon Collaborator

Technically that would work but I don't think it's correct.

The DEFAULT_TAGS are used for CSS classes and there's no guarantee that they match the name of the variable.
It would make it a bit more "DRY" but I think the small size of this file combined with the proximity of the two variables makes it OK to be a bit more verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 'DEBUG': DEBUG,
+ 'INFO': INFO,
+ 'SUCCESS': SUCCESS,
+ 'WARNING': WARNING,
+ 'ERROR': ERROR,
+}
View
6 django/contrib/messages/context_processors.py
@@ -1,8 +1,12 @@
from django.contrib.messages.api import get_messages
+from django.contrib.messages.constants import DEFAULT_LEVELS
def messages(request):
"""
Returns a lazy 'messages' context variable.
"""
- return {'messages': get_messages(request)}
+ return {
+ 'messages': get_messages(request),
+ 'DEFAULT_MESSAGE_LEVELS': DEFAULT_LEVELS,
+ }
View
8 django/contrib/messages/tests/base.py
@@ -4,6 +4,7 @@
from django.conf import settings, global_settings
from django.contrib.messages import constants, utils, get_level, set_level
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.base import Message
from django.core.urlresolvers import reverse
@@ -189,6 +190,13 @@ def test_with_template_response(self):
for msg in data['messages']:
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)
def test_multiple_posts(self):
"""
View
14 docs/ref/contrib/messages.txt
@@ -196,6 +196,20 @@ 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
for the next request.
+The context processor also provides a ``DEFAULT_MESSAGE_LEVELS`` variable which
@timgraham Owner

.. versionadded:: 1.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+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
------------------------------
View
3  docs/releases/1.7.txt
@@ -199,6 +199,9 @@ Minor features
follow the :setting:`SESSION_COOKIE_SECURE` and
: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`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Something went wrong with that request. Please try again.