diff --git a/src/c3nav/mapdata/api.py b/src/c3nav/mapdata/api.py index 883a9103..722f29d5 100644 --- a/src/c3nav/mapdata/api.py +++ b/src/c3nav/mapdata/api.py @@ -7,7 +7,9 @@ from collections import OrderedDict from django.conf import settings from django.core.files import File from django.http import Http404, HttpResponse, HttpResponseNotModified -from rest_framework.decorators import detail_route +from django.http import HttpResponseBadRequest +from django.views.decorators.csrf import csrf_exempt +from rest_framework.decorators import detail_route, list_route, api_view from rest_framework.response import Response from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet @@ -18,7 +20,7 @@ from c3nav.mapdata.models.geometry import DirectedLineGeometryMapItemWithLevel from c3nav.mapdata.search import get_location from c3nav.mapdata.serializers.main import LevelSerializer, PackageSerializer, SourceSerializer from c3nav.mapdata.utils.cache import (CachedReadOnlyViewSetMixin, cache_mapdata_api_response, get_levels_cached, - get_packages_cached) + get_packages_cached, get_bssid_areas_cached) class GeometryTypeViewSet(ViewSet): @@ -201,3 +203,20 @@ class LocationViewSet(ViewSet): if location is None: raise Http404 return Response(location.to_json()) + + @list_route(methods=['POST']) + def wifilocate(self, request): + stations = json.loads(request.POST['stations'])[:200] + if not stations: + return Response({}) + + bssids = get_bssid_areas_cached() + stations = sorted(stations, key=lambda l: l['level']) + for station in stations: + area_name = bssids.get(station['bssid']) + if area_name is not None: + location = get_location(request, area_name) + if location is not None: + return Response({'location': location.to_location_json()}); + + return Response({'location': None}) diff --git a/src/c3nav/mapdata/models/locations.py b/src/c3nav/mapdata/models/locations.py index 32fbe25d..8c96d708 100644 --- a/src/c3nav/mapdata/models/locations.py +++ b/src/c3nav/mapdata/models/locations.py @@ -347,7 +347,7 @@ class PointLocation(Location): subtitle += ' - '+add_subtitle return subtitle - def to_json(self): + def to_location_json(self): result = super().to_location_json() result['level'] = self.level.name result['x'] = self.x diff --git a/src/c3nav/mapdata/utils/cache.py b/src/c3nav/mapdata/utils/cache.py index 866092c1..de843b04 100644 --- a/src/c3nav/mapdata/utils/cache.py +++ b/src/c3nav/mapdata/utils/cache.py @@ -1,5 +1,6 @@ from calendar import timegm from collections import OrderedDict +from django.db.models import Q from functools import wraps from django.core.cache import cache @@ -98,3 +99,13 @@ def get_levels_cached(): def get_packages_cached(): from c3nav.mapdata.models import Package return {package.name: package for package in Package.objects.all()} + + +@cache_result('c3nav__mapdata__bssids') +def get_bssid_areas_cached(): + from c3nav.mapdata.models import AreaLocation + bssids = {} + for area in AreaLocation.objects.filter(~Q(bssids='')): + for bssid in area.bssids.split('\n'): + bssids[bssid] = area.name + return bssids diff --git a/src/c3nav/settings.py b/src/c3nav/settings.py index a152651d..103919d8 100644 --- a/src/c3nav/settings.py +++ b/src/c3nav/settings.py @@ -188,8 +188,11 @@ USE_TZ = True REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( - 'c3nav.api.authentication.ForceCSRFCheckSessionAuthentication', + 'rest_framework.authentication.TokenAuthentication', ), + 'DEFAULT_PERMISSION_CLASSES': ( + 'rest_framework.permissions.AllowAny', + ) } LOCALE_PATHS = ( diff --git a/src/c3nav/site/static/site/css/c3nav.css b/src/c3nav/site/static/site/css/c3nav.css index 29d4a19c..22de1e58 100644 --- a/src/c3nav/site/static/site/css/c3nav.css +++ b/src/c3nav/site/static/site/css/c3nav.css @@ -46,6 +46,9 @@ body, .btn { .locationselect .icons .map { background-image:url('../img/icons/map.svg'); } +.locationselect .icons .locate { + background-image:url('../img/icons/locate.svg'); +} .location-group .only-if-selected { display:none; diff --git a/src/c3nav/site/static/site/js/c3nav.js b/src/c3nav/site/static/site/js/c3nav.js index d2787509..8760fe44 100644 --- a/src/c3nav/site/static/site/js/c3nav.js +++ b/src/c3nav/site/static/site/js/c3nav.js @@ -8,8 +8,8 @@ c3nav = { c3nav.visible_areas = c3nav.main_view.attr('data-visible-areas').split(';'); c3nav.qr_modal = $('#qr_modal'); - c3nav.mobileclient = (typeof mobileclient !== "undefined") - if (c3nav.mobileclient) { + c3nav.mobileclient = (typeof mobileclient !== "undefined"); + if (c3nav.mobileclient || true) { $('body').removeClass('nomobileclient'); } diff --git a/src/c3nav/site/templates/site/fragment_location.html b/src/c3nav/site/templates/site/fragment_location.html index b8ace557..808fb9a7 100644 --- a/src/c3nav/site/templates/site/fragment_location.html +++ b/src/c3nav/site/templates/site/fragment_location.html @@ -37,6 +37,7 @@ {% endif %}