1 | Index: django/contrib/sessions/tests.py
|
---|
2 | ===================================================================
|
---|
3 | --- django/contrib/sessions/tests.py (revisión: 6596)
|
---|
4 | +++ django/contrib/sessions/tests.py (copia de trabajo)
|
---|
5 | @@ -15,6 +15,19 @@
|
---|
6 | 'dog'
|
---|
7 | >>> db_session.pop('some key', 'does not exist')
|
---|
8 | 'does not exist'
|
---|
9 | +>>> db_session.get_messages()
|
---|
10 | +[]
|
---|
11 | +>>> db_session.create_message('first post')
|
---|
12 | +>>> db_session.get_messages()
|
---|
13 | +['first post']
|
---|
14 | +>>> db_session.get_and_delete_messages()
|
---|
15 | +['first post']
|
---|
16 | +>>> db_session.get_and_delete_messages()
|
---|
17 | +[]
|
---|
18 | +>>> db_session.create_message('hello')
|
---|
19 | +>>> db_session.create_message('world')
|
---|
20 | +>>> db_session.get_and_delete_messages()
|
---|
21 | +['hello', 'world']
|
---|
22 | >>> db_session.save()
|
---|
23 | >>> db_session.exists(db_session.session_key)
|
---|
24 | True
|
---|
25 | @@ -32,6 +45,19 @@
|
---|
26 | 'dog'
|
---|
27 | >>> file_session.pop('some key', 'does not exist')
|
---|
28 | 'does not exist'
|
---|
29 | +>>> file_session.get_messages()
|
---|
30 | +[]
|
---|
31 | +>>> file_session.create_message('first post')
|
---|
32 | +>>> file_session.get_messages()
|
---|
33 | +['first post']
|
---|
34 | +>>> file_session.get_and_delete_messages()
|
---|
35 | +['first post']
|
---|
36 | +>>> file_session.get_and_delete_messages()
|
---|
37 | +[]
|
---|
38 | +>>> file_session.create_message('hello')
|
---|
39 | +>>> file_session.create_message('world')
|
---|
40 | +>>> file_session.get_and_delete_messages()
|
---|
41 | +['hello', 'world']
|
---|
42 | >>> file_session.save()
|
---|
43 | >>> file_session.exists(file_session.session_key)
|
---|
44 | True
|
---|
45 | @@ -49,6 +75,19 @@
|
---|
46 | 'dog'
|
---|
47 | >>> cache_session.pop('some key', 'does not exist')
|
---|
48 | 'does not exist'
|
---|
49 | +>>> cache_session.get_messages()
|
---|
50 | +[]
|
---|
51 | +>>> cache_session.create_message('first post')
|
---|
52 | +>>> cache_session.get_messages()
|
---|
53 | +['first post']
|
---|
54 | +>>> cache_session.get_and_delete_messages()
|
---|
55 | +['first post']
|
---|
56 | +>>> cache_session.get_and_delete_messages()
|
---|
57 | +[]
|
---|
58 | +>>> cache_session.create_message('hello')
|
---|
59 | +>>> cache_session.create_message('world')
|
---|
60 | +>>> cache_session.get_and_delete_messages()
|
---|
61 | +['hello', 'world']
|
---|
62 | >>> cache_session.save()
|
---|
63 | >>> cache_session.delete(cache_session.session_key)
|
---|
64 | >>> cache_session.exists(cache_session.session_key)
|
---|
65 | Index: django/contrib/sessions/backends/base.py
|
---|
66 | ===================================================================
|
---|
67 | --- django/contrib/sessions/backends/base.py (revisión: 6596)
|
---|
68 | +++ django/contrib/sessions/backends/base.py (copia de trabajo)
|
---|
69 | @@ -18,6 +18,7 @@
|
---|
70 | """
|
---|
71 | TEST_COOKIE_NAME = 'testcookie'
|
---|
72 | TEST_COOKIE_VALUE = 'worked'
|
---|
73 | + MESSAGES_NAME = '_messages'
|
---|
74 |
|
---|
75 | def __init__(self, session_key=None):
|
---|
76 | self._session_key = session_key
|
---|
77 | @@ -60,6 +61,21 @@
|
---|
78 | def delete_test_cookie(self):
|
---|
79 | del self[self.TEST_COOKIE_NAME]
|
---|
80 |
|
---|
81 | + def get_messages(self):
|
---|
82 | + return self.get(self.MESSAGES_NAME, [])
|
---|
83 | +
|
---|
84 | + def get_and_delete_messages(self):
|
---|
85 | + return self.pop(self.MESSAGES_NAME, [])
|
---|
86 | +
|
---|
87 | + def create_message(self, message):
|
---|
88 | + messages = self.get(self.MESSAGES_NAME)
|
---|
89 | + if messages is None:
|
---|
90 | + messages = []
|
---|
91 | + self[self.MESSAGES_NAME] = messages
|
---|
92 | + messages.append(message)
|
---|
93 | + self.modified = True
|
---|
94 | +
|
---|
95 | +
|
---|
96 | def encode(self, session_dict):
|
---|
97 | "Returns the given session dictionary pickled and encoded as a string."
|
---|
98 | pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
|
---|
99 | Index: docs/sessions.txt
|
---|
100 | ===================================================================
|
---|
101 | --- docs/sessions.txt (revisión: 6596)
|
---|
102 | +++ docs/sessions.txt (copia de trabajo)
|
---|
103 | @@ -191,6 +191,57 @@
|
---|
104 | request.session.set_test_cookie()
|
---|
105 | return render_to_response('foo/login_form.html')
|
---|
106 |
|
---|
107 | +Visitor Messages
|
---|
108 | +================
|
---|
109 | +
|
---|
110 | +**New in Django development version**
|
---|
111 | +
|
---|
112 | +The session message system provides a simple way to queue messages for
|
---|
113 | +all (anonymous or authenticated) site visitors. To associate messages with users
|
---|
114 | +in the user database, use the `authentication message framework`_.
|
---|
115 | +
|
---|
116 | +.. _authentication message framework: ../authentication/#messages
|
---|
117 | +
|
---|
118 | +Messages are associated with a session, therefore a message only lasts as long
|
---|
119 | +as a session is valid (see `browser-length sessions vs. persistent sessions`_).
|
---|
120 | +
|
---|
121 | +The message system relies on the session middleware and is accessed via
|
---|
122 | +``request.session``. The API is simple:
|
---|
123 | +
|
---|
124 | + * To create a new message, use
|
---|
125 | + ``request.session.create_message(message='message text').``
|
---|
126 | +
|
---|
127 | + * To retreive the messages, use ``request.session.get_messages()``,
|
---|
128 | + which returns a list of any messages (strings) in the session's queue.
|
---|
129 | +
|
---|
130 | + * To retrieve and delete messages, use
|
---|
131 | + ``user_obj.get_and_delete_messages()``, which returns the list of any
|
---|
132 | + messages in the session's queue and then deletes the messages from the
|
---|
133 | + queue.
|
---|
134 | +
|
---|
135 | +django.contrib.sessions.context_processors.visitor_messages
|
---|
136 | +-----------------------------------------------------------
|
---|
137 | +
|
---|
138 | +This `context processor`_ is provided (but not installed by default) which makes
|
---|
139 | +these messages available to the template context as the template variable
|
---|
140 | +``{{ visitor_messages }}`` when you use ``RequestContext``.
|
---|
141 | +
|
---|
142 | +.. _context processor: ../templates_python/#subclassing-context-requestcontext
|
---|
143 | +
|
---|
144 | +Here's an example of template code that displays messages::
|
---|
145 | +
|
---|
146 | + {% if visitor_messages %}
|
---|
147 | + <ul>
|
---|
148 | + {% for message in visitor_messages %}
|
---|
149 | + <li>{{ message|escape }}</li>
|
---|
150 | + {% endfor %}
|
---|
151 | + </ul>
|
---|
152 | + {% endif %}
|
---|
153 | +
|
---|
154 | +Note that when this context processor is accessed by ``RequestContext``,
|
---|
155 | +``get_and_delete_messages`` is called behind the scenes, so any messages will
|
---|
156 | +be deleted even if you don't display them.
|
---|
157 | +
|
---|
158 | Using sessions out of views
|
---|
159 | ===========================
|
---|
160 |
|
---|
161 | Index: docs/authentication.txt
|
---|
162 | ===================================================================
|
---|
163 | --- docs/authentication.txt (revisión: 6596)
|
---|
164 | +++ docs/authentication.txt (copia de trabajo)
|
---|
165 | @@ -947,7 +947,7 @@
|
---|
166 | Finally, note that this messages framework only works with users in the user
|
---|
167 | database. To send messages to anonymous users, use the `session framework`_.
|
---|
168 |
|
---|
169 | -.. _session framework: ../sessions/
|
---|
170 | +.. _session framework: ../sessions/#visitor-messages
|
---|
171 |
|
---|
172 | Other authentication sources
|
---|
173 | ============================
|
---|