Ticket #4604: messages.3.diff
File messages.3.diff, 15.1 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
24 24 user = AnonymousUser() 25 25 return { 26 26 'user': user, 27 'messages': user.get_and_delete_messages(),28 27 'perms': PermWrapper(user), 29 28 } 30 29 30 class LazyMessages(object): 31 def __init__(self, request): 32 self.request = request 33 def __iter__(self): 34 return self.messages.__iter__() 35 def __len__(self): 36 return len(self.messages) 37 def __nonzero__(self): 38 return bool(self.messages) 39 def __unicode__(self): 40 return unicode(self.messages) 41 def _get_messages(self): 42 if hasattr(self, '_messages'): 43 return self._messages 44 # First, retreive any messages for the user. 45 if hasattr(self.request, 'user') and \ 46 hasattr(self.request.user, 'get_and_delete_messages'): 47 self._messages = self.request.user.get_and_delete_messages() 48 else: 49 self._messages = [] 50 # Next, retrieve any messages for the session. 51 if hasattr(self.request, '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 Both contrib.session and contrib.auth are optional. If neither is provided, 64 no 'messages' variable will be added to the context. 65 """ 66 if hasattr(request, 'session') or hasattr(request, 'user'): 67 return {'messages': LazyMessages(request)} 68 return {} 69 31 70 def debug(request): 32 71 "Returns context variables helpful for debugging." 33 72 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) -
tests/regressiontests/messages/tests.py
1 """ 2 >>> from django.core import context_processors 3 >>> from django.http import HttpRequest 4 5 Set up request with a fake user and session (just enough to test getting and 6 deleting messages). 7 >>> request = HttpRequest() 8 >>> class FakeMessageObj: 9 ... def __init__(self, object, messages): 10 ... self.messages = messages 11 ... self.object = object 12 ... def get_and_delete_messages(self): 13 ... print 'Getting and deleting any %s messages...' % self.object 14 ... m = self.messages 15 ... self.messages = [] 16 ... return m 17 >>> request.user = FakeMessageObj('user', ['User message']) 18 >>> request.session = FakeMessageObj('session', ['Message 1', 'Second message']) 19 20 Run the messages context processor, and pull out the messages context variable. 21 >>> context = context_processors.messages(request) 22 >>> messages = context['messages'] 23 24 The messages context variable is a LazyMessages class. The messages haven't 25 actually been retreived (and deleted) yet. 26 >>> messages.__class__ 27 <class 'django.core.context_processors.LazyMessages'> 28 29 When any of the following methods are called, the messages are retreived from 30 the session (and user if contrib.auth is installed) from the LazyMessages 31 object to be retreived: __iter__, __len__, __nonzero__, __unicode__ 32 >>> len(messages) 33 Getting and deleting any user messages... 34 Getting and deleting any session messages... 35 3 36 37 When messages are retreived, messages are deleted from the session (and user if 38 contrib.auth is installed). 39 >>> request.user.messages 40 [] 41 >>> request.session.messages 42 [] 43 44 The messages are still available to the LazyMessages instance because it caches 45 them. 46 >>> for message in messages: 47 ... print message 48 User message 49 Message 1 50 Second message 51 52 Both contrib.sessions and contrib.auth are optional. If neither are provided, 53 no 'messages' variable will be added to the context. 54 >>> del request.user 55 >>> request.session = FakeMessageObj('session', []) 56 >>> context = context_processors.messages(request) 57 >>> messages = context['messages'] 58 >>> if messages: 59 ... print 'messages found!' 60 Getting and deleting any session messages... 61 62 >>> del request.session 63 >>> request.user = FakeMessageObj('user', []) 64 >>> context = context_processors.messages(request) 65 >>> messages = context['messages'] 66 >>> if messages: 67 ... print 'messages found!' 68 Getting and deleting any user messages... 69 70 >>> del request.user 71 >>> context = context_processors.messages(request) 72 >>> context 73 {} 74 """ 75 No newline at end of file -
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