2016-11-14 21:15:20 +01:00
|
|
|
import mimetypes
|
2017-10-31 13:25:42 +01:00
|
|
|
from functools import wraps
|
2017-05-05 16:37:03 +02:00
|
|
|
|
2017-10-27 14:51:09 +02:00
|
|
|
from django.core.cache import cache
|
2017-10-31 13:25:42 +01:00
|
|
|
from django.db.models import Prefetch
|
2017-05-11 19:36:49 +02:00
|
|
|
from django.http import HttpResponse
|
2017-05-11 22:40:48 +02:00
|
|
|
from django.shortcuts import redirect
|
2017-10-27 16:40:15 +02:00
|
|
|
from django.utils.cache import get_conditional_response
|
|
|
|
from django.utils.http import quote_etag
|
2017-05-11 19:36:49 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-10-28 21:47:53 +02:00
|
|
|
from django.utils.translation import get_language
|
2017-01-13 21:52:44 +01:00
|
|
|
from rest_framework.decorators import detail_route, list_route
|
2017-05-11 19:36:49 +02:00
|
|
|
from rest_framework.exceptions import NotFound, ValidationError
|
2017-05-11 21:30:29 +02:00
|
|
|
from rest_framework.mixins import RetrieveModelMixin
|
2016-11-14 21:15:20 +01:00
|
|
|
from rest_framework.response import Response
|
2017-10-10 17:55:55 +02:00
|
|
|
from rest_framework.viewsets import GenericViewSet, ReadOnlyModelViewSet, ViewSet
|
2016-11-14 21:15:20 +01:00
|
|
|
|
2017-10-27 14:51:09 +02:00
|
|
|
from c3nav.mapdata.models import AccessRestriction, Building, Door, Hole, LocationGroup, MapUpdate, Source, Space
|
|
|
|
from c3nav.mapdata.models.access import AccessPermission
|
2017-06-22 19:27:51 +02:00
|
|
|
from c3nav.mapdata.models.geometry.level import LevelGeometryMixin
|
2017-07-08 16:29:12 +02:00
|
|
|
from c3nav.mapdata.models.geometry.space import POI, Area, Column, LineObstacle, Obstacle, SpaceGeometryMixin, Stair
|
2017-06-11 14:43:14 +02:00
|
|
|
from c3nav.mapdata.models.level import Level
|
2017-07-10 13:54:33 +02:00
|
|
|
from c3nav.mapdata.models.locations import (Location, LocationGroupCategory, LocationRedirect, LocationSlug,
|
|
|
|
SpecificLocation)
|
2017-10-31 15:22:12 +01:00
|
|
|
from c3nav.mapdata.utils.locations import (get_location_by_slug_for_request, searchable_locations_for_request,
|
|
|
|
visible_locations_for_request)
|
2017-06-22 19:27:51 +02:00
|
|
|
from c3nav.mapdata.utils.models import get_submodels
|
2017-06-11 15:10:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def optimize_query(qs):
|
|
|
|
if issubclass(qs.model, SpecificLocation):
|
2017-11-01 12:05:37 +01:00
|
|
|
base_qs = LocationGroup.objects.select_related('category')
|
2017-07-10 16:30:38 +02:00
|
|
|
qs = qs.prefetch_related(Prefetch('groups', queryset=base_qs))
|
2017-06-11 15:10:36 +02:00
|
|
|
return qs
|
2016-11-14 21:15:20 +01:00
|
|
|
|
|
|
|
|
2017-11-01 11:12:40 +01:00
|
|
|
def api_etag(permissions=True, etag_func=AccessPermission.etag_func):
|
2017-10-27 16:40:15 +02:00
|
|
|
def wrapper(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapped_func(self, request, *args, **kwargs):
|
2017-10-28 21:47:53 +02:00
|
|
|
etag = quote_etag(get_language()+':'+(etag_func(request) if permissions else MapUpdate.current_cache_key()))
|
2017-10-27 16:40:15 +02:00
|
|
|
|
|
|
|
response = get_conditional_response(request, etag=etag)
|
|
|
|
if response is None:
|
|
|
|
response = func(self, request, *args, **kwargs)
|
|
|
|
|
|
|
|
response['ETag'] = etag
|
|
|
|
response['Cache-Control'] = 'no-cache'
|
|
|
|
return response
|
|
|
|
return wrapped_func
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2017-10-10 17:55:55 +02:00
|
|
|
class MapViewSet(ViewSet):
|
|
|
|
"""
|
|
|
|
Map API
|
|
|
|
/bounds/ returns the maximum bounds of the map
|
|
|
|
"""
|
|
|
|
|
|
|
|
@list_route(methods=['get'])
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag(permissions=False)
|
2017-10-10 17:55:55 +02:00
|
|
|
def bounds(self, request, *args, **kwargs):
|
|
|
|
return Response({
|
|
|
|
'bounds': Source.max_bounds(),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class MapdataViewSet(ReadOnlyModelViewSet):
|
2017-07-13 19:22:57 +02:00
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
if hasattr(qs.model, 'qs_for_request'):
|
|
|
|
return qs.model.qs_for_request(self.request)
|
|
|
|
return qs
|
|
|
|
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag()
|
2017-05-11 21:30:29 +02:00
|
|
|
def list(self, request, *args, **kwargs):
|
2017-06-11 15:10:36 +02:00
|
|
|
qs = optimize_query(self.get_queryset())
|
2017-05-11 19:36:49 +02:00
|
|
|
geometry = ('geometry' in request.GET)
|
2017-06-22 19:27:51 +02:00
|
|
|
if issubclass(qs.model, LevelGeometryMixin) and 'level' in request.GET:
|
2017-06-11 14:43:14 +02:00
|
|
|
if not request.GET['level'].isdigit():
|
|
|
|
raise ValidationError(detail={'detail': _('%s is not an integer.') % 'level'})
|
2017-05-11 19:36:49 +02:00
|
|
|
try:
|
2017-07-13 19:22:57 +02:00
|
|
|
level = Level.qs_for_request(request).get(pk=request.GET['level'])
|
2017-06-11 14:43:14 +02:00
|
|
|
except Level.DoesNotExist:
|
|
|
|
raise NotFound(detail=_('level not found.'))
|
|
|
|
qs = qs.filter(level=level)
|
2017-06-22 19:27:51 +02:00
|
|
|
if issubclass(qs.model, SpaceGeometryMixin) and 'space' in request.GET:
|
2017-05-11 19:36:49 +02:00
|
|
|
if not request.GET['space'].isdigit():
|
|
|
|
raise ValidationError(detail={'detail': _('%s is not an integer.') % 'space'})
|
|
|
|
try:
|
2017-07-13 19:22:57 +02:00
|
|
|
space = Space.qs_for_request(request).get(pk=request.GET['space'])
|
2017-05-11 19:36:49 +02:00
|
|
|
except Space.DoesNotExist:
|
2017-06-10 14:48:50 +02:00
|
|
|
raise NotFound(detail=_('space not found.'))
|
2017-05-11 19:36:49 +02:00
|
|
|
qs = qs.filter(space=space)
|
2017-07-10 16:36:52 +02:00
|
|
|
if issubclass(qs.model, LocationGroup) and 'category' in request.GET:
|
|
|
|
kwargs = {('pk' if request.GET['category'].isdigit() else 'name'): request.GET['category']}
|
|
|
|
try:
|
|
|
|
category = LocationGroupCategory.objects.get(**kwargs)
|
|
|
|
except LocationGroupCategory.DoesNotExist:
|
|
|
|
raise NotFound(detail=_('category not found.'))
|
|
|
|
qs = qs.filter(category=category)
|
2017-07-10 16:43:44 +02:00
|
|
|
if issubclass(qs.model, SpecificLocation) and 'group' in request.GET:
|
|
|
|
if not request.GET['group'].isdigit():
|
|
|
|
raise ValidationError(detail={'detail': _('%s is not an integer.') % 'group'})
|
|
|
|
try:
|
|
|
|
group = LocationGroup.objects.get(pk=request.GET['group'])
|
|
|
|
except LocationGroupCategory.DoesNotExist:
|
|
|
|
raise NotFound(detail=_('group not found.'))
|
|
|
|
qs = qs.filter(groups=group)
|
2017-06-11 14:43:14 +02:00
|
|
|
if qs.model == Level and 'on_top_of' in request.GET:
|
2017-06-10 14:58:13 +02:00
|
|
|
if request.GET['on_top_of'] == 'null':
|
|
|
|
qs = qs.filter(on_top_of__isnull=False)
|
|
|
|
else:
|
|
|
|
if not request.GET['on_top_of'].isdigit():
|
|
|
|
raise ValidationError(detail={'detail': _('%s is not null or an integer.') % 'on_top_of'})
|
|
|
|
try:
|
2017-06-11 14:43:14 +02:00
|
|
|
level = Level.objects.get(pk=request.GET['on_top_of'])
|
|
|
|
except Level.DoesNotExist:
|
|
|
|
raise NotFound(detail=_('level not found.'))
|
|
|
|
qs = qs.filter(on_top_of=level)
|
2017-05-11 19:36:49 +02:00
|
|
|
return Response([obj.serialize(geometry=geometry) for obj in qs.order_by('id')])
|
|
|
|
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag()
|
2017-05-11 21:30:29 +02:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
2017-05-11 19:36:49 +02:00
|
|
|
return Response(self.get_object().serialize())
|
|
|
|
|
2017-05-11 21:30:29 +02:00
|
|
|
@staticmethod
|
|
|
|
def list_types(models_list, **kwargs):
|
2016-12-06 20:47:17 +01:00
|
|
|
return Response([
|
2017-05-11 21:30:29 +02:00
|
|
|
model.serialize_type(**kwargs) for model in models_list
|
2016-12-06 20:47:17 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
|
2017-06-11 14:43:14 +02:00
|
|
|
class LevelViewSet(MapdataViewSet):
|
2017-07-10 16:43:44 +02:00
|
|
|
""" Add ?on_top_of=<null or id> to filter by on_top_of, add ?group=<id> to filter by group. """
|
2017-06-11 14:43:14 +02:00
|
|
|
queryset = Level.objects.all()
|
2016-12-07 16:11:33 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
@list_route(methods=['get'])
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag(permissions=False)
|
2017-05-11 19:36:49 +02:00
|
|
|
def geometrytypes(self, request):
|
2017-06-22 19:27:51 +02:00
|
|
|
return self.list_types(get_submodels(LevelGeometryMixin))
|
2016-12-07 16:11:33 +01:00
|
|
|
|
2017-05-12 23:37:03 +02:00
|
|
|
@detail_route(methods=['get'])
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag()
|
2017-07-14 00:16:20 +02:00
|
|
|
def svg(self, request, pk=None):
|
2017-06-11 14:43:14 +02:00
|
|
|
level = self.get_object()
|
2017-07-14 00:16:20 +02:00
|
|
|
response = HttpResponse(level.render_svg(request), 'image/svg+xml')
|
2017-05-12 23:37:03 +02:00
|
|
|
return response
|
|
|
|
|
2016-12-19 16:54:11 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class BuildingViewSet(MapdataViewSet):
|
2017-06-11 14:43:14 +02:00
|
|
|
""" Add ?geometry=1 to get geometries, add ?level=<id> to filter by level. """
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = Building.objects.all()
|
2016-12-06 23:43:57 +01:00
|
|
|
|
2016-12-19 16:54:11 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class SpaceViewSet(MapdataViewSet):
|
2017-07-10 16:43:44 +02:00
|
|
|
""" Add ?geometry=1 to get geometries, add ?level=<id> to filter by level, add ?group=<id> to filter by group. """
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = Space.objects.all()
|
2016-12-08 12:36:09 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
@list_route(methods=['get'])
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag(permissions=False)
|
2017-05-11 19:36:49 +02:00
|
|
|
def geometrytypes(self, request):
|
2017-06-22 19:27:51 +02:00
|
|
|
return self.list_types(get_submodels(SpaceGeometryMixin))
|
2016-12-06 23:43:57 +01:00
|
|
|
|
2016-11-14 21:15:20 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class DoorViewSet(MapdataViewSet):
|
2017-06-11 14:43:14 +02:00
|
|
|
""" Add ?geometry=1 to get geometries, add ?level=<id> to filter by level. """
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = Door.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class HoleViewSet(MapdataViewSet):
|
2017-06-08 15:19:12 +02:00
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = Hole.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class AreaViewSet(MapdataViewSet):
|
2017-07-10 16:43:44 +02:00
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space, add ?group=<id> to filter by group. """
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = Area.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class StairViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
queryset = Stair.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class ObstacleViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
queryset = Obstacle.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class LineObstacleViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
queryset = LineObstacle.objects.all()
|
|
|
|
|
|
|
|
|
2017-06-09 15:22:30 +02:00
|
|
|
class ColumnViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
queryset = Column.objects.all()
|
|
|
|
|
|
|
|
|
2017-07-08 16:29:12 +02:00
|
|
|
class POIViewSet(MapdataViewSet):
|
2017-07-10 16:43:44 +02:00
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space, add ?group=<id> to filter by group. """
|
2017-07-08 16:29:12 +02:00
|
|
|
queryset = POI.objects.all()
|
2017-05-11 19:36:49 +02:00
|
|
|
|
|
|
|
|
2017-07-10 13:54:33 +02:00
|
|
|
class LocationGroupCategoryViewSet(MapdataViewSet):
|
|
|
|
queryset = LocationGroupCategory.objects.all()
|
|
|
|
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class LocationGroupViewSet(MapdataViewSet):
|
2017-07-10 16:36:52 +02:00
|
|
|
""" Add ?category=<id or name> to filter by category. """
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = LocationGroup.objects.all()
|
|
|
|
|
|
|
|
|
2017-05-11 21:30:29 +02:00
|
|
|
class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
2017-05-12 12:55:23 +02:00
|
|
|
"""
|
|
|
|
only accesses locations that have can_search or can_describe set to true.
|
2017-10-28 14:09:59 +02:00
|
|
|
add ?searchable to only show locations with can_search set to true ordered by relevance
|
2017-10-27 14:52:31 +02:00
|
|
|
add ?detailed to show all attributes
|
|
|
|
add ?geometry to show geometries
|
2017-05-12 12:55:23 +02:00
|
|
|
/{id}/ add ?show_redirect=1 to suppress redirects and show them as JSON.
|
|
|
|
"""
|
2017-05-11 19:36:49 +02:00
|
|
|
queryset = LocationSlug.objects.all()
|
|
|
|
lookup_field = 'slug'
|
|
|
|
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag()
|
2017-06-22 20:09:58 +02:00
|
|
|
def list(self, request, *args, **kwargs):
|
2017-10-28 14:09:59 +02:00
|
|
|
searchable = 'searchable' in request.GET
|
2017-06-22 20:09:58 +02:00
|
|
|
detailed = 'detailed' in request.GET
|
2017-10-27 13:47:12 +02:00
|
|
|
geometry = 'geometry' in request.GET
|
2017-07-10 17:03:44 +02:00
|
|
|
|
2017-10-27 16:40:15 +02:00
|
|
|
cache_key = 'mapdata:api:location:list:%d:%s' % (
|
2017-10-28 14:09:59 +02:00
|
|
|
searchable + detailed*2 + geometry*4,
|
2017-10-27 16:40:15 +02:00
|
|
|
AccessPermission.cache_key_for_request(self.request)
|
2017-10-27 15:25:03 +02:00
|
|
|
)
|
|
|
|
result = cache.get(cache_key, None)
|
|
|
|
if result is None:
|
2017-10-31 15:22:12 +01:00
|
|
|
if searchable:
|
|
|
|
locations = searchable_locations_for_request(self.request)
|
|
|
|
else:
|
|
|
|
locations = visible_locations_for_request(self.request).values()
|
2017-10-28 13:31:12 +02:00
|
|
|
|
2017-10-28 21:36:52 +02:00
|
|
|
result = tuple(obj.serialize(include_type=True, detailed=detailed, geometry=geometry, simple_geometry=True)
|
2017-10-31 15:22:12 +01:00
|
|
|
for obj in locations)
|
2017-10-27 15:25:03 +02:00
|
|
|
cache.set(cache_key, result, 300)
|
|
|
|
|
|
|
|
return Response(result)
|
2017-05-12 12:55:23 +02:00
|
|
|
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag()
|
2017-05-11 22:40:48 +02:00
|
|
|
def retrieve(self, request, slug=None, *args, **kwargs):
|
2017-10-31 15:22:12 +01:00
|
|
|
show_redirects = 'show_redirects' in request.GET
|
|
|
|
detailed = 'detailed' in request.GET
|
|
|
|
geometry = 'geometry' in request.GET
|
|
|
|
|
|
|
|
location = get_location_by_slug_for_request(slug, request)
|
|
|
|
|
|
|
|
if location is None:
|
2017-05-11 22:40:48 +02:00
|
|
|
raise NotFound
|
2017-10-31 15:22:12 +01:00
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
|
|
|
if not show_redirects:
|
|
|
|
return redirect('../' + location.target.slug) # todo: why does redirect/reverse not work here?
|
|
|
|
|
|
|
|
return Response(location.serialize(include_type=True, detailed=detailed,
|
|
|
|
geometry=geometry, simple_geometry=True))
|
2016-11-14 21:15:20 +01:00
|
|
|
|
2017-05-11 21:30:29 +02:00
|
|
|
@list_route(methods=['get'])
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag(permissions=False)
|
2017-05-11 21:30:29 +02:00
|
|
|
def types(self, request):
|
2017-06-22 19:27:51 +02:00
|
|
|
return MapdataViewSet.list_types(get_submodels(Location), geomtype=False)
|
2017-05-11 21:30:29 +02:00
|
|
|
|
2016-11-14 21:15:20 +01:00
|
|
|
|
2017-05-12 01:21:53 +02:00
|
|
|
class SourceViewSet(MapdataViewSet):
|
2016-11-14 21:15:20 +01:00
|
|
|
queryset = Source.objects.all()
|
|
|
|
|
|
|
|
@detail_route(methods=['get'])
|
2017-11-01 11:12:40 +01:00
|
|
|
@api_etag()
|
2017-05-11 19:36:49 +02:00
|
|
|
def image(self, request, pk=None):
|
|
|
|
return self._image(request, pk=pk)
|
2016-12-07 16:11:33 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
def _image(self, request, pk=None):
|
2016-11-14 21:15:20 +01:00
|
|
|
source = self.get_object()
|
2017-07-08 15:09:56 +02:00
|
|
|
return HttpResponse(open(source.filepath, 'rb'), content_type=mimetypes.guess_type(source.name)[0])
|
2017-07-13 19:01:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AccessRestrictionViewSet(MapdataViewSet):
|
|
|
|
queryset = AccessRestriction.objects.all()
|