]> git.openstreetmap.org Git - osqa.git/blob - osqa/forum/middleware/anon_user.py
initial import
[osqa.git] / osqa / forum / middleware / anon_user.py
1 from django.http import HttpResponseRedirect
2 from forum.utils.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 django.conf 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                 msg = _('First time here? Check out the <a href="%s">FAQ</a>!') % reverse('faq')
35                 request.user.message_set.create(message=msg)