Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12575 - created a better interface for getting/setting the eff…

…ective level of contrib.messages

Thanks Chris Beaven.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12207 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c56beed24061bf94b67be21806fa654a1e180b5f 1 parent 2f9853b
Luke Plant authored January 12, 2010
29  django/contrib/messages/api.py
... ...
@@ -1,4 +1,5 @@
1 1
 from django.contrib.messages import constants
  2
+from django.contrib.messages.storage import default_storage
2 3
 from django.utils.functional import lazy, memoize
3 4
 
4 5
 __all__ = (
@@ -44,6 +45,34 @@ def get_user():
44 45
     return lazy(memoize(get_user().get_and_delete_messages, {}, 0), list)()
45 46
 
46 47
 
  48
+def get_level(request):
  49
+    """
  50
+    Returns the minimum level of messages to be recorded.
  51
+
  52
+    The default level is the ``MESSAGE_LEVEL`` setting. If this is not found,
  53
+    the ``INFO`` level is used.
  54
+    """
  55
+    if hasattr(request, '_messages'):
  56
+        storage = request._messages
  57
+    else:
  58
+        storage = default_storage(request)
  59
+    return storage.level
  60
+
  61
+
  62
+def set_level(request, level):
  63
+    """
  64
+    Sets the minimum level of messages to be recorded, returning ``True`` if
  65
+    the level was recorded successfully.
  66
+
  67
+    If set to ``None``, the default level will be used (see the ``get_level``
  68
+    method).
  69
+    """
  70
+    if not hasattr(request, '_messages'):
  71
+        return False
  72
+    request._messages.level = level
  73
+    return True
  74
+
  75
+
47 76
 def debug(request, message, extra_tags='', fail_silently=False):
48 77
     """
49 78
     Adds a message with the ``DEBUG`` level.
42  django/contrib/messages/tests/base.py
@@ -3,11 +3,11 @@
3 3
 from django.conf import settings
4 4
 from django.utils.translation import ugettext_lazy
5 5
 from django.contrib.messages import constants, utils
  6
+from django.contrib.messages.api import MessageFailure, get_level, set_level
6 7
 from django.contrib.messages.storage import default_storage, base
7 8
 from django.contrib.messages.storage.base import Message
8 9
 from django.core.urlresolvers import reverse
9 10
 from django.contrib.auth.models import User
10  
-from django.contrib.messages.api import MessageFailure
11 11
 
12 12
 
13 13
 def add_level_messages(storage):
@@ -41,16 +41,19 @@ def setUp(self):
41 41
             if hasattr(settings, setting):
42 42
                 self._remembered_settings[setting] = getattr(settings, setting)
43 43
                 delattr(settings._wrapped, setting)
44  
-        # backup these manually because we do not want them deleted
  44
+        # Backup these manually because we do not want them deleted.
45 45
         self._middleware_classes = settings.MIDDLEWARE_CLASSES
46 46
         self._template_context_processors = \
47 47
            settings.TEMPLATE_CONTEXT_PROCESSORS
48 48
         self._installed_apps = settings.INSTALLED_APPS
  49
+        self._message_storage = settings.MESSAGE_STORAGE
  50
+        settings.MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__,
  51
+                                              self.storage_class.__name__)
49 52
 
50 53
     def tearDown(self):
51 54
         for setting in self.restore_settings:
52 55
             self.restore_setting(setting)
53  
-        # restore these manually (see above)
  56
+        # Restore these manually (see above).
54 57
         settings.MIDDLEWARE_CLASSES = self._middleware_classes
55 58
         settings.TEMPLATE_CONTEXT_PROCESSORS = \
56 59
            self._template_context_processors
@@ -319,25 +322,48 @@ def test_existing_add(self):
319 322
         self.assert_(storage.added_new)
320 323
 
321 324
     def test_default_level(self):
  325
+        # get_level works even with no storage on the request.
  326
+        request = self.get_request()
  327
+        self.assertEqual(get_level(request), constants.INFO)
  328
+
  329
+        # get_level returns the default level if it hasn't been set.
322 330
         storage = self.get_storage()
  331
+        request._messages = storage
  332
+        self.assertEqual(get_level(request), constants.INFO)
  333
+
  334
+        # Only messages of sufficient level get recorded.
323 335
         add_level_messages(storage)
324 336
         self.assertEqual(len(storage), 5)
325 337
 
326 338
     def test_low_level(self):
327  
-        storage = self.get_storage()
328  
-        storage.level = 5
  339
+        request = self.get_request()
  340
+        storage = self.storage_class(request)
  341
+        request._messages = storage
  342
+
  343
+        self.assert_(set_level(request, 5))
  344
+        self.assertEqual(get_level(request), 5)
  345
+
329 346
         add_level_messages(storage)
330 347
         self.assertEqual(len(storage), 6)
331 348
 
332 349
     def test_high_level(self):
333  
-        storage = self.get_storage()
334  
-        storage.level = 30
  350
+        request = self.get_request()
  351
+        storage = self.storage_class(request)
  352
+        request._messages = storage
  353
+
  354
+        self.assert_(set_level(request, 30))
  355
+        self.assertEqual(get_level(request), 30)
  356
+
335 357
         add_level_messages(storage)
336 358
         self.assertEqual(len(storage), 2)
337 359
 
338 360
     def test_settings_level(self):
  361
+        request = self.get_request()
  362
+        storage = self.storage_class(request)
  363
+
339 364
         settings.MESSAGE_LEVEL = 29
340  
-        storage = self.get_storage()
  365
+        self.assertEqual(get_level(request), 29)
  366
+
341 367
         add_level_messages(storage)
342 368
         self.assertEqual(len(storage), 3)
343 369
 
22  docs/ref/contrib/messages.txt
@@ -137,8 +137,11 @@ Constant    Purpose
137 137
 ``ERROR``   An action was **not** successful or some other failure occurred
138 138
 =========== ========
139 139
 
140  
-The `MESSAGE_LEVEL`_ setting can be used to change the minimum recorded
141  
-level. Attempts to add messages of a level less than this will be ignored.
  140
+The `MESSAGE_LEVEL`_ setting can be used to change the minimum recorded level
  141
+(or it can be `changed per request`_). Attempts to add messages of a level less
  142
+than this will be ignored.
  143
+
  144
+.. _`changed per request`: `Changing the minimum recorded level per-request`_
142 145
 
143 146
 Message tags
144 147
 ------------
@@ -245,22 +248,27 @@ provide a mapping via the `MESSAGE_TAGS`_ setting.
245 248
 Changing the minimum recorded level per-request
246 249
 -----------------------------------------------
247 250
 
248  
-The minimum recorded level can be set per request by changing the ``level``
249  
-attribute of the messages storage instance::
  251
+The minimum recorded level can be set per request via the ``set_level``
  252
+method::
250 253
 
251 254
     from django.contrib import messages
252 255
 
253 256
     # Change the messages level to ensure the debug message is added.
254  
-    messages.get_messages(request).level = messages.DEBUG
  257
+    messages.set_level(request, messages.DEBUG)
255 258
     messages.debug(request, 'Test message...')
256 259
 
257 260
     # In another request, record only messages with a level of WARNING and higher
258  
-    messages.get_messages(request).level = messages.WARNING
  261
+    messages.set_level(request, messages.WARNING)
259 262
     messages.success(request, 'Your profile was updated.') # ignored
260 263
     messages.warning(request, 'Your account is about to expire.') # recorded
261 264
 
262 265
     # Set the messages level back to default.
263  
-    messages.get_messages(request).level = None
  266
+    messages.set_level(request, None)
  267
+
  268
+Similarly, the current effective level can be retrieved with ``get_level``::
  269
+
  270
+    from django.contrib import messages
  271
+    current_level = messages.get_level(request)
264 272
 
265 273
 For more information on how the minimum recorded level functions, see
266 274
 `Message levels`_ above.

0 notes on commit c56beed

Please sign in to comment.
Something went wrong with that request. Please try again.