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