2016-11-14 21:15:20 +01:00
|
|
|
import mimetypes
|
2017-05-04 12:28:17 +02:00
|
|
|
from collections import OrderedDict
|
2017-05-11 19:36:49 +02:00
|
|
|
from itertools import chain
|
2017-05-05 16:37:03 +02:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
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
|
2016-11-14 21:15:20 +01:00
|
|
|
from rest_framework.response import Response
|
2017-05-11 19:36:49 +02:00
|
|
|
from rest_framework.viewsets import ReadOnlyModelViewSet
|
2016-11-14 21:15:20 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
from c3nav.access.apply import filter_queryset_by_access
|
|
|
|
from c3nav.mapdata.models import Building, Door, Hole, LocationGroup, Source, Space
|
|
|
|
from c3nav.mapdata.models.geometry.section import SECTION_MODELS
|
|
|
|
from c3nav.mapdata.models.geometry.space import SPACE_MODELS, Area, LineObstacle, Obstacle, Point, Stair
|
|
|
|
from c3nav.mapdata.models.locations import LocationSlug
|
2017-05-07 12:06:13 +02:00
|
|
|
from c3nav.mapdata.models.section import Section
|
2017-05-11 19:36:49 +02:00
|
|
|
from c3nav.mapdata.serializers.main import SourceSerializer
|
|
|
|
from c3nav.mapdata.utils.cache import CachedReadOnlyViewSetMixin
|
2016-11-14 21:15:20 +01:00
|
|
|
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class MapdataViewSet(ReadOnlyModelViewSet):
|
2016-12-06 20:47:17 +01:00
|
|
|
def list(self, request):
|
2017-05-11 19:36:49 +02:00
|
|
|
qs = self.get_queryset()
|
|
|
|
geometry = ('geometry' in request.GET)
|
|
|
|
if qs.model in SECTION_MODELS and 'section' in request.GET:
|
|
|
|
if not request.GET['section'].isdigit():
|
|
|
|
raise ValidationError(detail={'detail': _('%s is not an integer.') % 'section'})
|
|
|
|
try:
|
|
|
|
section = Section.objects.get(pk=request.GET['section'])
|
|
|
|
except Section.DoesNotExist:
|
|
|
|
raise NotFound(detail=_('section not found.'))
|
|
|
|
qs = qs.filter(section=section)
|
|
|
|
if qs.model in SPACE_MODELS and 'space' in request.GET:
|
|
|
|
if not request.GET['space'].isdigit():
|
|
|
|
raise ValidationError(detail={'detail': _('%s is not an integer.') % 'space'})
|
|
|
|
try:
|
|
|
|
space = Space.objects.get(pk=request.GET['space'])
|
|
|
|
except Space.DoesNotExist:
|
|
|
|
raise NotFound(detail=_('section not found.'))
|
|
|
|
qs = qs.filter(space=space)
|
|
|
|
return Response([obj.serialize(geometry=geometry) for obj in qs.order_by('id')])
|
|
|
|
|
|
|
|
def retrieve(self, request, pk=None):
|
|
|
|
return Response(self.get_object().serialize())
|
|
|
|
|
|
|
|
def list_geometrytypes(self, models_list):
|
2016-12-06 20:47:17 +01:00
|
|
|
return Response([
|
|
|
|
OrderedDict((
|
2017-05-11 19:36:49 +02:00
|
|
|
('name', model.__name__.lower()),
|
|
|
|
('name_plural', model._meta.default_related_name),
|
|
|
|
('geomtype', model._meta.get_field('geometry').geomtype),
|
|
|
|
('title', str(model._meta.verbose_name)),
|
|
|
|
('title_plural', str(model._meta.verbose_name_plural)),
|
|
|
|
)) for model in models_list
|
2016-12-06 20:47:17 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class SectionViewSet(MapdataViewSet):
|
|
|
|
queryset = Section.objects.all()
|
2016-12-07 16:11:33 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
@list_route(methods=['get'])
|
|
|
|
def geometrytypes(self, request):
|
|
|
|
return self.list_geometrytypes(SECTION_MODELS)
|
2016-12-07 16:11:33 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
@detail_route(methods=['get'])
|
|
|
|
def geometries(self, requests, pk=None):
|
|
|
|
section = self.get_object()
|
|
|
|
results = []
|
|
|
|
results.extend(section.buildings.all())
|
|
|
|
results.extend(section.holes.all())
|
|
|
|
for space in section.spaces.all():
|
|
|
|
results.append(space)
|
|
|
|
for door in section.doors.all():
|
|
|
|
results.append(door)
|
|
|
|
return Response([obj.to_geojson() for obj in results])
|
2016-12-07 16:11:33 +01:00
|
|
|
|
2016-12-19 16:54:11 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class BuildingViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?section=<id> to filter by section. """
|
|
|
|
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):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?section=<id> to filter by section. """
|
|
|
|
queryset = Space.objects.all()
|
2016-12-08 12:36:09 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
@list_route(methods=['get'])
|
|
|
|
def geometrytypes(self, request):
|
|
|
|
return self.list_geometrytypes(SPACE_MODELS)
|
2016-12-06 23:43:57 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
@detail_route(methods=['get'])
|
|
|
|
def geometries(self, requests, pk=None):
|
|
|
|
space = self.get_object()
|
|
|
|
results = chain(
|
|
|
|
space.stairs.all(),
|
|
|
|
space.areas.all(),
|
|
|
|
space.obstacles.all(),
|
|
|
|
space.lineobstacles.all(),
|
|
|
|
space.points.all(),
|
|
|
|
)
|
|
|
|
return Response([obj.to_geojson() for obj in results])
|
2016-11-14 21:15:20 +01:00
|
|
|
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class DoorViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?section=<id> to filter by section. """
|
|
|
|
queryset = Door.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class HoleViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?section=<id> to filter by section. """
|
|
|
|
queryset = Hole.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class AreaViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
class PointViewSet(MapdataViewSet):
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
queryset = Point.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class LocationGroupViewSet(MapdataViewSet):
|
|
|
|
queryset = LocationGroup.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class LocationViewSet(MapdataViewSet):
|
|
|
|
queryset = LocationSlug.objects.all()
|
|
|
|
lookup_field = 'slug'
|
|
|
|
|
|
|
|
def retrieve(self, request, slug=None):
|
|
|
|
return Response(self.get_object().get_child().serialize(include_type=True))
|
2016-11-14 21:15:20 +01:00
|
|
|
|
|
|
|
|
2016-12-07 16:11:33 +01:00
|
|
|
class SourceViewSet(CachedReadOnlyViewSetMixin, ReadOnlyModelViewSet):
|
2016-11-14 21:15:20 +01:00
|
|
|
queryset = Source.objects.all()
|
|
|
|
serializer_class = SourceSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2016-12-22 11:50:12 +01:00
|
|
|
return filter_queryset_by_access(self.request, super().get_queryset().all())
|
2016-11-14 21:15:20 +01:00
|
|
|
|
|
|
|
@detail_route(methods=['get'])
|
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()
|
|
|
|
response = HttpResponse(content_type=mimetypes.guess_type(source.name)[0])
|
2017-05-01 16:50:36 +02:00
|
|
|
response.write(source.image)
|
2016-11-14 21:15:20 +01:00
|
|
|
return response
|