add routing api
This commit is contained in:
parent
c6948b4ce0
commit
4c88cb2504
3 changed files with 60 additions and 0 deletions
|
@ -12,6 +12,7 @@ from c3nav.mapdata.api import (AccessRestrictionViewSet, AreaViewSet, BuildingVi
|
||||||
HoleViewSet, LevelViewSet, LineObstacleViewSet, LocationBySlugViewSet,
|
HoleViewSet, LevelViewSet, LineObstacleViewSet, LocationBySlugViewSet,
|
||||||
LocationGroupCategoryViewSet, LocationGroupViewSet, LocationViewSet, MapViewSet,
|
LocationGroupCategoryViewSet, LocationGroupViewSet, LocationViewSet, MapViewSet,
|
||||||
ObstacleViewSet, POIViewSet, RampViewSet, SourceViewSet, SpaceViewSet, StairViewSet)
|
ObstacleViewSet, POIViewSet, RampViewSet, SourceViewSet, SpaceViewSet, StairViewSet)
|
||||||
|
from c3nav.routing.api import RoutingViewSet
|
||||||
|
|
||||||
router = SimpleRouter()
|
router = SimpleRouter()
|
||||||
router.register(r'map', MapViewSet, base_name='map')
|
router.register(r'map', MapViewSet, base_name='map')
|
||||||
|
@ -35,6 +36,8 @@ router.register(r'locations/by_slug', LocationBySlugViewSet, base_name='location
|
||||||
router.register(r'locationgroupcategories', LocationGroupCategoryViewSet)
|
router.register(r'locationgroupcategories', LocationGroupCategoryViewSet)
|
||||||
router.register(r'locationgroups', LocationGroupViewSet)
|
router.register(r'locationgroups', LocationGroupViewSet)
|
||||||
|
|
||||||
|
router.register(r'routing', RoutingViewSet, base_name='routing')
|
||||||
|
|
||||||
router.register(r'editor', EditorViewSet, base_name='editor')
|
router.register(r'editor', EditorViewSet, base_name='editor')
|
||||||
router.register(r'changesets', ChangeSetViewSet)
|
router.register(r'changesets', ChangeSetViewSet)
|
||||||
|
|
||||||
|
|
32
src/c3nav/routing/api.py
Normal file
32
src/c3nav/routing/api.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
from rest_framework.decorators import list_route
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.viewsets import ViewSet
|
||||||
|
|
||||||
|
from c3nav.routing.forms import RouteForm
|
||||||
|
from c3nav.routing.router import Router
|
||||||
|
|
||||||
|
|
||||||
|
class RoutingViewSet(ViewSet):
|
||||||
|
"""
|
||||||
|
Routing API
|
||||||
|
/geometries/ returns a list of geojson features, you have to specify ?level=<id> or ?space=<id>
|
||||||
|
/geometrystyles/ returns styling information for all geometry types
|
||||||
|
/bounds/ returns the maximum bounds of the map
|
||||||
|
"""
|
||||||
|
|
||||||
|
def list(self, request):
|
||||||
|
return []
|
||||||
|
|
||||||
|
@list_route(methods=['get'])
|
||||||
|
def route(self, request, *args, **kwargs):
|
||||||
|
params = request.POST if request.method == 'POST' else request.GET
|
||||||
|
form = RouteForm(params, request=request)
|
||||||
|
|
||||||
|
if not form.is_valid():
|
||||||
|
return Response({
|
||||||
|
'errors': form.errors,
|
||||||
|
})
|
||||||
|
|
||||||
|
route = Router.load().get_route(form.cleaned_data['origin'], form.cleaned_data['destination'])
|
||||||
|
|
||||||
|
return Response(route.serialize())
|
25
src/c3nav/routing/forms.py
Normal file
25
src/c3nav/routing/forms.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
from django import forms
|
||||||
|
from django.utils.translation import ugettext_lazy
|
||||||
|
|
||||||
|
from c3nav.mapdata.utils.locations import locations_for_request
|
||||||
|
|
||||||
|
|
||||||
|
class RouteForm(forms.Form):
|
||||||
|
origin = forms.IntegerField(min_value=1)
|
||||||
|
destination = forms.IntegerField(min_value=1)
|
||||||
|
|
||||||
|
def __init__(self, *args, request=None, **kwargs):
|
||||||
|
self.request = request
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def clean_origin(self):
|
||||||
|
try:
|
||||||
|
return locations_for_request(self.request)[self.cleaned_data['origin']]
|
||||||
|
except KeyError:
|
||||||
|
raise forms.ValidationError(ugettext_lazy('Unknown origin.'))
|
||||||
|
|
||||||
|
def clean_destination(self):
|
||||||
|
try:
|
||||||
|
return locations_for_request(self.request)[self.cleaned_data['destination']]
|
||||||
|
except KeyError:
|
||||||
|
raise forms.ValidationError(ugettext_lazy('Unknown destination.'))
|
Loading…
Add table
Add a link
Reference in a new issue