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-28 16:17:08 +01:00
|
|
|
from django.http import Http404, HttpResponse
|
|
|
|
from django.shortcuts import render
|
2016-12-20 20:29:18 +01:00
|
|
|
from django.urls import reverse
|
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
|
|
|
|
from c3nav.mapdata.utils.locations import get_location_by_slug_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-19 01:31:20 +01:00
|
|
|
ctype_mapping = {
|
|
|
|
'yes': ('up', 'down'),
|
|
|
|
'up': ('up', ),
|
|
|
|
'down': ('down', ),
|
|
|
|
'no': ()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_ctypes(prefix, value):
|
2016-12-19 12:24:07 +01:00
|
|
|
return tuple((prefix+'_'+direction) for direction in ctype_mapping.get(value, ('up', 'down')))
|
2016-12-15 17:30:55 +01:00
|
|
|
|
2016-12-15 18:28:04 +01:00
|
|
|
|
2016-12-19 23:58:12 +01:00
|
|
|
def reverse_ctypes(ctypes, name):
|
|
|
|
if name+'_up' in ctypes:
|
|
|
|
return 'yes' if name + '_down' in ctypes else 'up'
|
|
|
|
else:
|
|
|
|
return 'down' if name + '_down' in ctypes else 'no'
|
|
|
|
|
|
|
|
|
2016-12-20 20:29:18 +01:00
|
|
|
def get_location_or_404(request, location):
|
|
|
|
if location is None:
|
|
|
|
return None
|
2016-12-15 18:28:04 +01:00
|
|
|
|
2017-05-12 01:21:53 +02:00
|
|
|
get_location = None
|
2016-12-20 20:29:18 +01:00
|
|
|
location = get_location(request, location)
|
|
|
|
if location is None:
|
|
|
|
raise Http404
|
2016-12-15 18:28:04 +01:00
|
|
|
|
2016-12-20 20:29:18 +01:00
|
|
|
return location
|
|
|
|
|
|
|
|
|
2016-12-22 02:40:12 +01:00
|
|
|
def qr_code(request, location):
|
|
|
|
location = get_location_or_404(request, location)
|
|
|
|
|
|
|
|
qr = qrcode.QRCode(
|
|
|
|
version=1,
|
|
|
|
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
|
|
box_size=10,
|
|
|
|
border=4,
|
|
|
|
)
|
|
|
|
qr.add_data(request.build_absolute_uri(reverse('site.location', kwargs={'location': location.location_id})))
|
|
|
|
qr.make(fit=True)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type='image/png')
|
|
|
|
qr.make_image().save(response, 'PNG')
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
levels_cache_key = 'mapdata:levels:%s' % AccessPermission.cache_key_for_request(request)
|
|
|
|
levels = cache.get(levels_cache_key, None)
|
|
|
|
if levels is None:
|
|
|
|
levels = OrderedDict(
|
|
|
|
(level.slug, (level.pk, level.slug, level.short_label))
|
2017-11-05 17:54:03 +01:00
|
|
|
for level in Level.qs_for_request(request).filter(on_top_of_id__isnull=True).order_by('base_altitude')
|
2017-10-31 16:08:20 +01:00
|
|
|
)
|
|
|
|
cache.set(levels_cache_key, levels, 300)
|
|
|
|
|
|
|
|
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-10-31 16:08:20 +01:00
|
|
|
'level': level[0],
|
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-10-31 16:08:20 +01:00
|
|
|
'levels': json.dumps(tuple(levels.values()), separators=(',', ':')),
|
2017-10-31 15:45:07 +01:00
|
|
|
'state': json.dumps(state, separators=(',', ':')),
|
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)
|