Serwis powinien już działać bez Firebuga.
[redakcja.git] / apps / explorer / middleware.py
1 import urllib
2
3 from django.conf import settings
4 from django.utils import simplejson
5
6 from explorer import models
7
8
9 EDITOR_DEFAULT_SETTINGS_JSON = simplejson.dumps(settings.EDITOR_DEFAULT_SETTINGS)
10
11
12 class EditorSettingsMiddleware(object):
13     def process_request(self, request):
14         if request.user.is_anonymous():
15             return
16         cookie_settings = request.COOKIES.get(settings.EDITOR_COOKIE_NAME, EDITOR_DEFAULT_SETTINGS_JSON)
17         
18         cookie_settings = simplejson.loads(urllib.unquote(cookie_settings))
19         last_update = cookie_settings.get('lastUpdate', 0)
20         cookie_settings['lastUpdate'] = last_update
21         
22         try:
23             editor_settings = models.EditorSettings.objects.get(user=request.user)
24         except models.EditorSettings.DoesNotExist:
25             editor_settings = models.EditorSettings(user=request.user)
26             editor_settings.set_settings_value(cookie_settings)
27             editor_settings.save()
28
29         # print editor_settings.get_settings_value()['lastUpdate'], '<>', last_update
30         if editor_settings.get_settings_value()['lastUpdate'] < last_update:
31             editor_settings.set_settings_value(cookie_settings)
32             editor_settings.save()
33
34         request.editor_settings = editor_settings
35
36
37     def process_response(self, request, response):
38         if hasattr(request, 'editor_settings'):
39             response.set_cookie(settings.EDITOR_COOKIE_NAME,
40                 urllib.quote(request.editor_settings.settings), max_age=7 * 60 * 60 * 24, path='/')
41
42         return response