Ticket #12575: 12575.diff

File 12575.diff, 7.1 KB (added by Chris Beaven, 15 years ago)

patch with docs and fixed tests (to reflect api changes)

  • django/contrib/messages/api.py

    diff --git a/django/contrib/messages/api.py b/django/contrib/messages/api.py
    index 4164f27..7159737 100644
    a b  
    11from django.contrib.messages import constants
     2from django.contrib.messages.storage import default_storage
    23from django.utils.functional import lazy, memoize
    34
    45__all__ = (
    def get_messages(request):  
    4445    return lazy(memoize(get_user().get_and_delete_messages, {}, 0), list)()
    4546
    4647
     48def 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
     62def 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
    4776def debug(request, message, extra_tags='', fail_silently=False):
    4877    """
    4978    Adds a message with the ``DEBUG`` level.
  • django/contrib/messages/tests/base.py

    diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py
    index 6ca9185..d0edce9 100644
    a b from django.test import TestCase  
    33from django.conf import settings
    44from django.utils.translation import ugettext_lazy
    55from django.contrib.messages import constants, utils
     6from django.contrib.messages.api import MessageFailure, get_level, set_level
    67from django.contrib.messages.storage import default_storage, base
    78from django.contrib.messages.storage.base import Message
    89from django.core.urlresolvers import reverse
    910from django.contrib.auth.models import User
    10 from django.contrib.messages.api import MessageFailure
    1111
    1212
    1313def add_level_messages(storage):
    class BaseTest(TestCase):  
    4141            if hasattr(settings, setting):
    4242                self._remembered_settings[setting] = getattr(settings, setting)
    4343                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.
    4545        self._middleware_classes = settings.MIDDLEWARE_CLASSES
    4646        self._template_context_processors = \
    4747           settings.TEMPLATE_CONTEXT_PROCESSORS
    4848        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__)
    4952
    5053    def tearDown(self):
    5154        for setting in self.restore_settings:
    5255            self.restore_setting(setting)
    53         # restore these manually (see above)
     56        # Restore these manually (see above).
    5457        settings.MIDDLEWARE_CLASSES = self._middleware_classes
    5558        settings.TEMPLATE_CONTEXT_PROCESSORS = \
    5659           self._template_context_processors
    class BaseTest(TestCase):  
    319322        self.assert_(storage.added_new)
    320323
    321324    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.
    322330        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.
    323335        add_level_messages(storage)
    324336        self.assertEqual(len(storage), 5)
    325337
    326338    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
    329346        add_level_messages(storage)
    330347        self.assertEqual(len(storage), 6)
    331348
    332349    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
    335357        add_level_messages(storage)
    336358        self.assertEqual(len(storage), 2)
    337359
    338360    def test_settings_level(self):
     361        request = self.get_request()
     362        storage = self.storage_class(request)
     363
    339364        settings.MESSAGE_LEVEL = 29
    340         storage = self.get_storage()
     365        self.assertEqual(get_level(request), 29)
     366
    341367        add_level_messages(storage)
    342368        self.assertEqual(len(storage), 3)
    343369
  • docs/ref/contrib/messages.txt

    diff --git a/docs/ref/contrib/messages.txt b/docs/ref/contrib/messages.txt
    index 7cfe4ae..554e70b 100644
    a b Constant Purpose  
    137137``ERROR``   An action was **not** successful or some other failure occurred
    138138=========== ========
    139139
    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.
     140The `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
     142than this will be ignored.
     143
     144.. _`changed per request`: `Changing the minimum recorded level per-request`_
    142145
    143146Message tags
    144147------------
    provide a mapping via the `MESSAGE_TAGS`_ setting.  
    245248Changing the minimum recorded level per-request
    246249-----------------------------------------------
    247250
    248 The minimum recorded level can be set per request by changing the ``level``
    249 attribute of the messages storage instance::
     251The minimum recorded level can be set per request via the ``set_level``
     252method::
    250253
    251254    from django.contrib import messages
    252255
    253256    # 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)
    255258    messages.debug(request, 'Test message...')
    256259
    257260    # 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)
    259262    messages.success(request, 'Your profile was updated.') # ignored
    260263    messages.warning(request, 'Your account is about to expire.') # recorded
    261264
    262265    # Set the messages level back to default.
    263     messages.get_messages(request).level = None
     266    messages.set_level(request, None)
     267
     268Similarly, the current effective level can be retrieved with ``get_level``::
     269
     270    from django.contrib import messages
     271    current_level = messages.get_level(request)
    264272
    265273For more information on how the minimum recorded level functions, see
    266274`Message levels`_ above.
Back to Top