2017-06-20 14:02:30 +02:00
|
|
|
from functools import wraps
|
|
|
|
|
2018-09-19 19:08:47 +02:00
|
|
|
from django.core.exceptions import PermissionDenied
|
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
|
2018-09-19 19:08:47 +02:00
|
|
|
from c3nav.mapdata.utils.user import can_access_editor
|
2017-06-20 14:02:30 +02:00
|
|
|
|
|
|
|
|
2018-11-21 01:35:31 +01:00
|
|
|
def sidebar_view(func=None, select_related=None, api_hybrid=False):
|
2017-07-05 16:17:19 +02:00
|
|
|
if func is None:
|
|
|
|
def wrapped(inner_func):
|
2018-11-21 01:35:31 +01:00
|
|
|
return sidebar_view(inner_func, select_related=select_related, api_hybrid=api_hybrid)
|
2017-07-05 16:17:19 +02:00
|
|
|
return wrapped
|
|
|
|
|
2017-06-20 14:02:30 +02:00
|
|
|
@wraps(func)
|
2018-11-21 01:35:31 +01:00
|
|
|
def wrapped(request, *args, api=False, **kwargs):
|
|
|
|
if api and api_hybrid:
|
|
|
|
raise Exception('API call on a view without api_hybrid!')
|
|
|
|
|
2018-09-19 19:08:47 +02:00
|
|
|
if not can_access_editor(request):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
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
|
|
|
|
2018-11-21 01:35:31 +01:00
|
|
|
if api:
|
|
|
|
return call_api_hybrid_view_for_api(func, request, *args, **kwargs)
|
2017-10-27 17:21:04 +02:00
|
|
|
|
2018-11-21 01:35:31 +01:00
|
|
|
ajax = request.is_ajax() or 'ajax' in request.GET
|
2017-10-27 17:21:04 +02:00
|
|
|
if not ajax:
|
2017-10-27 17:08:36 +02:00
|
|
|
request.META.pop('HTTP_IF_NONE_MATCH', None)
|
|
|
|
|
2018-11-21 01:35:31 +01:00
|
|
|
if api_hybrid:
|
|
|
|
response = call_api_hybrid_view_for_html(func, request, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
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-28 21:49:30 +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
|
2018-09-18 17:07:58 +02:00
|
|
|
response = render(request, 'editor/map.html', {'content': response.content.decode()})
|
2017-10-27 17:15:55 +02:00
|
|
|
response['Cache-Control'] = 'no-cache'
|
2017-10-28 21:49:30 +02:00
|
|
|
patch_vary_headers(response, ('X-Requested-With', ))
|
2017-10-27 17:15:55 +02:00
|
|
|
return response
|
2018-11-21 01:35:31 +01:00
|
|
|
wrapped.api_hybrid = api_hybrid
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def call_api_hybrid_view_for_api(func, request, *args, **kwargs):
|
|
|
|
response = func(request, *args, **kwargs)
|
|
|
|
return response
|
|
|
|
|
2017-10-27 17:15:55 +02:00
|
|
|
|
2018-11-21 01:35:31 +01:00
|
|
|
def call_api_hybrid_view_for_html(func, request, *args, **kwargs):
|
|
|
|
response = func(request, *args, **kwargs)
|
|
|
|
return response
|
2017-10-27 17:08:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def etag_func(request, *args, **kwargs):
|
2017-11-30 17:05:56 +01:00
|
|
|
try:
|
|
|
|
changeset = request.changeset
|
|
|
|
except AttributeError:
|
|
|
|
changeset = ChangeSet.get_for_request(request)
|
|
|
|
request.changeset = changeset
|
|
|
|
|
|
|
|
return (get_language() + ':' + changeset.raw_cache_key_by_changes + ':' +
|
2018-11-21 01:29:35 +01:00
|
|
|
AccessPermission.cache_key_for_request(request, with_update=False) + ':' + str(request.user.pk or 0)
|
|
|
|
+ ':' + str(int(request.user_permissions.can_access_base_mapdata))
|
|
|
|
+ ':' + str(int(request.user.is_superuser)))
|