Code

Ticket #4604: visitor_messages_r6596.patch

File visitor_messages_r6596.patch, 6.3 KB (added by msaelices, 6 years ago)

Updated patch for revision 6596

  • django/contrib/sessions/tests.py

     
    1515'dog' 
    1616>>> db_session.pop('some key', 'does not exist') 
    1717'does not exist' 
     18>>> db_session.get_messages() 
     19[] 
     20>>> db_session.create_message('first post') 
     21>>> db_session.get_messages() 
     22['first post'] 
     23>>> db_session.get_and_delete_messages() 
     24['first post'] 
     25>>> db_session.get_and_delete_messages() 
     26[] 
     27>>> db_session.create_message('hello') 
     28>>> db_session.create_message('world') 
     29>>> db_session.get_and_delete_messages() 
     30['hello', 'world'] 
    1831>>> db_session.save() 
    1932>>> db_session.exists(db_session.session_key) 
    2033True 
     
    3245'dog' 
    3346>>> file_session.pop('some key', 'does not exist') 
    3447'does not exist' 
     48>>> file_session.get_messages() 
     49[] 
     50>>> file_session.create_message('first post') 
     51>>> file_session.get_messages() 
     52['first post'] 
     53>>> file_session.get_and_delete_messages() 
     54['first post'] 
     55>>> file_session.get_and_delete_messages() 
     56[] 
     57>>> file_session.create_message('hello') 
     58>>> file_session.create_message('world') 
     59>>> file_session.get_and_delete_messages() 
     60['hello', 'world'] 
    3561>>> file_session.save() 
    3662>>> file_session.exists(file_session.session_key) 
    3763True 
     
    4975'dog' 
    5076>>> cache_session.pop('some key', 'does not exist') 
    5177'does not exist' 
     78>>> cache_session.get_messages() 
     79[] 
     80>>> cache_session.create_message('first post') 
     81>>> cache_session.get_messages() 
     82['first post'] 
     83>>> cache_session.get_and_delete_messages() 
     84['first post'] 
     85>>> cache_session.get_and_delete_messages() 
     86[] 
     87>>> cache_session.create_message('hello') 
     88>>> cache_session.create_message('world') 
     89>>> cache_session.get_and_delete_messages() 
     90['hello', 'world'] 
    5291>>> cache_session.save() 
    5392>>> cache_session.delete(cache_session.session_key) 
    5493>>> cache_session.exists(cache_session.session_key) 
  • django/contrib/sessions/backends/base.py

     
    1818    """ 
    1919    TEST_COOKIE_NAME = 'testcookie' 
    2020    TEST_COOKIE_VALUE = 'worked' 
     21    MESSAGES_NAME = '_messages' 
    2122 
    2223    def __init__(self, session_key=None): 
    2324        self._session_key = session_key 
     
    6061    def delete_test_cookie(self): 
    6162        del self[self.TEST_COOKIE_NAME] 
    6263 
     64    def get_messages(self): 
     65            return self.get(self.MESSAGES_NAME, []) 
     66     
     67    def get_and_delete_messages(self): 
     68            return self.pop(self.MESSAGES_NAME, []) 
     69 
     70    def create_message(self, message): 
     71        messages = self.get(self.MESSAGES_NAME) 
     72        if messages is None: 
     73            messages = [] 
     74            self[self.MESSAGES_NAME] = messages 
     75        messages.append(message) 
     76        self.modified = True 
     77                 
     78         
    6379    def encode(self, session_dict): 
    6480        "Returns the given session dictionary pickled and encoded as a string." 
    6581        pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) 
  • django/contrib/sessions/context_processors.py

     
     1def visitor_messages(request): 
     2    """ 
     3    Returns any messages in the session's message queue. 
     4    """ 
     5    return { 
     6        'visitor_messages': request.session.get_and_delete_messages(), 
     7    } 
  • docs/sessions.txt

     
    191191        request.session.set_test_cookie() 
    192192        return render_to_response('foo/login_form.html') 
    193193 
     194Visitor Messages 
     195================ 
     196 
     197**New in Django development version** 
     198 
     199The session message system provides a simple way to queue messages for 
     200all (anonymous or authenticated) site visitors. To associate messages with users  
     201in the user database, use the `authentication message framework`_. 
     202 
     203.. _authentication message framework: ../authentication/#messages 
     204 
     205Messages are associated with a session, therefore a message only lasts as long 
     206as a session is valid (see `browser-length sessions vs. persistent sessions`_). 
     207 
     208The message system relies on the session middleware and is accessed via 
     209``request.session``. The API is simple: 
     210 
     211    * To create a new message, use 
     212      ``request.session.create_message(message='message text').`` 
     213 
     214    * To retreive the messages, use ``request.session.get_messages()``, 
     215      which returns a list of any messages (strings) in the session's queue. 
     216 
     217    * To retrieve and delete messages, use 
     218      ``user_obj.get_and_delete_messages()``, which returns the list of any 
     219      messages in the session's queue and then deletes the messages from the 
     220      queue. 
     221 
     222django.contrib.sessions.context_processors.visitor_messages 
     223----------------------------------------------------------- 
     224 
     225This `context processor`_ is provided (but not installed by default) which makes 
     226these messages available to the template context as the template variable 
     227``{{ visitor_messages }}`` when you use ``RequestContext``. 
     228 
     229.. _context processor: ../templates_python/#subclassing-context-requestcontext 
     230 
     231Here's an example of template code that displays messages:: 
     232 
     233        {% if visitor_messages %} 
     234        <ul> 
     235            {% for message in visitor_messages %} 
     236            <li>{{ message|escape }}</li> 
     237            {% endfor %} 
     238        </ul> 
     239        {% endif %} 
     240 
     241Note that when this context processor is accessed by ``RequestContext``,  
     242``get_and_delete_messages`` is called behind the scenes, so any messages will 
     243be deleted even if you don't display them. 
     244 
    194245Using sessions out of views 
    195246=========================== 
    196247 
  • docs/authentication.txt

     
    947947Finally, note that this messages framework only works with users in the user 
    948948database. To send messages to anonymous users, use the `session framework`_. 
    949949 
    950 .. _session framework: ../sessions/ 
     950.. _session framework: ../sessions/#visitor-messages 
    951951 
    952952Other authentication sources 
    953953============================