2017-06-20 14:02:30 +02:00
|
|
|
from functools import wraps
|
|
|
|
|
2017-10-27 17:08:36 +02:00
|
|
|
from django.http import HttpResponseNotModified, HttpResponseRedirect
|
2017-06-20 14:02:30 +02:00
|
|
|
from django.shortcuts import render
|
2017-10-27 17:23:58 +02:00
|
|
|
from django.utils.cache import patch_vary_headers
|
2017-10-28 21:47:53 +02:00
|
|
|
from django.utils.translation import get_language
|
2017-06-20 14:02:30 +02:00
|
|
|
|
|
|
|
from c3nav.editor.models import ChangeSet
|
2017-10-27 17:08:36 +02:00
|
|
|
from c3nav.mapdata.models.access import AccessPermission
|
2017-06-20 14:02:30 +02:00
|
|
|
|
|
|
|
|
2017-07-05 16:17:19 +02:00
|
|
|
def sidebar_view(func=None, select_related=None):
|
|
|
|
if func is None:
|
|
|
|
def wrapped(inner_func):
|
|
|
|
return sidebar_view(inner_func, select_related)
|
|
|
|
return wrapped
|
|
|
|
|
2017-06-20 14:02:30 +02:00
|
|
|
@wraps(func)
|
|
|
|
def with_ajax_check(request, *args, **kwargs):
|
2017-07-05 16:17:19 +02:00
|
|
|
request.changeset = ChangeSet.get_for_request(request, select_related)
|
2017-06-20 14:02:30 +02:00
|
|
|
|
2017-10-27 17:21:04 +02:00
|
|
|
ajax = request.is_ajax() or 'ajax' in request.GET
|
|
|
|
|
|
|
|
if not ajax:
|
2017-10-27 17:08:36 +02:00
|
|
|
request.META.pop('HTTP_IF_NONE_MATCH', None)
|
|
|
|
|
2017-06-20 14:02:30 +02:00
|
|
|
response = func(request, *args, **kwargs)
|
2017-10-27 17:21:04 +02:00
|
|
|
if ajax:
|
2017-06-20 14:02:30 +02:00
|
|
|
if isinstance(response, HttpResponseRedirect):
|
|
|
|
return render(request, 'editor/redirect.html', {'target': response['location']})
|
2017-10-27 17:08:36 +02:00
|
|
|
if not isinstance(response, HttpResponseNotModified):
|
|
|
|
response.write(render(request, 'editor/fragment_nav.html', {}).content)
|
2017-10-27 17:15:55 +02:00
|
|
|
response['Cache-Control'] = 'no-cache'
|
2017-10-27 17:23:58 +02:00
|
|
|
patch_vary_headers(response, 'X-Requested-With')
|
2017-06-20 14:02:30 +02:00
|
|
|
return response
|
|
|
|
if isinstance(response, HttpResponseRedirect):
|
|
|
|
return response
|
2017-10-27 17:15:55 +02:00
|
|
|
response = render(request, 'editor/map.html', {'content': response.content})
|
|
|
|
response['Cache-Control'] = 'no-cache'
|
2017-10-27 17:23:58 +02:00
|
|
|
patch_vary_headers(response, 'X-Requested-With')
|
2017-10-27 17:15:55 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
return with_ajax_check
|
2017-10-27 17:08:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def etag_func(request, *args, **kwargs):
|
2017-10-28 21:47:53 +02:00
|
|
|
return (get_language() + ':' + request.changeset.raw_cache_key_by_changes + ':' +
|
2017-10-27 17:08:36 +02:00
|
|
|
AccessPermission.cache_key_for_request(request, with_update=False))
|