team-3/src/c3nav/mapdata/api.py

166 lines
5.8 KiB
Python
Raw Normal View History

2017-05-05 16:37:03 +02:00
import hashlib
2016-12-27 19:37:50 +01:00
import json
import mimetypes
from collections import OrderedDict
2017-05-05 16:37:03 +02:00
2016-12-27 19:37:50 +01:00
from django.http import Http404, HttpResponse, HttpResponseNotModified
2017-01-13 21:52:44 +01:00
from rest_framework.decorators import detail_route, list_route
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet
from c3nav.access.apply import filter_arealocations_by_access, filter_queryset_by_access
2016-12-27 19:37:50 +01:00
from c3nav.mapdata.lastupdate import get_last_mapdata_update
2017-05-10 21:31:54 +02:00
from c3nav.mapdata.models import LocationGroup, Source
2017-05-09 09:36:08 +02:00
from c3nav.mapdata.models.geometry.base import GEOMETRY_MODELS
2017-05-07 12:06:13 +02:00
from c3nav.mapdata.models.geometry.space import Stair
from c3nav.mapdata.models.section import Section
2016-12-21 20:56:28 +01:00
from c3nav.mapdata.search import get_location
2017-05-07 12:06:13 +02:00
from c3nav.mapdata.serializers.main import SectionSerializer, SourceSerializer
2017-05-05 16:37:03 +02:00
from c3nav.mapdata.utils.cache import CachedReadOnlyViewSetMixin, cache_mapdata_api_response, get_bssid_areas_cached
class GeometryTypeViewSet(ViewSet):
"""
Lists all geometry types.
"""
2016-12-07 16:11:33 +01:00
@cache_mapdata_api_response()
def list(self, request):
return Response([
OrderedDict((
('name', name),
('title', str(mapitemtype._meta.verbose_name)),
('title_plural', str(mapitemtype._meta.verbose_name_plural)),
2017-05-09 09:36:08 +02:00
)) for name, mapitemtype in GEOMETRY_MODELS.items()
])
2016-11-27 14:03:39 +01:00
class GeometryViewSet(ViewSet):
"""
2016-11-27 14:03:39 +01:00
List all geometries.
"""
def list(self, request):
2016-12-07 16:11:33 +01:00
types = set(request.GET.getlist('type'))
2017-05-09 09:36:08 +02:00
valid_types = list(GEOMETRY_MODELS.keys())
2016-11-27 14:03:39 +01:00
if not types:
types = valid_types
else:
2016-12-07 16:11:33 +01:00
types = [t for t in valid_types if t in types]
cache_key = '__'.join((
','.join([str(i) for i in types]),
))
2017-05-07 12:06:13 +02:00
return self._list(request, types=types, add_cache_key=cache_key)
2016-12-07 16:11:33 +01:00
2016-12-19 16:54:11 +01:00
@staticmethod
2017-05-10 21:31:54 +02:00
def compare_by_location_type(x, y):
2016-12-19 16:54:11 +01:00
return AreaLocation.LOCATION_TYPES.index(x.location_type) - AreaLocation.LOCATION_TYPES.index(y.location_type)
2016-12-07 16:11:33 +01:00
@cache_mapdata_api_response()
2017-05-07 12:06:13 +02:00
def _list(self, request, types):
2016-11-27 14:03:39 +01:00
results = []
for t in types:
2017-05-09 09:36:08 +02:00
mapitemtype = GEOMETRY_MODELS[t]
2016-11-27 14:03:39 +01:00
queryset = mapitemtype.objects.all()
queryset = filter_queryset_by_access(request, queryset)
queryset = queryset.order_by('id')
2016-12-06 23:43:57 +01:00
2016-12-19 16:54:11 +01:00
if issubclass(mapitemtype, AreaLocation):
queryset = sorted(queryset, key=AreaLocation.get_sort_key)
if issubclass(mapitemtype, Stair):
2016-12-08 12:36:09 +01:00
results.extend(obj.to_shadow_geojson() for obj in queryset)
results.extend(obj.to_geojson() for obj in queryset)
2016-12-06 23:43:57 +01:00
2016-11-27 14:03:39 +01:00
return Response(results)
2017-05-07 12:06:13 +02:00
class SectionViewSet(CachedReadOnlyViewSetMixin, ReadOnlyModelViewSet):
"""
2017-05-07 12:06:13 +02:00
List and retrieve sections.
"""
2017-05-07 12:06:13 +02:00
queryset = Section.objects.all()
serializer_class = SectionSerializer
lookup_field = 'id'
2016-12-07 16:11:33 +01:00
class SourceViewSet(CachedReadOnlyViewSetMixin, ReadOnlyModelViewSet):
"""
List and retrieve source images (to use as a drafts).
"""
queryset = Source.objects.all()
serializer_class = SourceSerializer
lookup_field = 'id'
def get_queryset(self):
return filter_queryset_by_access(self.request, super().get_queryset().all())
@detail_route(methods=['get'])
def image(self, request, name=None):
2016-12-07 16:11:33 +01:00
return self._image(request, name=name, add_cache_key=self._get_add_cache_key(request))
@cache_mapdata_api_response()
def _image(self, request, name=None):
source = self.get_object()
response = HttpResponse(content_type=mimetypes.guess_type(source.name)[0])
response.write(source.image)
return response
2016-12-15 13:18:46 +01:00
class LocationViewSet(ViewSet):
2016-12-15 13:18:46 +01:00
"""
List and retrieve locations
"""
# We don't cache this, because it depends on access_list
lookup_field = 'location_id'
2016-12-15 13:18:46 +01:00
2016-12-24 21:51:30 +01:00
@staticmethod
def _filter(queryset):
return queryset.filter(can_search=True).order_by('id')
2016-12-24 21:51:30 +01:00
2016-12-15 13:18:46 +01:00
def list(self, request, **kwargs):
2016-12-27 19:37:50 +01:00
etag = hashlib.sha256(json.dumps({
'full_access': request.c3nav_full_access,
'access_list': request.c3nav_access_list,
'last_update': get_last_mapdata_update().isoformat()
2016-12-27 19:42:46 +01:00
}).encode()).hexdigest()
2016-12-27 19:37:50 +01:00
if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
if if_none_match:
if if_none_match == etag:
return HttpResponseNotModified()
2016-12-15 13:18:46 +01:00
locations = []
2016-12-24 21:51:30 +01:00
locations += list(filter_queryset_by_access(request, self._filter(LocationGroup.objects.all())))
locations += sorted(filter_arealocations_by_access(request, self._filter(AreaLocation.objects.all())),
key=AreaLocation.get_sort_key, reverse=True)
2016-12-27 19:37:50 +01:00
response = Response([location.to_location_json() for location in locations])
response['ETag'] = etag
response['Cache-Control'] = 'no-cache'
2016-12-27 19:42:46 +01:00
return response
2016-12-15 13:18:46 +01:00
def retrieve(self, request, location_id=None, **kwargs):
location = get_location(request, location_id)
2016-12-15 13:18:46 +01:00
if location is None:
raise Http404
2016-12-28 01:23:52 +01:00
return Response(location.to_location_json())
2016-12-27 23:39:14 +01:00
@list_route(methods=['POST'])
def wifilocate(self, request):
stations = json.loads(request.POST['stations'])[:200]
if not stations:
2016-12-28 13:46:04 +01:00
return Response({'location': None})
2016-12-27 23:39:14 +01:00
bssids = get_bssid_areas_cached()
stations = sorted(stations, key=lambda l: l['level'])
for station in stations:
2016-12-28 11:28:21 +01:00
area_name = bssids.get(station['bssid'].lower())
2016-12-27 23:39:14 +01:00
if area_name is not None:
location = get_location(request, area_name)
if location is not None:
2017-01-13 21:52:44 +01:00
return Response({'location': location.to_location_json()})
2016-12-27 23:39:14 +01:00
return Response({'location': None})