3 from django.conf import settings
4 from django.utils import simplejson
6 from explorer import models
9 EDITOR_DEFAULT_SETTINGS_JSON = simplejson.dumps(settings.EDITOR_DEFAULT_SETTINGS)
12 class EditorSettingsMiddleware(object):
13 def process_request(self, request):
14 if request.user.is_anonymous():
16 cookie_settings = request.COOKIES.get(settings.EDITOR_COOKIE_NAME, EDITOR_DEFAULT_SETTINGS_JSON)
18 cookie_settings = simplejson.loads(urllib.unquote(cookie_settings))
19 last_update = cookie_settings.get('lastUpdate', 0)
20 cookie_settings['lastUpdate'] = last_update
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()
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()
34 request.editor_settings = editor_settings
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='/')