Ticket #4604: messages-r8347.diff
File messages-r8347.diff, 12.7 KB (added by , 16 years ago) |
---|
-
django/core/context_processors.py
8 8 """ 9 9 10 10 from django.conf import settings 11 from django.utils.encoding import StrAndUnicode 11 12 13 # LazyMessages is used by the `messages` and `auth` context processors. 14 class LazyMessages(StrAndUnicode): 15 """ 16 A lazy proxy for session and authentication messages. 17 """ 18 def __init__(self, request): 19 self.request = request 20 21 def __iter__(self): 22 return iter(self.messages) 23 24 def __len__(self): 25 return len(self.messages) 26 27 def __nonzero__(self): 28 return bool(self.messages) 29 30 def __unicode__(self): 31 return unicode(self.messages) 32 33 def __getitem__(self, *args, **kwargs): 34 return self.messages.__getitem__(*args, **kwargs) 35 36 def _get_messages(self): 37 if hasattr(self, '_messages'): 38 return self._messages 39 # First, retreive any messages for the user. 40 if hasattr(self.request, 'user') and \ 41 hasattr(self.request.user, 'get_and_delete_messages'): 42 self._messages = self.request.user.get_and_delete_messages() 43 else: 44 self._messages = [] 45 # Next, retrieve any messages for the session. 46 if hasattr(self.request, 'session'): 47 self._messages += self.request.session.get_and_delete_messages() 48 return self._messages 49 messages = property(_get_messages) 50 12 51 def auth(request): 13 52 """ 14 53 Returns context variables required by apps that use Django's authentication … … 22 61 else: 23 62 from django.contrib.auth.models import AnonymousUser 24 63 user = AnonymousUser() 25 return{64 context_extras = { 26 65 'user': user, 27 'messages': user.get_and_delete_messages(),28 66 'perms': PermWrapper(user), 29 67 } 68 # Add authentication (and session) LazyMessages to the context too. 69 context_extras.update(messages(request)) 70 return context_extras 30 71 72 def messages(request): 73 """ 74 Returns messages for the session and the current user. 75 76 Note that this processor is only useful to use explicity if you are not 77 using the (enabled by default) auth processor, as it also provides the 78 messages (by calling this method). 79 80 The messages are lazy loaded, so no messages are retreived and deleted 81 unless requested from the template. 82 83 Both contrib.session and contrib.auth are optional. If neither is provided, 84 no 'messages' variable will be added to the context. 85 """ 86 if hasattr(request, 'session') or hasattr(request, 'user'): 87 return {'messages': LazyMessages(request)} 88 return {} 89 31 90 def debug(request): 32 91 "Returns context variables helpful for debugging." 33 92 context_extras = {} … … 83 142 def __iter__(self): 84 143 # I am large, I contain multitudes. 85 144 raise TypeError("PermWrapper is not iterable.") 145 -
django/contrib/sessions/tests.py
16 16 'dog' 17 17 >>> db_session.pop('some key', 'does not exist') 18 18 'does not exist' 19 >>> db_session.get_messages() 20 [] 21 >>> db_session.create_message('first post') 22 >>> db_session.get_messages() 23 ['first post'] 24 >>> db_session.get_and_delete_messages() 25 ['first post'] 26 >>> db_session.get_and_delete_messages() 27 [] 28 >>> db_session.create_message('hello') 29 >>> db_session.create_message('world') 30 >>> db_session.get_and_delete_messages() 31 ['hello', 'world'] 19 32 >>> db_session.save() 20 33 >>> db_session.exists(db_session.session_key) 21 34 True … … 46 59 'dog' 47 60 >>> file_session.pop('some key', 'does not exist') 48 61 'does not exist' 62 >>> file_session.get_messages() 63 [] 64 >>> file_session.create_message('first post') 65 >>> file_session.get_messages() 66 ['first post'] 67 >>> file_session.get_and_delete_messages() 68 ['first post'] 69 >>> file_session.get_and_delete_messages() 70 [] 71 >>> file_session.create_message('hello') 72 >>> file_session.create_message('world') 73 >>> file_session.get_and_delete_messages() 74 ['hello', 'world'] 49 75 >>> file_session.save() 50 76 >>> file_session.exists(file_session.session_key) 51 77 True … … 83 109 'dog' 84 110 >>> cache_session.pop('some key', 'does not exist') 85 111 'does not exist' 112 >>> cache_session.get_messages() 113 [] 114 >>> cache_session.create_message('first post') 115 >>> cache_session.get_messages() 116 ['first post'] 117 >>> cache_session.get_and_delete_messages() 118 ['first post'] 119 >>> cache_session.get_and_delete_messages() 120 [] 121 >>> cache_session.create_message('hello') 122 >>> cache_session.create_message('world') 123 >>> cache_session.get_and_delete_messages() 124 ['hello', 'world'] 86 125 >>> cache_session.save() 87 126 >>> cache_session.delete(cache_session.session_key) 88 127 >>> cache_session.exists(cache_session.session_key) -
django/contrib/sessions/backends/base.py
33 33 """ 34 34 TEST_COOKIE_NAME = 'testcookie' 35 35 TEST_COOKIE_VALUE = 'worked' 36 MESSAGES_NAME = '_messages' 36 37 37 38 def __init__(self, session_key=None): 38 39 self._session_key = session_key … … 83 84 def delete_test_cookie(self): 84 85 del self[self.TEST_COOKIE_NAME] 85 86 87 def get_messages(self): 88 return self.get(self.MESSAGES_NAME, []) 89 90 def get_and_delete_messages(self): 91 return self.pop(self.MESSAGES_NAME, []) 92 93 def create_message(self, message): 94 messages = self.get(self.MESSAGES_NAME) 95 if messages is None: 96 messages = [] 97 self[self.MESSAGES_NAME] = messages 98 messages.append(message) 99 self.modified = True 100 86 101 def encode(self, session_dict): 87 102 "Returns the given session dictionary pickled and encoded as a string." 88 103 pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) -
docs/authentication.txt
975 975 Messages 976 976 ======== 977 977 978 The message system is a lightweight way to queue messages for given users. 978 The user message system is a lightweight way to queue messages for given users. 979 To send messages to anonymous users, use `session messages`_. 979 980 981 .. _session framework: ../sessions/#messages 982 980 983 A message is associated with a ``User``. There's no concept of expiration or 981 984 timestamps. 982 985 … … 1002 1005 context_instance=RequestContext(request)) 1003 1006 1004 1007 When you use ``RequestContext``, the currently logged-in user and his/her 1005 messages are made available in the `template context`_ as the template variable1006 ``{{ messages }}``. Here's an example of template code that displays messages:: 1008 messages are made available in the `template context`_ as the ``{{ messages }}`` 1009 template variable. 1007 1010 1008 {% if messages %} 1009 <ul> 1010 {% for message in messages %} 1011 <li>{{ message }}</li> 1012 {% endfor %} 1013 </ul> 1014 {% endif %} 1011 **New in Django development version** 1015 1012 1016 Note that ``RequestContext`` calls ``get_and_delete_messages`` behind the 1017 scenes, so any messages will be deleted even if you don't display them.1013 The ``{{ messages }}`` template variable will also contain session messages. 1014 For more information, see `django.core.context_processors.messages`_. 1018 1015 1019 Finally, note that this messages framework only works with users in the user 1020 database. To send messages to anonymous users, use the `session framework`_. 1016 .. _django.core.context_processors.messages: ../templates_python/#django-core-context_processors-messages 1021 1017 1022 .. _session framework: ../sessions/ 1018 Also note that previously, ``RequestContext`` directly called 1019 ``get_and_delete_messages`` behind the scenes, so any messages were deleted even 1020 if not displayed. Messages are now only deleted if the ``{{ messages }}`` 1021 variable is accessed in a template. 1023 1022 1024 1023 Other authentication sources 1025 1024 ============================ -
docs/sessions.txt
262 262 request.session.set_test_cookie() 263 263 return render_to_response('foo/login_form.html') 264 264 265 Messages 266 ======== 267 268 **New in Django development version** 269 270 The session message system provides a simple way to queue messages for all 271 (anonymous or authenticated) site visitors. To associate messages with users in 272 the user database, use the `authentication message framework`_. 273 274 .. _authentication message framework: ../authentication/#messages 275 276 Messages are associated with a session, therefore a message only lasts as long 277 as a session is valid (see `browser-length sessions vs. persistent sessions`_). 278 279 The message system relies on the session middleware and is accessed via 280 ``request.session``. The API is simple: 281 282 * To create a new message, use 283 ``request.session.create_message(message='message text').`` 284 285 * To retreive the messages, use ``request.session.get_messages()``, 286 which returns a list of any messages (strings) in the session's queue. 287 288 * To retrieve and delete messages, use 289 ``request.session.get_and_delete_messages()``, which returns the list of 290 any messages in the session's queue and then deletes the messages from the 291 queue. 292 293 The `django.core.context_processors.messages`_ context processor makes both 294 session messages and user messages available to templates. 295 296 .. _django.core.context_processors.messages: ../templates_python/#django-core-context_processors-messages 297 265 298 Using sessions out of views 266 299 =========================== 267 300 -
docs/templates_python.txt
346 346 logged in). See the `user authentication docs`_. 347 347 348 348 * ``messages`` -- A list of messages (as strings) for the currently 349 logged-in user. Behind the scenes, this calls 350 ``request.user.get_and_delete_messages()`` for every request. That method 351 collects the user's messages and deletes them from the database. 349 logged-in user. 352 350 353 Note that messages are set with ``user.message_set.create``. See the 354 `message docs`_ for more. 351 **New in Django development version** 355 352 353 This ``messages`` list now also contains session messages. 354 355 The messages are not retrieved and cleared (using 356 ``get_and_delete_messages``) until the ``messages`` variable is accessed 357 in a template whereas previously, this context processor called 358 ``request.user.get_and_delete_messages()`` behind the scenes for every 359 request. 360 361 See the `authentication message docs`_ or `session message docs`_ for 362 information on creating messages. 363 356 364 * ``perms`` -- An instance of 357 365 ``django.core.context_processors.PermWrapper``, representing the 358 366 permissions that the currently logged-in user has. See the `permissions 359 367 docs`_. 360 368 361 369 .. _user authentication docs: ../authentication/#users 362 .. _message docs: ../authentication/#messages 370 .. _authentication message docs: ../authentication/#messages 371 .. _session message docs: ../sessions/#messages 363 372 .. _permissions docs: ../authentication/#permissions 364 373 365 374 django.core.context_processors.debug … … 411 420 `HttpRequest object`_. Note that this processor is not enabled by default; 412 421 you'll have to activate it. 413 422 423 django.core.context_processors.messages 424 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 425 426 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every 427 ``RequestContext`` will contain a variable ``messages``, which is a list of 428 messages (as strings) for the current session and the currently logged-in user. 429 See the `session messages docs`_ or the `authentication messages docs`_ for more 430 information on using messages. 431 432 Note that this processor is only useful if you are not using the (enabled by 433 default) ``auth`` processor, as it also provides the ``messages`` variable. 434 435 The messages are not retrieved and cleared (using ``get_and_delete_messages``) 436 until the ``messages`` variable is accessed in a template. 437 438 Here's an example of template code that displays messages made available by this 439 context processor:: 440 441 {% if messages %} 442 <ul> 443 {% for message in messages %} 444 <li>{{ message }}</li> 445 {% endfor %} 446 </ul> 447 {% endif %} 448 414 449 Writing your own context processors 415 450 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 416 451