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