Ticket #4604: visitor_messages_r6373.patch
File visitor_messages_r6373.patch, 6.6 KB (added by , 17 years ago) |
---|
-
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 } -
django/contrib/sessions/tests.py
14 14 'dog' 15 15 >>> db_session.pop('some key', 'does not exist') 16 16 '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'] 17 30 >>> db_session.save() 18 31 >>> db_session.exists(db_session.session_key) 19 32 True … … 31 44 'dog' 32 45 >>> file_session.pop('some key', 'does not exist') 33 46 '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'] 34 60 >>> file_session.save() 35 61 >>> file_session.exists(file_session.session_key) 36 62 True … … 48 74 'dog' 49 75 >>> cache_session.pop('some key', 'does not exist') 50 76 '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'] 51 90 >>> cache_session.save() 52 91 >>> cache_session.delete(cache_session.session_key) 53 92 >>> cache_session.exists(cache_session.session_key) -
django/contrib/sessions/backends/base.py
18 18 """ 19 19 20 20 TEST_COOKIE_NAME = 'testcookie' 21 TEST_COOKIE_VALUE = 'worked' 21 TEST_COOKIE_VALUE = 'worked' 22 MESSAGES_NAME = '_messages' 22 23 23 24 def __init__(self, session_key=None): 24 25 self._session_key = session_key … … 59 60 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 63 78 def encode(self, session_dict): 64 79 "Returns the given session dictionary pickled and encoded as a string." 65 80 pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) -
docs/sessions.txt
190 190 return HttpResponse("Please enable cookies and try again.") 191 191 request.session.set_test_cookie() 192 192 return render_to_response('foo/login_form.html') 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. 193 244 194 245 Using sessions out of views 195 246 =========================== -
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 ============================