Ticket #4604: visitor_messages_r6596.patch
File visitor_messages_r6596.patch, 6.3 KB (added by , 17 years ago) |
---|
-
django/contrib/sessions/tests.py
15 15 'dog' 16 16 >>> db_session.pop('some key', 'does not exist') 17 17 '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'] 18 31 >>> db_session.save() 19 32 >>> db_session.exists(db_session.session_key) 20 33 True … … 32 45 'dog' 33 46 >>> file_session.pop('some key', 'does not exist') 34 47 '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'] 35 61 >>> file_session.save() 36 62 >>> file_session.exists(file_session.session_key) 37 63 True … … 49 75 'dog' 50 76 >>> cache_session.pop('some key', 'does not exist') 51 77 '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'] 52 91 >>> cache_session.save() 53 92 >>> cache_session.delete(cache_session.session_key) 54 93 >>> cache_session.exists(cache_session.session_key) -
django/contrib/sessions/backends/base.py
18 18 """ 19 19 TEST_COOKIE_NAME = 'testcookie' 20 20 TEST_COOKIE_VALUE = 'worked' 21 MESSAGES_NAME = '_messages' 21 22 22 23 def __init__(self, session_key=None): 23 24 self._session_key = session_key … … 60 61 def delete_test_cookie(self): 61 62 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 78 63 79 def encode(self, session_dict): 64 80 "Returns the given session dictionary pickled and encoded as a string." 65 81 pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) -
django/contrib/sessions/context_processors.py
1 def 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
191 191 request.session.set_test_cookie() 192 192 return render_to_response('foo/login_form.html') 193 193 194 Visitor Messages 195 ================ 196 197 **New in Django development version** 198 199 The session message system provides a simple way to queue messages for 200 all (anonymous or authenticated) site visitors. To associate messages with users 201 in the user database, use the `authentication message framework`_. 202 203 .. _authentication message framework: ../authentication/#messages 204 205 Messages are associated with a session, therefore a message only lasts as long 206 as a session is valid (see `browser-length sessions vs. persistent sessions`_). 207 208 The 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 222 django.contrib.sessions.context_processors.visitor_messages 223 ----------------------------------------------------------- 224 225 This `context processor`_ is provided (but not installed by default) which makes 226 these 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 231 Here'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 241 Note that when this context processor is accessed by ``RequestContext``, 242 ``get_and_delete_messages`` is called behind the scenes, so any messages will 243 be deleted even if you don't display them. 244 194 245 Using sessions out of views 195 246 =========================== 196 247 -
docs/authentication.txt
947 947 Finally, note that this messages framework only works with users in the user 948 948 database. To send messages to anonymous users, use the `session framework`_. 949 949 950 .. _session framework: ../sessions/ 950 .. _session framework: ../sessions/#visitor-messages 951 951 952 952 Other authentication sources 953 953 ============================