blob: df9840fd96f02c37af140e18ba16a910b33d647d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
"""
Context processor for lightweight session messages.
Time-stamp: <2008-07-19 23:16:19 carljm context_processors.py>
"""
from django.utils.encoding import StrAndUnicode
from session_messages import get_and_delete_messages
def session_messages (request):
"""
Returns session messages for the current session.
"""
return { 'session_messages': LazyMessages(request) }
class LazyMessages (StrAndUnicode):
"""
Lazy message container, so messages aren't actually retrieved from
session and deleted until the template asks for them.
"""
def __init__(self, request):
self.request = request
def __iter__(self):
return iter(self.messages)
def __len__(self):
return len(self.messages)
def __nonzero__(self):
return bool(self.messages)
def __unicode__(self):
return unicode(self.messages)
def __getitem__(self, *args, **kwargs):
return self.messages.__getitem__(*args, **kwargs)
def _get_messages(self):
if hasattr(self, '_messages'):
return self._messages
self._messages = get_and_delete_messages(self.request)
return self._messages
messages = property(_get_messages)
|