]> git.openstreetmap.org Git - osqa.git/blob - forum/user_messages/context_processors.py
OSQA-829, resolves a unicode problem when answering question, use smart_unicode for...
[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     #import inspect
21     #print inspect.stack()[1]
22     #print messages
23     return { 'user_messages': messages }
24
25 class LazyMessages (StrAndUnicode):
26     """
27     Lazy message container, so messages aren't actually retrieved from
28     session and deleted until the template asks for them.
29
30     """
31     def __init__(self, request):
32         self.request = request
33
34     def __iter__(self):
35         return iter(self.messages)
36
37     def __len__(self):
38         return len(self.messages)
39
40     def __nonzero__(self):
41         return bool(self.messages)
42
43     def __unicode__(self):
44         return unicode(self.messages)
45
46     def __getitem__(self, *args, **kwargs):
47         return self.messages.__getitem__(*args, **kwargs)
48
49     def _get_messages(self):
50         if hasattr(self, '_messages'):
51             return self._messages
52         self._messages = get_and_delete_messages(self.request)
53         return self._messages
54     messages = property(_get_messages)
55