2 Context processor for lightweight session messages.
4 Time-stamp: <2008-07-19 23:16:19 carljm context_processors.py>
7 from django.utils.encoding import StrAndUnicode
9 from forum.user_messages import get_and_delete_messages
11 def user_messages (request):
13 Returns session messages for the current session.
16 messages = request.user.get_and_delete_messages()
17 #if request.user.is_authenticated():
19 # messages = LazyMessages(request)
20 return { 'user_messages': messages }
22 class LazyMessages (StrAndUnicode):
24 Lazy message container, so messages aren't actually retrieved from
25 session and deleted until the template asks for them.
28 def __init__(self, request):
29 self.request = request
32 return iter(self.messages)
35 return len(self.messages)
37 def __nonzero__(self):
38 return bool(self.messages)
40 def __unicode__(self):
41 return unicode(self.messages)
43 def __getitem__(self, *args, **kwargs):
44 return self.messages.__getitem__(*args, **kwargs)
46 def _get_messages(self):
47 if hasattr(self, '_messages'):
49 self._messages = get_and_delete_messages(self.request)
51 messages = property(_get_messages)