Django

Code

Ticket #4604: messages-r8347.diff

File messages-r8347.diff, 12.7 kB (added by john, 2 years ago)

Updated messages.7.diff to avoid rejects with django.core.context_processors.py

  • django/core/context_processors.py

    old new  
    88""" 
    99 
    1010from django.conf import settings 
     11from django.utils.encoding import StrAndUnicode 
    1112 
     13# LazyMessages is used by the `messages` and `auth` context processors. 
     14class LazyMessages(StrAndUnicode): 
     15    """ 
     16    A lazy proxy for session and authentication messages. 
     17    """ 
     18    def __init__(self, request): 
     19        self.request = request 
     20     
     21    def __iter__(self): 
     22        return iter(self.messages) 
     23     
     24    def __len__(self): 
     25        return len(self.messages) 
     26     
     27    def __nonzero__(self): 
     28        return bool(self.messages) 
     29     
     30    def __unicode__(self): 
     31        return unicode(self.messages) 
     32     
     33    def __getitem__(self, *args, **kwargs): 
     34        return self.messages.__getitem__(*args, **kwargs) 
     35     
     36    def _get_messages(self): 
     37        if hasattr(self, '_messages'): 
     38            return self._messages 
     39        # First, retreive any messages for the user. 
     40        if hasattr(self.request, 'user') and \ 
     41           hasattr(self.request.user, 'get_and_delete_messages'): 
     42            self._messages = self.request.user.get_and_delete_messages() 
     43        else: 
     44            self._messages = [] 
     45        # Next, retrieve any messages for the session. 
     46        if hasattr(self.request, 'session'): 
     47            self._messages += self.request.session.get_and_delete_messages() 
     48        return self._messages 
     49    messages = property(_get_messages) 
     50 
    1251def auth(request): 
    1352    """ 
    1453    Returns context variables required by apps that use Django's authentication 
     
    2261    else: 
    2362        from django.contrib.auth.models import AnonymousUser 
    2463        user = AnonymousUser() 
    25     return
     64    context_extras =
    2665        'user': user, 
    27         'messages': user.get_and_delete_messages(), 
    2866        'perms': PermWrapper(user), 
    2967    } 
     68    # Add authentication (and session) LazyMessages to the context too. 
     69    context_extras.update(messages(request)) 
     70    return context_extras 
    3071 
     72def messages(request): 
     73    """ 
     74    Returns messages for the session and the current user. 
     75 
     76    Note that this processor is only useful to use explicity if you are not 
     77    using the (enabled by default) auth processor, as it also provides the 
     78    messages (by calling this method). 
     79 
     80    The messages are lazy loaded, so no messages are retreived and deleted 
     81    unless requested from the template. 
     82 
     83    Both contrib.session and contrib.auth are optional. If neither is provided, 
     84    no 'messages' variable will be added to the context. 
     85    """ 
     86    if hasattr(request, 'session') or hasattr(request, 'user'): 
     87        return {'messages': LazyMessages(request)} 
     88    return {} 
     89 
    3190def debug(request): 
    3291    "Returns context variables helpful for debugging." 
    3392    context_extras = {} 
     
    83142    def __iter__(self): 
    84143        # I am large, I contain multitudes. 
    85144        raise TypeError("PermWrapper is not iterable.") 
     145 
  • django/contrib/sessions/tests.py

    old new  
    1616'dog' 
    1717>>> db_session.pop('some key', 'does not exist') 
    1818'does not exist' 
     19>>> db_session.get_messages() 
     20[] 
     21>>> db_session.create_message('first post') 
     22>>> db_session.get_messages() 
     23['first post'] 
     24>>> db_session.get_and_delete_messages() 
     25['first post'] 
     26>>> db_session.get_and_delete_messages() 
     27[] 
     28>>> db_session.create_message('hello') 
     29>>> db_session.create_message('world') 
     30>>> db_session.get_and_delete_messages() 
     31['hello', 'world'] 
    1932>>> db_session.save() 
    2033>>> db_session.exists(db_session.session_key) 
    2134True 
     
    4659'dog' 
    4760>>> file_session.pop('some key', 'does not exist') 
    4861'does not exist' 
     62>>> file_session.get_messages() 
     63[] 
     64>>> file_session.create_message('first post') 
     65>>> file_session.get_messages() 
     66['first post'] 
     67>>> file_session.get_and_delete_messages() 
     68['first post'] 
     69>>> file_session.get_and_delete_messages() 
     70[] 
     71>>> file_session.create_message('hello') 
     72>>> file_session.create_message('world') 
     73>>> file_session.get_and_delete_messages() 
     74['hello', 'world'] 
    4975>>> file_session.save() 
    5076>>> file_session.exists(file_session.session_key) 
    5177True 
     
    83109'dog' 
    84110>>> cache_session.pop('some key', 'does not exist') 
    85111'does not exist' 
     112>>> cache_session.get_messages() 
     113[] 
     114>>> cache_session.create_message('first post') 
     115>>> cache_session.get_messages() 
     116['first post'] 
     117>>> cache_session.get_and_delete_messages() 
     118['first post'] 
     119>>> cache_session.get_and_delete_messages() 
     120[] 
     121>>> cache_session.create_message('hello') 
     122>>> cache_session.create_message('world') 
     123>>> cache_session.get_and_delete_messages() 
     124['hello', 'world'] 
    86125>>> cache_session.save() 
    87126>>> cache_session.delete(cache_session.session_key) 
    88127>>> cache_session.exists(cache_session.session_key) 
  • django/contrib/sessions/backends/base.py

    old new  
    3333    """ 
    3434    TEST_COOKIE_NAME = 'testcookie' 
    3535    TEST_COOKIE_VALUE = 'worked' 
     36    MESSAGES_NAME = '_messages' 
    3637 
    3738    def __init__(self, session_key=None): 
    3839        self._session_key = session_key 
     
    8384    def delete_test_cookie(self): 
    8485        del self[self.TEST_COOKIE_NAME] 
    8586 
     87    def get_messages(self): 
     88            return self.get(self.MESSAGES_NAME, []) 
     89 
     90    def get_and_delete_messages(self): 
     91            return self.pop(self.MESSAGES_NAME, []) 
     92 
     93    def create_message(self, message): 
     94        messages = self.get(self.MESSAGES_NAME) 
     95        if messages is None: 
     96            messages = [] 
     97            self[self.MESSAGES_NAME] = messages 
     98        messages.append(message) 
     99        self.modified = True 
     100 
    86101    def encode(self, session_dict): 
    87102        "Returns the given session dictionary pickled and encoded as a string." 
    88103        pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) 
  • docs/authentication.txt

    old new  
    975975Messages 
    976976======== 
    977977 
    978 The message system is a lightweight way to queue messages for given users. 
     978The user message system is a lightweight way to queue messages for given users. 
     979To send messages to anonymous users, use `session messages`_. 
    979980 
     981.. _session framework: ../sessions/#messages 
     982 
    980983A message is associated with a ``User``. There's no concept of expiration or 
    981984timestamps. 
    982985 
     
    10021005            context_instance=RequestContext(request)) 
    10031006 
    10041007When you use ``RequestContext``, the currently logged-in user and his/her 
    1005 messages are made available in the `template context`_ as the template variable 
    1006 ``{{ messages }}``. Here's an example of template code that displays messages:: 
     1008messages are made available in the `template context`_ as the ``{{ messages }}`` 
     1009template variable. 
    10071010 
    1008     {% if messages %} 
    1009     <ul> 
    1010         {% for message in messages %} 
    1011         <li>{{ message }}</li> 
    1012         {% endfor %} 
    1013     </ul> 
    1014     {% endif %} 
     1011**New in Django development version** 
    10151012 
    1016 Note that ``RequestContext`` calls ``get_and_delete_messages`` behind the 
    1017 scenes, so any messages will be deleted even if you don't display them
     1013The ``{{ messages }}`` template variable will also contain session messages. 
     1014For more information, see `django.core.context_processors.messages`_
    10181015 
    1019 Finally, note that this messages framework only works with users in the user 
    1020 database. To send messages to anonymous users, use the `session framework`_. 
     1016.. _django.core.context_processors.messages: ../templates_python/#django-core-context_processors-messages 
    10211017 
    1022 .. _session framework: ../sessions/ 
     1018Also note that previously, ``RequestContext`` directly called 
     1019``get_and_delete_messages`` behind the scenes, so any messages were deleted even 
     1020if not displayed. Messages are now only deleted if the ``{{ messages }}`` 
     1021variable is accessed in a template. 
    10231022 
    10241023Other authentication sources 
    10251024============================ 
  • docs/sessions.txt

    old new  
    262262        request.session.set_test_cookie() 
    263263        return render_to_response('foo/login_form.html') 
    264264 
     265Messages 
     266======== 
     267 
     268**New in Django development version** 
     269 
     270The session message system provides a simple way to queue messages for all 
     271(anonymous or authenticated) site visitors. To associate messages with users in 
     272the user database, use the `authentication message framework`_. 
     273 
     274.. _authentication message framework: ../authentication/#messages 
     275 
     276Messages are associated with a session, therefore a message only lasts as long 
     277as a session is valid (see `browser-length sessions vs. persistent sessions`_). 
     278 
     279The message system relies on the session middleware and is accessed via 
     280``request.session``. The API is simple: 
     281 
     282    * To create a new message, use 
     283      ``request.session.create_message(message='message text').`` 
     284 
     285    * To retreive the messages, use ``request.session.get_messages()``, 
     286      which returns a list of any messages (strings) in the session's queue. 
     287 
     288    * To retrieve and delete messages, use 
     289      ``request.session.get_and_delete_messages()``, which returns the list of 
     290      any messages in the session's queue and then deletes the messages from the 
     291      queue. 
     292 
     293The `django.core.context_processors.messages`_ context processor makes both 
     294session messages and user messages available to templates. 
     295 
     296.. _django.core.context_processors.messages: ../templates_python/#django-core-context_processors-messages 
     297 
    265298Using sessions out of views 
    266299=========================== 
    267300 
  • docs/templates_python.txt

    old new  
    346346      logged in). See the `user authentication docs`_. 
    347347 
    348348    * ``messages`` -- A list of messages (as strings) for the currently 
    349       logged-in user. Behind the scenes, this calls 
    350       ``request.user.get_and_delete_messages()`` for every request. That method 
    351       collects the user's messages and deletes them from the database. 
     349      logged-in user. 
    352350 
    353       Note that messages are set with ``user.message_set.create``. See the 
    354       `message docs`_ for more. 
     351      **New in Django development version** 
    355352 
     353      This ``messages`` list now also contains session messages. 
     354 
     355      The messages are not retrieved and cleared (using 
     356      ``get_and_delete_messages``) until the ``messages`` variable is accessed 
     357      in a template whereas previously, this context processor called 
     358      ``request.user.get_and_delete_messages()`` behind the scenes for every 
     359      request. 
     360 
     361      See the `authentication message docs`_ or `session message docs`_ for 
     362      information on creating messages. 
     363 
    356364    * ``perms`` -- An instance of 
    357365      ``django.core.context_processors.PermWrapper``, representing the 
    358366      permissions that the currently logged-in user has. See the `permissions 
    359367      docs`_. 
    360368 
    361369.. _user authentication docs: ../authentication/#users 
    362 .. _message docs: ../authentication/#messages 
     370.. _authentication message docs: ../authentication/#messages 
     371.. _session message docs: ../sessions/#messages 
    363372.. _permissions docs: ../authentication/#permissions 
    364373 
    365374django.core.context_processors.debug 
     
    411420`HttpRequest object`_. Note that this processor is not enabled by default; 
    412421you'll have to activate it. 
    413422 
     423django.core.context_processors.messages 
     424~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     425 
     426If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every 
     427``RequestContext`` will contain a variable ``messages``, which is a list of 
     428messages (as strings) for the current session and the currently logged-in user. 
     429See the `session messages docs`_ or the `authentication messages docs`_ for more 
     430information on using messages. 
     431 
     432Note that this processor is only useful if you are not using the (enabled by 
     433default) ``auth`` processor, as it also provides the ``messages`` variable. 
     434 
     435The messages are not retrieved and cleared (using ``get_and_delete_messages``) 
     436until the ``messages`` variable is accessed in a template. 
     437 
     438Here's an example of template code that displays messages made available by this 
     439context processor:: 
     440 
     441        {% if messages %} 
     442        <ul> 
     443            {% for message in messages %} 
     444            <li>{{ message }}</li> 
     445            {% endfor %} 
     446        </ul> 
     447        {% endif %} 
     448 
    414449Writing your own context processors 
    415450~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    416451