Ticket #4604: messages.diff
File messages.diff, 11.9 KB (added by , 17 years ago) |
---|
-
django/conf/global_settings.py
154 154 'django.core.context_processors.debug', 155 155 'django.core.context_processors.i18n', 156 156 'django.core.context_processors.media', 157 'django.core.context_processors.messages', 157 158 # 'django.core.context_processors.request', 158 159 ) 159 160 -
django/core/context_processors.py
8 8 """ 9 9 10 10 from django.conf import settings 11 from django.utils.functional import memoize, lazy 11 12 12 13 def auth(request): 13 14 """ … … 24 25 user = AnonymousUser() 25 26 return { 26 27 'user': user, 27 'messages': user.get_and_delete_messages(),28 28 'perms': PermWrapper(user), 29 29 } 30 30 31 class LazyMessages(object): 32 def __init__(self, request): 33 self.request = request 34 def __iter__(self): 35 return self.messages.__iter__() 36 def __len__(self): 37 return len(self.messages) 38 def __nonzero__(self): 39 return bool(self.messages) 40 def __unicode__(self): 41 return unicode(self.messages) 42 def _get_messages(self): 43 if hasattr(self, '_messages'): 44 return self._messages 45 # First, retreive any messages for the user. 46 if hasattr(self.request, 'user') and \ 47 hasattr(self.request.user, 'get_and_delete_messages'): 48 self._messages = self.request.user.get_and_delete_messages() 49 else: 50 self._messages = [] 51 # Next, retrieve any messages for the session. 52 self._messages += self.request.session.get_and_delete_messages() 53 return self._messages 54 messages = property(_get_messages) 55 56 def messages(request): 57 """ 58 Return messages for the session and the current user. 59 60 The messages are lazy loaded, so no messages are retreived and deleted 61 unless requested from the template. 62 """ 63 return { 64 'messages': LazyMessages(request) 65 } 66 31 67 def debug(request): 32 68 "Returns context variables helpful for debugging." 33 69 context_extras = {} -
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 … … 33 46 'dog' 34 47 >>> file_session.pop('some key', 'does not exist') 35 48 'does not exist' 49 >>> file_session.get_messages() 50 [] 51 >>> file_session.create_message('first post') 52 >>> file_session.get_messages() 53 ['first post'] 54 >>> file_session.get_and_delete_messages() 55 ['first post'] 56 >>> file_session.get_and_delete_messages() 57 [] 58 >>> file_session.create_message('hello') 59 >>> file_session.create_message('world') 60 >>> file_session.get_and_delete_messages() 61 ['hello', 'world'] 36 62 >>> file_session.save() 37 63 >>> file_session.exists(file_session.session_key) 38 64 True … … 57 83 'dog' 58 84 >>> cache_session.pop('some key', 'does not exist') 59 85 'does not exist' 86 >>> cache_session.get_messages() 87 [] 88 >>> cache_session.create_message('first post') 89 >>> cache_session.get_messages() 90 ['first post'] 91 >>> cache_session.get_and_delete_messages() 92 ['first post'] 93 >>> cache_session.get_and_delete_messages() 94 [] 95 >>> cache_session.create_message('hello') 96 >>> cache_session.create_message('world') 97 >>> cache_session.get_and_delete_messages() 98 ['hello', 'world'] 60 99 >>> cache_session.save() 61 100 >>> cache_session.delete(cache_session.session_key) 62 101 >>> 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 … … 68 69 def delete_test_cookie(self): 69 70 del self[self.TEST_COOKIE_NAME] 70 71 72 def get_messages(self): 73 return self.get(self.MESSAGES_NAME, []) 74 75 def get_and_delete_messages(self): 76 return self.pop(self.MESSAGES_NAME, []) 77 78 def create_message(self, message): 79 messages = self.get(self.MESSAGES_NAME) 80 if messages is None: 81 messages = [] 82 self[self.MESSAGES_NAME] = messages 83 messages.append(message) 84 self.modified = True 85 71 86 def encode(self, session_dict): 72 87 "Returns the given session dictionary pickled and encoded as a string." 73 88 pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) -
docs/sessions.txt
193 193 request.session.set_test_cookie() 194 194 return render_to_response('foo/login_form.html') 195 195 196 Messages 197 ======== 198 199 **New in Django development version** 200 201 The session message system provides a simple way to queue messages for all 202 (anonymous or authenticated) site visitors. To associate messages with users in 203 the user database, use the `authentication message framework`_. 204 205 .. _authentication message framework: ../authentication/#messages 206 207 Messages are associated with a session, therefore a message only lasts as long 208 as a session is valid (see `browser-length sessions vs. persistent sessions`_). 209 210 The message system relies on the session middleware and is accessed via 211 ``request.session``. The API is simple: 212 213 * To create a new message, use 214 ``request.session.create_message(message='message text').`` 215 216 * To retreive the messages, use ``request.session.get_messages()``, 217 which returns a list of any messages (strings) in the session's queue. 218 219 * To retrieve and delete messages, use 220 ``request.session.get_and_delete_messages()``, which returns the list of 221 any messages in the session's queue and then deletes the messages from the 222 queue. 223 224 The `django.core.context_processors.messages`_ context processor makes both 225 session messages and user messages available to templates. 226 227 .. _django.core.context_processors.messages: ../templates_python/#django-core-context_processors-messages 228 196 229 Using sessions out of views 197 230 =========================== 198 231 -
docs/authentication.txt
956 956 Messages 957 957 ======== 958 958 959 The message system is a lightweight way to queue messages for given users. 959 The user message system is a lightweight way to queue messages for given users. 960 To send messages to anonymous users, use `session messages`_. 960 961 962 .. _session framework: ../sessions/#messages 963 961 964 A message is associated with a ``User``. There's no concept of expiration or 962 965 timestamps. 963 966 … … 983 986 context_instance=RequestContext(request)) 984 987 985 988 When you use ``RequestContext``, the currently logged-in user and his/her 986 messages are made available in the `template context`_ as the template variable987 ``{{ messages }}``. Here's an example of template code that displays messages:: 989 messages are made available in the `template context`_ as the ``{{ messages }}`` 990 template variable. 988 991 989 {% if messages %} 990 <ul> 991 {% for message in messages %} 992 <li>{{ message }}</li> 993 {% endfor %} 994 </ul> 995 {% endif %} 992 **New in Django development version** 996 993 997 Note that ``RequestContext`` calls ``get_and_delete_messages`` behind the 998 scenes, so any messages will be deleted even if you don't display them.994 The ``{{ messages }}`` template variable will also contain session messages. 995 For more information, see `django.core.context_processors.messages`_. 999 996 1000 Finally, note that this messages framework only works with users in the user 1001 database. To send messages to anonymous users, use the `session framework`_. 997 .. _django.core.context_processors.messages: ../templates_python/#django-core-context_processors-messages 1002 998 1003 .. _session framework: ../sessions/ 999 Also note that previously, ``RequestContext`` directly called 1000 ``get_and_delete_messages`` behind the scenes, so any messages were deleted even 1001 if not displayed. Messages are now only deleted if the ``{{ messages }}`` 1002 variable is accessed in a template. 1004 1003 1005 1004 Other authentication sources 1006 1005 ============================ -
docs/templates_python.txt
295 295 ("django.core.context_processors.auth", 296 296 "django.core.context_processors.debug", 297 297 "django.core.context_processors.i18n", 298 "django.core.context_processors.media") 298 "django.core.context_processors.media", 299 "django.core.context_processors.messages") 299 300 300 301 Each processor is applied in order. That means, if one processor adds a 301 302 variable to the context and a second processor adds a variable with the same … … 345 346 logged-in user (or an ``AnonymousUser`` instance, if the client isn't 346 347 logged in). See the `user authentication docs`_. 347 348 348 * ``messages`` -- A list of messages (as strings) for the currently349 logged-in user. Behind the scenes, this calls350 ``request.user.get_and_delete_messages()`` for every request. That method351 collects the user's messages and deletes them from the database.352 353 349 Note that messages are set with ``user.message_set.create``. See the 354 350 `message docs`_ for more. 355 351 … … 358 354 permissions that the currently logged-in user has. See the `permissions 359 355 docs`_. 360 356 361 .. _user authentication docs: ../authentication/#users 362 .. _message docs: ../authentication/#messages 363 .. _permissions docs: ../authentication/#permissions 357 **New in Django development version** 364 358 359 Previously, a ``messages`` variable was also added to ``RequestContext`` 360 containing a list of messages for the currently logged-in user. This 361 functionality has been moved to the `django.core.context_processors.messages`_ 362 context processor. 363 365 364 django.core.context_processors.debug 366 365 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 367 366 … … 409 408 `HttpRequest object`_. Note that this processor is not enabled by default; 410 409 you'll have to activate it. 411 410 411 django.core.context_processors.messages 412 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 413 414 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every 415 ``RequestContext`` will contain a variable ``messages``, which is a list of 416 messages (as strings) containing any messages for the current session (see 417 `session messages`_) or the currently logged-in user (see `auth messages`_). 418 419 The messages are not retrieved and cleared (using ``get_and_delete_messages``) 420 until the ``messages`` variable is accessed in a template. 421 422 Here's an example of template code that displays messages made available by this 423 context processor:: 424 425 {% if messages %} 426 <ul> 427 {% for message in messages %} 428 <li>{{ message }}</li> 429 {% endfor %} 430 </ul> 431 {% endif %} 432 433 .. _session messages: ../sessions/#messages 434 .. _auth messages: ../authentication/#messages 435 412 436 Writing your own context processors 413 437 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 414 438