2016-11-14 21:15:20 +01:00
|
|
|
|
import mimetypes
|
2017-12-19 20:20:29 +01:00
|
|
|
|
import os
|
2017-11-01 12:26:13 +01:00
|
|
|
|
from collections import namedtuple
|
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-11-02 13:35:58 +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
|
2017-12-19 20:28:15 +01:00
|
|
|
|
from django.utils.http import http_date, quote_etag, urlsafe_base64_encode
|
2017-05-11 19:36:49 +02:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-11-02 13:35:58 +01: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
|
2017-12-20 22:53:57 +01:00
|
|
|
|
from c3nav.mapdata.models.access import AccessPermission, AccessRestrictionGroup
|
2017-06-22 19:27:51 +02:00
|
|
|
|
from c3nav.mapdata.models.geometry.level import LevelGeometryMixin
|
2017-12-20 22:53:57 +01:00
|
|
|
|
from c3nav.mapdata.models.geometry.space import (POI, Area, Column, CrossDescription, LeaveDescription, LineObstacle,
|
|
|
|
|
Obstacle, Ramp, 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-11-28 20:15:31 +01:00
|
|
|
|
from c3nav.mapdata.utils.locations import (get_location_by_id_for_request, get_location_by_slug_for_request,
|
2017-11-28 21:24:08 +01:00
|
|
|
|
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-12-04 16:11:42 +01:00
|
|
|
|
from c3nav.mapdata.utils.user import get_user_data
|
2017-12-04 17:24:01 +01:00
|
|
|
|
from c3nav.mapdata.views import set_tile_access_cookie
|
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-12-20 22:53:57 +01:00
|
|
|
|
if issubclass(qs.model, AccessRestriction):
|
|
|
|
|
qs = qs.prefetch_related('groups')
|
2017-06-11 15:10:36 +02:00
|
|
|
|
return qs
|
2016-11-14 21:15:20 +01:00
|
|
|
|
|
|
|
|
|
|
2017-11-29 12:07:27 +01:00
|
|
|
|
def api_etag(permissions=True, etag_func=AccessPermission.etag_func, cache_parameters=None):
|
2017-10-27 16:40:15 +02:00
|
|
|
|
def wrapper(func):
|
|
|
|
|
@wraps(func)
|
|
|
|
|
def wrapped_func(self, request, *args, **kwargs):
|
2017-11-29 12:29:37 +01:00
|
|
|
|
response_format = self.perform_content_negotiation(request)[0].format
|
|
|
|
|
etag_user = (':'+str(request.user.pk or 0)) if response_format == 'api' else ''
|
|
|
|
|
raw_etag = '%s%s:%s:%s' % (response_format, etag_user, get_language(),
|
|
|
|
|
(etag_func(request) if permissions else MapUpdate.current_cache_key()))
|
2017-11-29 12:07:27 +01:00
|
|
|
|
etag = quote_etag(raw_etag)
|
2017-10-27 16:40:15 +02:00
|
|
|
|
|
|
|
|
|
response = get_conditional_response(request, etag=etag)
|
|
|
|
|
if response is None:
|
2017-11-29 12:07:27 +01:00
|
|
|
|
cache_key = 'mapdata:api:'+request.path_info[5:].replace('/', '-').strip('-')+':'+raw_etag
|
|
|
|
|
if cache_parameters is not None:
|
|
|
|
|
for param, type_ in cache_parameters.items():
|
|
|
|
|
value = int(param in request.GET) if type_ == bool else type_(request.GET.get(param))
|
|
|
|
|
cache_key += ':'+urlsafe_base64_encode(str(value).encode()).decode()
|
|
|
|
|
data = cache.get(cache_key)
|
|
|
|
|
if data is not None:
|
|
|
|
|
response = Response(data)
|
|
|
|
|
|
|
|
|
|
if response is None:
|
|
|
|
|
response = func(self, request, *args, **kwargs)
|
|
|
|
|
if cache_parameters is not None and response.status_code == 200:
|
|
|
|
|
cache.set(cache_key, response.data, 300)
|
2017-10-27 16:40:15 +02:00
|
|
|
|
|
|
|
|
|
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-29 12:07:27 +01:00
|
|
|
|
@api_etag(permissions=False, cache_parameters={})
|
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-12-07 19:29:46 +01:00
|
|
|
|
order_by = ('id', )
|
|
|
|
|
|
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 12:26:13 +01:00
|
|
|
|
qs_filter = namedtuple('qs_filter', ('field', 'model', 'key', 'value'))
|
|
|
|
|
|
|
|
|
|
def _get_keys_for_model(self, request, model, key):
|
|
|
|
|
if hasattr(model, 'qs_for_request'):
|
2017-11-29 12:07:27 +01:00
|
|
|
|
cache_key = 'mapdata:api:keys:%s:%s:%s' % (model.__name__, key,
|
|
|
|
|
AccessPermission.cache_key_for_request(request))
|
2017-11-01 12:26:13 +01:00
|
|
|
|
qs = model.qs_for_request(request)
|
|
|
|
|
else:
|
2017-11-29 12:07:27 +01:00
|
|
|
|
cache_key = 'mapdata:api:keys:%s:%s:%s' % (model.__name__, key,
|
|
|
|
|
MapUpdate.current_cache_key())
|
2017-11-01 12:26:13 +01:00
|
|
|
|
qs = model.objects.all()
|
|
|
|
|
|
|
|
|
|
result = cache.get(cache_key, None)
|
|
|
|
|
if result is not None:
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
result = set(qs.values_list(key, flat=True))
|
|
|
|
|
cache.set(cache_key, result, 300)
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def _get_list(self, request):
|
2017-06-11 15:10:36 +02:00
|
|
|
|
qs = optimize_query(self.get_queryset())
|
2017-11-01 12:26:13 +01:00
|
|
|
|
filters = []
|
2017-06-22 19:27:51 +02:00
|
|
|
|
if issubclass(qs.model, LevelGeometryMixin) and 'level' in request.GET:
|
2017-11-01 12:26:13 +01:00
|
|
|
|
filters.append(self.qs_filter(field='level', model=Level, key='pk', value=request.GET['level']))
|
|
|
|
|
|
2017-06-22 19:27:51 +02:00
|
|
|
|
if issubclass(qs.model, SpaceGeometryMixin) and 'space' in request.GET:
|
2017-11-01 12:26:13 +01:00
|
|
|
|
filters.append(self.qs_filter(field='space', model=Space, key='pk', value=request.GET['space']))
|
|
|
|
|
|
2017-07-10 16:36:52 +02:00
|
|
|
|
if issubclass(qs.model, LocationGroup) and 'category' in request.GET:
|
2017-11-01 12:26:13 +01:00
|
|
|
|
filters.append(self.qs_filter(field='category', model=LocationGroupCategory,
|
|
|
|
|
key='pk' if request.GET['category'].isdigit() else 'name',
|
|
|
|
|
value=request.GET['category']))
|
|
|
|
|
|
2017-07-10 16:43:44 +02:00
|
|
|
|
if issubclass(qs.model, SpecificLocation) and 'group' in request.GET:
|
2017-11-01 12:26:13 +01:00
|
|
|
|
filters.append(self.qs_filter(field='groups', model=LocationGroup, key='pk', value=request.GET['group']))
|
|
|
|
|
|
2017-06-11 14:43:14 +02:00
|
|
|
|
if qs.model == Level and 'on_top_of' in request.GET:
|
2017-11-01 12:26:13 +01:00
|
|
|
|
value = None if request.GET['on_top_of'] == 'null' else request.GET['on_top_of']
|
|
|
|
|
filters.append(self.qs_filter(field='on_top_of', model=Level, key='pk', value=value))
|
|
|
|
|
|
|
|
|
|
cache_key = 'mapdata:api:%s:%s' % (qs.model.__name__, AccessPermission.cache_key_for_request(request))
|
|
|
|
|
for qs_filter in filters:
|
|
|
|
|
cache_key += ';%s,%s' % (qs_filter.field, qs_filter.value)
|
|
|
|
|
|
|
|
|
|
results = cache.get(cache_key, None)
|
|
|
|
|
if results is not None:
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
for qs_filter in filters:
|
|
|
|
|
if qs_filter.key == 'pk' and not qs_filter.value.isdigit():
|
|
|
|
|
raise ValidationError(detail={
|
|
|
|
|
'detail': _('%(field)s is not an integer.') % {'field': qs_filter.field}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for qs_filter in filters:
|
|
|
|
|
if qs_filter.value is not None:
|
|
|
|
|
keys = self._get_keys_for_model(request, qs_filter.model, qs_filter.key)
|
|
|
|
|
value = int(qs_filter.value) if qs_filter.key == 'pk' else qs_filter.value
|
|
|
|
|
if value not in keys:
|
|
|
|
|
raise NotFound(detail=_('%(model)s not found.') % {'model': qs_filter.model._meta.verbose_name})
|
|
|
|
|
|
2017-12-07 19:29:46 +01:00
|
|
|
|
results = tuple(qs.order_by(*self.order_by))
|
2017-11-01 12:26:13 +01:00
|
|
|
|
cache.set(cache_key, results, 300)
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
@api_etag()
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
|
geometry = ('geometry' in request.GET)
|
|
|
|
|
results = self._get_list(request)
|
|
|
|
|
|
|
|
|
|
return Response([obj.serialize(geometry=geometry) for obj in results])
|
2017-05-11 19:36:49 +02:00
|
|
|
|
|
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-11-29 11:27:50 +01:00
|
|
|
|
"""
|
|
|
|
|
Add ?on_top_of=<null or id> to filter by on_top_of, add ?group=<id> to filter by group.
|
|
|
|
|
A Level is a Location – so if it is visible, you can use its ID in the Location API as well.
|
|
|
|
|
"""
|
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-29 12:07:27 +01:00
|
|
|
|
@api_etag(permissions=False, cache_parameters={})
|
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
|
|
|
|
|
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-11-29 11:27:50 +01:00
|
|
|
|
"""
|
|
|
|
|
Add ?geometry=1 to get geometries, add ?level=<id> to filter by level, add ?group=<id> to filter by group.
|
|
|
|
|
A Space is a Location – so if it is visible, you can use its ID in the Location API as well.
|
|
|
|
|
"""
|
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-29 12:07:27 +01:00
|
|
|
|
@api_etag(permissions=False, cache_parameters={})
|
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-11-29 11:27:50 +01:00
|
|
|
|
"""
|
|
|
|
|
Add ?geometry=1 to get geometries, add ?space=<id> to filter by space, add ?group=<id> to filter by group.
|
|
|
|
|
An Area is a Location – so if it is visible, you can use its ID in the Location API as well.
|
|
|
|
|
"""
|
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()
|
2017-11-17 20:31:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RampViewSet(MapdataViewSet):
|
|
|
|
|
""" Add ?geometry=1 to get geometries, add ?space=<id> to filter by space. """
|
|
|
|
|
queryset = Ramp.objects.all()
|
2017-05-11 19:36:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-11-29 11:27:50 +01:00
|
|
|
|
"""
|
|
|
|
|
Add ?geometry=1 to get geometries, add ?space=<id> to filter by space, add ?group=<id> to filter by group.
|
|
|
|
|
A POI is a Location – so if it is visible, you can use its ID in the Location API as well.
|
|
|
|
|
"""
|
2017-07-08 16:29:12 +02:00
|
|
|
|
queryset = POI.objects.all()
|
2017-05-11 19:36:49 +02:00
|
|
|
|
|
|
|
|
|
|
2017-12-20 22:53:57 +01:00
|
|
|
|
class LeaveDescriptionViewSet(MapdataViewSet):
|
|
|
|
|
queryset = LeaveDescription.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CrossDescriptionViewSet(MapdataViewSet):
|
|
|
|
|
queryset = CrossDescription.objects.all()
|
|
|
|
|
|
|
|
|
|
|
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-11-29 11:27:50 +01:00
|
|
|
|
"""
|
|
|
|
|
Add ?category=<id or name> to filter by category.
|
|
|
|
|
A Location Group is a Location – so if it is visible, you can use its ID in the Location API as well.
|
|
|
|
|
"""
|
2017-05-11 19:36:49 +02:00
|
|
|
|
queryset = LocationGroup.objects.all()
|
|
|
|
|
|
|
|
|
|
|
2017-11-30 17:54:40 +01:00
|
|
|
|
class LocationViewSetBase(RetrieveModelMixin, GenericViewSet):
|
|
|
|
|
queryset = LocationSlug.objects.all()
|
|
|
|
|
|
|
|
|
|
def get_object(self) -> LocationSlug:
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@api_etag(cache_parameters={'show_redirects': bool, 'detailed': bool, 'geometry': bool})
|
|
|
|
|
def retrieve(self, request, key=None, *args, **kwargs):
|
|
|
|
|
show_redirects = 'show_redirects' in request.GET
|
|
|
|
|
detailed = 'detailed' in request.GET
|
|
|
|
|
geometry = 'geometry' in request.GET
|
|
|
|
|
|
|
|
|
|
location = self.get_object()
|
|
|
|
|
|
|
|
|
|
if location is None:
|
|
|
|
|
raise NotFound
|
|
|
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
|
|
|
|
if not show_redirects:
|
|
|
|
|
return redirect('../' + str(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))
|
|
|
|
|
|
|
|
|
|
@detail_route(methods=['get'])
|
|
|
|
|
@api_etag()
|
2017-12-14 22:17:51 +01:00
|
|
|
|
def details(self, request, **kwargs):
|
2017-11-30 17:54:40 +01:00
|
|
|
|
location = self.get_object()
|
|
|
|
|
|
|
|
|
|
if location is None:
|
|
|
|
|
raise NotFound
|
|
|
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
2017-12-14 22:17:51 +01:00
|
|
|
|
return redirect('../' + str(location.target.pk) + '/details/')
|
2017-11-30 17:54:40 +01:00
|
|
|
|
|
|
|
|
|
return Response(location.details_display())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocationViewSet(LocationViewSetBase):
|
2017-05-12 12:55:23 +02:00
|
|
|
|
"""
|
2017-11-29 11:27:50 +01:00
|
|
|
|
Locations are Levels, Spaces, Areas, POIs and Location Groups (see /locations/types/). They have a shared ID pool.
|
|
|
|
|
This API endpoint only accesses locations that have can_search or can_describe set to true.
|
|
|
|
|
If you want to access all of them, use the API endpoints for the Location Types.
|
|
|
|
|
Additionally, you can access Custom Locations (Coordinates) by using c:<level.short_label>:x:y as an id or slug.
|
|
|
|
|
|
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()
|
2017-11-28 20:15:31 +01:00
|
|
|
|
lookup_value_regex = r'[^/]+'
|
2017-05-11 19:36:49 +02:00
|
|
|
|
|
2017-11-30 17:54:40 +01:00
|
|
|
|
def get_object(self):
|
|
|
|
|
return get_location_by_id_for_request(self.kwargs['pk'], self.request)
|
|
|
|
|
|
2017-11-29 12:07:27 +01:00
|
|
|
|
@api_etag(cache_parameters={'searchable': bool, 'detailed': bool, 'geometry': bool})
|
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-27 16:06:55 +01:00
|
|
|
|
@list_route(methods=['get'])
|
|
|
|
|
@api_etag(permissions=False)
|
|
|
|
|
def types(self, request):
|
|
|
|
|
return MapdataViewSet.list_types(get_submodels(Location), geomtype=False)
|
|
|
|
|
|
|
|
|
|
|
2017-11-30 17:54:40 +01:00
|
|
|
|
class LocationBySlugViewSet(LocationViewSetBase):
|
2017-11-27 16:06:55 +01:00
|
|
|
|
queryset = LocationSlug.objects.all()
|
|
|
|
|
lookup_field = 'slug'
|
2017-11-28 20:15:31 +01:00
|
|
|
|
lookup_value_regex = r'[^/]+'
|
2017-11-27 16:06:55 +01:00
|
|
|
|
|
2017-11-30 17:54:40 +01:00
|
|
|
|
def get_object(self):
|
|
|
|
|
return get_location_by_slug_for_request(self.kwargs['slug'], self.request)
|
2017-11-01 15:55:59 +01: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()
|
2017-12-07 19:29:46 +01:00
|
|
|
|
order_by = ('name',)
|
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()
|
2017-12-19 20:41:32 +01:00
|
|
|
|
last_modified = int(os.path.getmtime(source.filepath))
|
2017-12-19 20:20:29 +01:00
|
|
|
|
response = get_conditional_response(request, last_modified=last_modified)
|
|
|
|
|
if response is None:
|
|
|
|
|
response = HttpResponse(open(source.filepath, 'rb'), content_type=mimetypes.guess_type(source.name)[0])
|
2017-12-19 20:28:15 +01:00
|
|
|
|
response['Last-Modified'] = http_date(last_modified)
|
2017-12-19 20:20:29 +01:00
|
|
|
|
return response
|
2017-07-13 19:01:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccessRestrictionViewSet(MapdataViewSet):
|
|
|
|
|
queryset = AccessRestriction.objects.all()
|
2017-12-04 16:11:42 +01:00
|
|
|
|
|
|
|
|
|
|
2017-12-20 22:53:57 +01:00
|
|
|
|
class AccessRestrictionGroupViewSet(MapdataViewSet):
|
|
|
|
|
queryset = AccessRestrictionGroup.objects.all()
|
|
|
|
|
|
|
|
|
|
|
2017-12-04 17:24:01 +01:00
|
|
|
|
class UserViewSet(GenericViewSet):
|
|
|
|
|
"""
|
|
|
|
|
Get display information about the current user. This endpoint also sets the tile access cookie.
|
|
|
|
|
The tile access cookie is only valid for 1 minute, so if you are displaying a map, call this endpoint repeatedly.
|
|
|
|
|
"""
|
2017-12-04 16:11:42 +01:00
|
|
|
|
@list_route(methods=['get'])
|
|
|
|
|
def current(self, request, key=None):
|
2017-12-20 23:30:57 +01:00
|
|
|
|
try:
|
|
|
|
|
cache.incr('api_user_current_requests')
|
|
|
|
|
except ValueError:
|
|
|
|
|
cache.set('api_user_current_requests', 0, None)
|
|
|
|
|
|
2017-12-04 17:24:01 +01:00
|
|
|
|
response = Response(get_user_data(request))
|
|
|
|
|
set_tile_access_cookie(request, response)
|
|
|
|
|
return response
|