Ticket #4604: visitor_messages.patch

File visitor_messages.patch, 4.9 KB (added by Chris Beaven, 17 years ago)
  • 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    }
  • django/contrib/sessions/middleware.py

     
    66
    77TEST_COOKIE_NAME = 'testcookie'
    88TEST_COOKIE_VALUE = 'worked'
     9MESSAGES_NAME = '_messages'
    910
    1011class SessionWrapper(object):
    1112    def __init__(self, session_key):
     
    4849    def delete_test_cookie(self):
    4950        del self[TEST_COOKIE_NAME]
    5051
     52    def get_messages(self):
     53        return self.get(MESSAGES_NAME, [])
     54
     55    def get_and_delete_messages(self):
     56        return self.pop(MESSAGES_NAME, [])
     57
     58    def create_message(self, message):
     59        messages = self.get(MESSAGES_NAME)
     60        if messages is None:
     61            messages = []
     62            self[MESSAGES_NAME] = messages
     63        messages.append(message)
     64        self.modified = True
     65
    5166    def _get_session(self):
    5267        # Lazily loads session from storage.
    5368        self.accessed = True
  • django/contrib/sessions/tests.py

     
    1010
    1111>>> s.pop('some key', 'does not exist')
    1212'does not exist'
     13
     14############
     15# Messages #
     16############
     17
     18>>> s.get_messages()
     19[]
     20
     21>>> s.create_message('first post')
     22>>> s.get_messages()
     23['first post']
     24>>> s.get_and_delete_messages()
     25['first post']
     26>>> s.get_and_delete_messages()
     27[]
     28
     29>>> s.create_message('hello')
     30>>> s.create_message('world')
     31>>> s.get_and_delete_messages()
     32['hello', 'world']
    1333"""
    1434
    1535from django.contrib.sessions.middleware import SessionWrapper
  • docs/authentication.txt

     
    911911Finally, note that this messages framework only works with users in the user
    912912database. To send messages to anonymous users, use the `session framework`_.
    913913
    914 .. _session framework: ../sessions/
     914.. _session framework: ../sessions/#visitor-messages
    915915
    916916Other authentication sources
    917917============================
  • docs/sessions.txt

     
    150150        request.session.set_test_cookie()
    151151        return render_to_response('foo/login_form.html')
    152152
     153Visitor Messages
     154================
     155
     156**New in Django development version**
     157
     158The session message system provides a simple way to queue messages for
     159anonymous site visitors. To associate messages with users in the user database,
     160use the `authentication message framework`_.
     161
     162.. _authentication message framework: ../authentication/#messages
     163
     164Messages are associated with a session, therefore a message only lasts as long
     165as a session is valid (see `browser-length sessions vs. persistent sessions`_).
     166
     167The message system relies on the session middleware and is accessed via
     168``request.session``. The API is simple:
     169
     170    * To create a new message, use
     171      ``request.session.create_message(message='message text').``
     172
     173    * To retreive the messages, use ``request.session.get_messages()``,
     174      which returns a list of any messages (strings) in the session's queue.
     175
     176    * To retrieve and delete messages, use
     177      ``user_obj.get_and_delete_messages()``, which returns the list of any
     178      messages in the session's queue and then deletes the messages from the
     179      queue.
     180
     181django.contrib.sessions.context_processors.visitor_messages
     182-----------------------------------------------------------
     183
     184This `context processor`_ is provided (but not installed by default) which makes
     185these messages available to the template context as the template variable
     186``{{ visitor_messages }}`` when you use ``RequestContext``.
     187
     188.. _context processor: ../templates_python/#subclassing-context-requestcontext
     189
     190Here's an example of template code that displays messages::
     191
     192        {% if visitor_messages %}
     193        <ul>
     194            {% for message in visitor_messages %}
     195            <li>{{ message|escape }}</li>
     196            {% endfor %}
     197        </ul>
     198        {% endif %}
     199
     200Note that when this context processor is accessed by ``RequestContext``,
     201``get_and_delete_messages`` is called behind the scenes, so any messages will
     202be deleted even if you don't display them.
     203
    153204Using sessions out of views
    154205===========================
    155206
Back to Top