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)
21 #print inspect.stack()[1]
23 return { 'user_messages': messages }
25 class LazyMessages (StrAndUnicode):
27 Lazy message container, so messages aren't actually retrieved from
28 session and deleted until the template asks for them.
31 def __init__(self, request):
32 self.request = request
35 return iter(self.messages)
38 return len(self.messages)
40 def __nonzero__(self):
41 return bool(self.messages)
43 def __unicode__(self):
44 return unicode(self.messages)
46 def __getitem__(self, *args, **kwargs):
47 return self.messages.__getitem__(*args, **kwargs)
49 def _get_messages(self):
50 if hasattr(self, '_messages'):
52 self._messages = get_and_delete_messages(self.request)
54 messages = property(_get_messages)