]> git.openstreetmap.org Git - osqa.git/blob - forum/middleware/anon_user.py
use latest jQuery version
[osqa.git] / forum / middleware / anon_user.py
1 from django.http import HttpResponseRedirect, HttpResponse
2 from forum.forms import get_next_url
3 from django.utils.translation import ugettext as _
4 from forum.user_messages import create_message, get_and_delete_messages
5 from forum import settings
6 from django.core.urlresolvers import reverse
7 import logging
8
9 class AnonymousMessageManager(object):
10     def __init__(self,request):
11         self.request = request
12     def create(self,message=''):
13         create_message(self.request,message)  
14     def get_and_delete(self):
15         messages = get_and_delete_messages(self.request)
16         return messages
17
18 def dummy_deepcopy(*arg):
19     """this is necessary to prevent deepcopy() on anonymous user object
20     that now contains reference to request, which cannot be deepcopied
21     """
22     return None
23
24 class ConnectToSessionMessagesMiddleware(object):
25     def process_request(self, request):
26         if not request.user.is_authenticated():
27             request.user.__deepcopy__ = dummy_deepcopy #plug on deepcopy which may be called by django db "driver"
28             request.user.message_set = AnonymousMessageManager(request) #here request is linked to anon user
29             request.user.get_and_delete_messages = request.user.message_set.get_and_delete
30
31             #also set the first greeting one time per session only
32             if 'greeting_set' not in request.session:
33                 request.session['greeting_set'] = True
34
35                 msg = _('First time here? Check out the <a href="%s">FAQ</a>!') % reverse('faq')
36
37                 # If the store greeting in cookie setting is activated make sure that the greeting_set cookies isn't set
38                 if (settings.STORE_GREETING_IN_COOKIE and not request.COOKIES.has_key('greeting_set')) or \
39                   not settings.STORE_GREETING_IN_COOKIE:
40                     request.user.message_set.create(message=msg)
41
42                 if settings.STORE_GREETING_IN_COOKIE:
43                     request.COOKIES.set(key='greeting_set', value=True)