Ticket #4604: visitor_messages_r6373.patch

File visitor_messages_r6373.patch, 6.6 KB (added by Pedro Lima <pedro.lima@…>, 17 years ago)

Updated patch to new session implementation and included the suggested documentation change

  • 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/tests.py

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

     
    1818    """
    1919
    2020    TEST_COOKIE_NAME = 'testcookie'
    21     TEST_COOKIE_VALUE = 'worked'
     21    TEST_COOKIE_VALUE = 'worked'
     22    MESSAGES_NAME = '_messages'
    2223
    2324    def __init__(self, session_key=None):
    2425        self._session_key = session_key
     
    5960
    6061    def delete_test_cookie(self):
    6162        del self[self.TEST_COOKIE_NAME]
    62        
     63
     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
    6378    def encode(self, session_dict):
    6479        "Returns the given session dictionary pickled and encoded as a string."
    6580        pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
  • docs/sessions.txt

     
    190190                return HttpResponse("Please enable cookies and try again.")
    191191        request.session.set_test_cookie()
    192192        return render_to_response('foo/login_form.html')
     193
     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.
    193244
    194245Using sessions out of views
    195246===========================
  • 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============================
Back to Top