2023-11-23 21:11:31 +01:00
|
|
|
import json
|
2023-12-03 23:27:20 +01:00
|
|
|
from typing import Annotated, Optional, Union
|
2023-11-23 21:11:31 +01:00
|
|
|
|
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
|
|
|
from django.shortcuts import redirect
|
2023-11-24 15:42:48 +01:00
|
|
|
from django.utils import timezone
|
2023-11-23 16:37:25 +01:00
|
|
|
from ninja import Query
|
2023-11-19 15:34:08 +01:00
|
|
|
from ninja import Router as APIRouter
|
2023-11-23 16:37:25 +01:00
|
|
|
from ninja import Schema
|
|
|
|
from pydantic import Field as APIField
|
2023-12-03 23:27:20 +01:00
|
|
|
from pydantic import PositiveInt
|
2023-11-19 15:34:08 +01:00
|
|
|
|
2023-12-03 21:55:08 +01:00
|
|
|
from c3nav.api.auth import auth_permission_responses, auth_responses, validate_responses
|
2023-11-24 15:42:48 +01:00
|
|
|
from c3nav.api.exceptions import API404, APIPermissionDenied, APIRequestValidationFailed
|
2023-11-23 22:44:09 +01:00
|
|
|
from c3nav.api.utils import NonEmptyStr
|
2023-12-03 21:55:08 +01:00
|
|
|
from c3nav.mapdata.api.base import api_etag, api_stats
|
2023-11-19 15:34:08 +01:00
|
|
|
from c3nav.mapdata.models import Source
|
2023-11-24 01:05:38 +01:00
|
|
|
from c3nav.mapdata.models.locations import DynamicLocation, LocationRedirect, Position
|
2023-11-23 18:10:31 +01:00
|
|
|
from c3nav.mapdata.schemas.filters import BySearchableFilter, RemoveGeometryFilter
|
2023-11-24 15:42:48 +01:00
|
|
|
from c3nav.mapdata.schemas.model_base import AnyLocationID, AnyPositionID, CustomLocationID
|
2023-11-24 01:05:38 +01:00
|
|
|
from c3nav.mapdata.schemas.models import (AnyPositionStatusSchema, FullListableLocationSchema, FullLocationSchema,
|
|
|
|
LocationDisplay, SlimListableLocationSchema, SlimLocationSchema)
|
2023-11-24 01:23:07 +01:00
|
|
|
from c3nav.mapdata.schemas.responses import BoundsSchema, LocationGeometry
|
2023-11-23 22:44:09 +01:00
|
|
|
from c3nav.mapdata.utils.locations import (get_location_by_id_for_request, get_location_by_slug_for_request,
|
|
|
|
searchable_locations_for_request, visible_locations_for_request)
|
2023-11-23 21:11:31 +01:00
|
|
|
from c3nav.mapdata.utils.user import can_access_editor
|
2023-11-19 15:34:08 +01:00
|
|
|
|
|
|
|
map_api_router = APIRouter(tags=["map"])
|
|
|
|
|
|
|
|
|
2023-12-03 19:04:23 +01:00
|
|
|
@map_api_router.get('/bounds/', summary="get boundaries",
|
|
|
|
description="get maximum boundaries of everything on the map",
|
2023-11-19 15:34:08 +01:00
|
|
|
response={200: BoundsSchema, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_etag(permissions=False)
|
2023-11-19 15:34:08 +01:00
|
|
|
def bounds(request):
|
|
|
|
return {
|
|
|
|
"bounds": Source.max_bounds(),
|
|
|
|
}
|
2023-11-23 16:37:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LocationEndpointParameters(Schema):
|
|
|
|
searchable: bool = APIField(
|
|
|
|
False,
|
|
|
|
title='only list searchable locations',
|
|
|
|
description='if set, only searchable locations will be listed'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def can_access_geometry(request):
|
|
|
|
return True # todo: implementFd
|
|
|
|
|
|
|
|
|
2023-11-23 18:10:31 +01:00
|
|
|
class LocationListFilters(BySearchableFilter, RemoveGeometryFilter):
|
|
|
|
pass
|
|
|
|
|
2023-11-23 16:37:25 +01:00
|
|
|
|
2023-11-23 18:10:31 +01:00
|
|
|
def _location_list(request, detailed: bool, filters: LocationListFilters):
|
|
|
|
# todo: cache, visibility, etc…
|
2023-11-24 16:33:57 +01:00
|
|
|
if filters.searchable:
|
|
|
|
locations = searchable_locations_for_request(request)
|
|
|
|
else:
|
|
|
|
locations = visible_locations_for_request(request).values()
|
|
|
|
|
2023-12-03 21:47:43 +01:00
|
|
|
result = [obj.serialize(detailed=detailed, search=filters.searchable,
|
|
|
|
geometry=filters.geometry and can_access_geometry(request),
|
|
|
|
simple_geometry=True)
|
|
|
|
for obj in locations]
|
2023-11-23 16:37:25 +01:00
|
|
|
return result
|
2023-11-23 18:10:31 +01:00
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/', summary="list locations (slim)",
|
|
|
|
description="Get locations (with most important attributes set)",
|
|
|
|
response={200: list[SlimListableLocationSchema], **validate_responses, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-23 18:10:31 +01:00
|
|
|
def location_list(request, filters: Query[LocationListFilters]):
|
|
|
|
return _location_list(request, detailed=False, filters=filters)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/full/', summary="list locations (full)",
|
|
|
|
description="Get locations (with all attributes set)",
|
|
|
|
response={200: list[FullListableLocationSchema], **validate_responses, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-23 18:10:31 +01:00
|
|
|
def location_list_full(request, filters: Query[LocationListFilters]):
|
|
|
|
return _location_list(request, detailed=True, filters=filters)
|
2023-11-23 21:11:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _location_retrieve(request, location, detailed: bool, geometry: bool, show_redirects: bool):
|
|
|
|
# todo: cache, visibility, etc…
|
|
|
|
|
|
|
|
if location is None:
|
2023-11-24 15:42:48 +01:00
|
|
|
raise API404()
|
2023-11-23 21:11:31 +01:00
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
|
|
|
if not show_redirects:
|
|
|
|
return redirect('../' + str(location.target.slug)) # todo: use reverse, make pk and slug both work
|
|
|
|
|
2023-11-24 17:24:07 +01:00
|
|
|
if isinstance(location, (DynamicLocation, Position)):
|
|
|
|
request._target_etag = None
|
|
|
|
request._target_cache_key = None
|
|
|
|
|
2023-11-23 21:11:31 +01:00
|
|
|
return location.serialize(
|
|
|
|
detailed=detailed,
|
|
|
|
geometry=geometry and can_access_geometry(request),
|
|
|
|
simple_geometry=True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _location_display(request, location):
|
|
|
|
# todo: cache, visibility, etc…
|
|
|
|
|
|
|
|
if location is None:
|
2023-11-24 15:42:48 +01:00
|
|
|
raise API404()
|
2023-11-23 21:11:31 +01:00
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
|
|
|
return redirect('../' + str(location.target.slug) + '/details/') # todo: use reverse, make pk+slug work
|
|
|
|
|
|
|
|
result = location.details_display(
|
|
|
|
detailed_geometry=can_access_geometry(request),
|
|
|
|
editor_url=can_access_editor(request)
|
|
|
|
)
|
|
|
|
from pprint import pprint
|
|
|
|
pprint(result)
|
|
|
|
return json.loads(json.dumps(result, cls=DjangoJSONEncoder)) # todo: wtf?? well we need to get rid of lazy strings
|
|
|
|
|
|
|
|
|
2023-11-24 01:23:07 +01:00
|
|
|
def _location_geometry(request, location):
|
|
|
|
# todo: cache, visibility, etc…
|
|
|
|
|
|
|
|
if location is None:
|
2023-11-24 15:42:48 +01:00
|
|
|
raise API404()
|
2023-11-24 01:23:07 +01:00
|
|
|
|
|
|
|
if isinstance(location, LocationRedirect):
|
|
|
|
return redirect('../' + str(location.target.slug) + '/geometry/') # todo: use reverse, make pk+slug work
|
|
|
|
|
|
|
|
return LocationGeometry(
|
|
|
|
id=location.pk,
|
|
|
|
level=getattr(location, 'level_id', None),
|
|
|
|
geometry=location.get_geometry(
|
|
|
|
detailed_geometry=can_access_geometry(request)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-23 21:11:31 +01:00
|
|
|
class ShowRedirects(Schema):
|
|
|
|
show_redirects: bool = APIField(
|
|
|
|
False,
|
|
|
|
name="show redirects",
|
|
|
|
description="whether to show redirects instead of sending a redirect response",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/{location_id}/', summary="location by ID (slim)",
|
|
|
|
description="Get locations by ID (with all attributes set)",
|
|
|
|
response={200: SlimLocationSchema, **API404.dict(), **validate_responses, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_get')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-24 01:05:38 +01:00
|
|
|
def location_by_id(request, location_id: AnyLocationID, filters: Query[RemoveGeometryFilter],
|
2023-11-23 22:44:09 +01:00
|
|
|
redirects: Query[ShowRedirects]):
|
2023-11-23 21:11:31 +01:00
|
|
|
return _location_retrieve(
|
|
|
|
request,
|
|
|
|
get_location_by_id_for_request(location_id, request),
|
|
|
|
detailed=False, geometry=filters.geometry, show_redirects=redirects.show_redirects,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/{location_id}/full/', summary="location by ID (full)",
|
|
|
|
description="Get location by ID (with all attributes set)",
|
|
|
|
response={200: FullLocationSchema, **API404.dict(), **validate_responses, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_get')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-24 01:05:38 +01:00
|
|
|
def location_by_id_full(request, location_id: AnyLocationID, filters: Query[RemoveGeometryFilter],
|
2023-11-23 22:44:09 +01:00
|
|
|
redirects: Query[ShowRedirects]):
|
2023-11-23 21:11:31 +01:00
|
|
|
return _location_retrieve(
|
|
|
|
request,
|
|
|
|
get_location_by_id_for_request(location_id, request),
|
|
|
|
detailed=True, geometry=filters.geometry, show_redirects=redirects.show_redirects,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/{location_id}/display/', summary="location display by ID",
|
|
|
|
description="Get location display information by ID",
|
|
|
|
response={200: LocationDisplay, **API404.dict(), **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_display')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-24 01:05:38 +01:00
|
|
|
def location_by_id_display(request, location_id: AnyLocationID):
|
2023-11-23 21:11:31 +01:00
|
|
|
return _location_display(
|
|
|
|
request,
|
|
|
|
get_location_by_id_for_request(location_id, request),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/{location_id}/geometry/', summary="location geometry by id",
|
|
|
|
description="Get location geometry (if available) by ID",
|
|
|
|
response={200: LocationGeometry, **API404.dict(), **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_geometery')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-24 01:23:07 +01:00
|
|
|
def location_by_id_geometry(request, location_id: AnyLocationID):
|
|
|
|
return _location_geometry(
|
|
|
|
request,
|
|
|
|
get_location_by_id_for_request(location_id, request),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/by-slug/{location_slug}/', summary="location by slug (slim)",
|
|
|
|
description="Get location by slug (with most important attributes set)",
|
|
|
|
response={200: SlimLocationSchema, **API404.dict(), **validate_responses, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_get')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-23 22:44:09 +01:00
|
|
|
def location_by_slug(request, location_slug: NonEmptyStr, filters: Query[RemoveGeometryFilter],
|
|
|
|
redirects: Query[ShowRedirects]):
|
|
|
|
return _location_retrieve(
|
|
|
|
request,
|
|
|
|
get_location_by_slug_for_request(location_slug, request),
|
|
|
|
detailed=False, geometry=filters.geometry, show_redirects=redirects.show_redirects,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/by-slug/{location_slug}/full/', summary="location by slug (full)",
|
|
|
|
description="Get location by slug (with all attributes set)",
|
|
|
|
response={200: FullLocationSchema, **API404.dict(), **validate_responses, **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_get')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-23 22:44:09 +01:00
|
|
|
def location_by_slug_full(request, location_slug: NonEmptyStr, filters: Query[RemoveGeometryFilter],
|
|
|
|
redirects: Query[ShowRedirects]):
|
|
|
|
return _location_retrieve(
|
|
|
|
request,
|
|
|
|
get_location_by_slug_for_request(location_slug, request),
|
|
|
|
detailed=True, geometry=filters.geometry, show_redirects=redirects.show_redirects,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/by-slug/{location_slug}/display/', summary="location display by slug",
|
|
|
|
description="Get location display information by slug",
|
|
|
|
response={200: LocationDisplay, **API404.dict(), **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_display')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-23 22:44:09 +01:00
|
|
|
def location_by_slug_display(request, location_slug: NonEmptyStr):
|
|
|
|
return _location_display(
|
|
|
|
request,
|
|
|
|
get_location_by_slug_for_request(location_slug, request),
|
|
|
|
)
|
2023-11-24 01:05:38 +01:00
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/locations/by-slug/{location_slug}/geometry/', summary="location geometry by slug",
|
|
|
|
description="Get location geometry (if available) by slug",
|
|
|
|
response={200: LocationGeometry, **API404.dict(), **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('location_geometry')
|
|
|
|
@api_etag(base_mapdata=True)
|
2023-11-24 01:23:07 +01:00
|
|
|
def location_by_slug_geometry(request, location_slug: NonEmptyStr):
|
|
|
|
return _location_geometry(
|
|
|
|
request,
|
|
|
|
get_location_by_slug_for_request(location_slug, request),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-03 19:35:19 +01:00
|
|
|
@map_api_router.get('/positions/{position_id}/', summary="moving position coordinates",
|
|
|
|
description="get current coordinates of a moving position / dynamic location",
|
|
|
|
response={200: AnyPositionStatusSchema, **API404.dict(), **auth_responses})
|
2023-12-03 21:55:08 +01:00
|
|
|
@api_stats('get_position')
|
2023-11-24 17:37:33 +01:00
|
|
|
def get_position_by_id(request, position_id: AnyPositionID):
|
2023-11-24 14:35:48 +01:00
|
|
|
# no caching for obvious reasons!
|
2023-11-24 01:05:38 +01:00
|
|
|
location = None
|
|
|
|
if isinstance(position_id, int) or position_id.isdigit():
|
|
|
|
location = get_location_by_id_for_request(position_id, request)
|
|
|
|
if not isinstance(location, DynamicLocation):
|
2023-11-24 15:42:48 +01:00
|
|
|
raise API404()
|
2023-11-24 01:05:38 +01:00
|
|
|
if location is None and position_id.startswith('p:'):
|
|
|
|
try:
|
|
|
|
location = Position.objects.get(secret=position_id[2:])
|
|
|
|
except Position.DoesNotExist:
|
2023-11-24 15:42:48 +01:00
|
|
|
raise API404()
|
|
|
|
return location.serialize_position()
|
|
|
|
|
|
|
|
class UpdatePositionSchema(Schema):
|
2023-12-03 23:27:20 +01:00
|
|
|
coordinates_id: Union[
|
|
|
|
Annotated[CustomLocationID, APIField(title="set coordinates")],
|
|
|
|
Annotated[None, APIField(title="unset coordinates")],
|
|
|
|
] = APIField(
|
|
|
|
description="coordinates to set the location to or null to unset it"
|
2023-11-24 15:42:48 +01:00
|
|
|
)
|
2023-12-03 23:27:20 +01:00
|
|
|
timeout: Union[
|
|
|
|
Annotated[PositiveInt, APIField(title="new timeout")],
|
|
|
|
Annotated[None, APIField(title="don't change")],
|
|
|
|
] = APIField(
|
2023-11-24 15:42:48 +01:00
|
|
|
None,
|
2023-12-03 23:27:20 +01:00
|
|
|
title="timeout",
|
2023-11-24 15:42:48 +01:00
|
|
|
description="timeout for this new location in seconds, or None if not to change it",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-24 17:37:33 +01:00
|
|
|
@map_api_router.put('/positions/{position_id}/', url_name="position-update",
|
2023-12-03 19:35:19 +01:00
|
|
|
summary="set moving position",
|
|
|
|
description="only the string ID for the position secret must be used",
|
|
|
|
response={200: AnyPositionStatusSchema, **API404.dict(), **auth_permission_responses})
|
2023-11-24 17:37:33 +01:00
|
|
|
def set_position(request, position_id: AnyPositionID, update: UpdatePositionSchema):
|
2023-11-24 15:42:48 +01:00
|
|
|
# todo: may an API key do this?
|
|
|
|
if not update.position_id.startswith('p:'):
|
|
|
|
raise API404()
|
|
|
|
try:
|
|
|
|
location = Position.objects.get(secret=update.position_id[2:])
|
|
|
|
except Position.DoesNotExist:
|
|
|
|
raise API404()
|
|
|
|
if location.owner != request.user:
|
|
|
|
raise APIPermissionDenied()
|
|
|
|
|
|
|
|
coordinates = get_location_by_id_for_request(update.coordinates_id, request)
|
|
|
|
if coordinates is None:
|
|
|
|
raise APIRequestValidationFailed('Cant resolve coordinates.')
|
|
|
|
|
|
|
|
location.coordinates_id = update.coordinates_id
|
|
|
|
location.timeout = update.timeout
|
|
|
|
location.last_coordinates_update = timezone.now()
|
|
|
|
location.save()
|
|
|
|
|
2023-11-24 01:05:38 +01:00
|
|
|
return location.serialize_position()
|