2017-05-29 10:18:59 +02:00
|
|
|
# flake8: noqa
|
2017-10-10 19:31:51 +02:00
|
|
|
import json
|
2017-10-31 16:08:20 +01:00
|
|
|
from collections import OrderedDict
|
2017-11-28 16:17:08 +01:00
|
|
|
from typing import Optional
|
2016-12-15 17:30:55 +01:00
|
|
|
|
2016-12-22 02:40:12 +01:00
|
|
|
import qrcode
|
2017-11-21 04:34:43 +01:00
|
|
|
from django.conf import settings
|
2017-10-31 16:08:20 +01:00
|
|
|
from django.core.cache import cache
|
2017-11-30 13:31:45 +01:00
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
2017-11-30 18:23:47 +01:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseBadRequest
|
2017-11-28 16:17:08 +01:00
|
|
|
from django.shortcuts import render
|
2016-12-20 20:29:18 +01:00
|
|
|
from django.urls import reverse
|
2017-11-30 18:23:47 +01:00
|
|
|
from django.views.decorators.cache import cache_control
|
|
|
|
from django.views.decorators.http import etag
|
2016-12-17 19:25:27 +01:00
|
|
|
|
2017-10-31 16:08:20 +01:00
|
|
|
from c3nav.mapdata.models import Location, Source
|
|
|
|
from c3nav.mapdata.models.access import AccessPermission
|
2017-06-11 14:43:14 +02:00
|
|
|
from c3nav.mapdata.models.level import Level
|
2017-10-31 15:28:49 +01:00
|
|
|
from c3nav.mapdata.models.locations import LocationRedirect, SpecificLocation
|
2017-11-28 20:24:39 +01:00
|
|
|
from c3nav.mapdata.utils.locations import get_location_by_slug_for_request, levels_by_short_label_for_request
|
2017-11-21 00:15:49 +01:00
|
|
|
from c3nav.mapdata.views import set_tile_access_cookie
|
2016-12-15 17:30:55 +01:00
|
|
|
|
2016-12-22 02:40:12 +01:00
|
|
|
|
2017-10-31 15:28:49 +01:00
|
|
|
def check_location(location: Optional[str], request) -> Optional[SpecificLocation]:
|
|
|
|
if location is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
location = get_location_by_slug_for_request(location, request)
|
|
|
|
if location is None:
|
2017-10-31 18:35:42 +01:00
|
|
|
return None
|
2017-10-31 15:28:49 +01:00
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
|
|
|
location: Location = location.target
|
|
|
|
if location is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not location.can_search:
|
|
|
|
location = None
|
|
|
|
|
|
|
|
return location
|
|
|
|
|
|
|
|
|
2017-11-21 00:15:49 +01:00
|
|
|
@set_tile_access_cookie
|
2017-11-22 18:02:11 +01:00
|
|
|
def map_index(request, mode=None, slug=None, slug2=None, details=None, level=None, x=None, y=None, zoom=None):
|
2017-10-31 18:35:42 +01:00
|
|
|
origin = None
|
|
|
|
destination = None
|
|
|
|
routing = False
|
|
|
|
if slug2 is not None:
|
|
|
|
routing = True
|
|
|
|
origin = check_location(slug, request)
|
|
|
|
destination = check_location(slug2, request)
|
|
|
|
else:
|
|
|
|
routing = (mode and mode != 'l')
|
|
|
|
if mode == 'o':
|
|
|
|
origin = check_location(slug, request)
|
|
|
|
else:
|
|
|
|
destination = check_location(slug, request)
|
2017-10-31 15:28:49 +01:00
|
|
|
|
|
|
|
state = {
|
2017-10-31 18:35:42 +01:00
|
|
|
'routing': routing,
|
2017-10-31 15:28:49 +01:00
|
|
|
'origin': (origin.serialize(detailed=False, simple_geometry=True, geometry=False)
|
|
|
|
if origin else None),
|
|
|
|
'destination': (destination.serialize(detailed=False, simple_geometry=True, geometry=False)
|
|
|
|
if destination else None),
|
2017-10-31 18:35:42 +01:00
|
|
|
'sidebar': routing or destination is not None,
|
2017-11-22 18:02:11 +01:00
|
|
|
'details': True if details else False,
|
2017-10-31 15:28:49 +01:00
|
|
|
}
|
2017-10-31 16:08:20 +01:00
|
|
|
|
2017-11-28 20:24:39 +01:00
|
|
|
levels = levels_by_short_label_for_request(request)
|
2017-10-31 16:08:20 +01:00
|
|
|
|
|
|
|
level = levels.get(level, None) if level else None
|
2017-10-31 15:28:49 +01:00
|
|
|
if level is not None:
|
|
|
|
state.update({
|
2017-11-28 20:44:15 +01:00
|
|
|
'level': level.pk,
|
2017-10-31 15:28:49 +01:00
|
|
|
'center': (float(x), float(y)),
|
|
|
|
'zoom': float(zoom),
|
|
|
|
})
|
|
|
|
|
2017-10-10 19:31:51 +02:00
|
|
|
ctx = {
|
2017-10-25 12:15:19 +02:00
|
|
|
'bounds': json.dumps(Source.max_bounds(), separators=(',', ':')),
|
2017-11-28 20:38:37 +01:00
|
|
|
'levels': json.dumps(tuple((level.pk, level.short_label) for level in levels.values()), separators=(',', ':')),
|
2017-11-30 13:31:45 +01:00
|
|
|
'state': json.dumps(state, separators=(',', ':'), cls=DjangoJSONEncoder),
|
2017-11-21 04:34:43 +01:00
|
|
|
'tile_cache_server': settings.TILE_CACHE_SERVER,
|
2017-10-10 19:31:51 +02:00
|
|
|
}
|
2017-11-21 00:15:49 +01:00
|
|
|
return render(request, 'site/map.html', ctx)
|
2017-11-30 18:23:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def qr_code_etag(request, path):
|
|
|
|
return '1'
|
|
|
|
|
|
|
|
|
|
|
|
@etag(qr_code_etag)
|
|
|
|
@cache_control(max_age=3600)
|
|
|
|
def qr_code(request, path):
|
|
|
|
data = (request.build_absolute_uri('/'+path) +
|
|
|
|
('?'+request.META['QUERY_STRING'] if request.META['QUERY_STRING'] else ''))
|
|
|
|
if len(data) > 256:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
qr = qrcode.QRCode(
|
|
|
|
version=1,
|
|
|
|
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
|
|
box_size=10,
|
|
|
|
border=4,
|
|
|
|
)
|
|
|
|
qr.add_data(data)
|
|
|
|
qr.make(fit=True)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type='image/png')
|
|
|
|
qr.make_image().save(response, 'PNG')
|
|
|
|
return response
|