web/lib/django/contrib/messages/middleware.py
changeset 29 cc9b7e14412b
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
       
     1 from django.conf import settings
       
     2 from django.contrib.messages.storage import default_storage
       
     3 
       
     4 
       
     5 class MessageMiddleware(object):
       
     6     """
       
     7     Middleware that handles temporary messages.
       
     8     """
       
     9 
       
    10     def process_request(self, request):
       
    11         request._messages = default_storage(request)
       
    12 
       
    13     def process_response(self, request, response):
       
    14         """
       
    15         Updates the storage backend (i.e., saves the messages).
       
    16 
       
    17         If not all messages could not be stored and ``DEBUG`` is ``True``, a
       
    18         ``ValueError`` is raised.
       
    19         """
       
    20         # A higher middleware layer may return a request which does not contain
       
    21         # messages storage, so make no assumption that it will be there.
       
    22         if hasattr(request, '_messages'):
       
    23             unstored_messages = request._messages.update(response)
       
    24             if unstored_messages and settings.DEBUG:
       
    25                 raise ValueError('Not all temporary messages could be stored.')
       
    26         return response