From 4c88cb25040026b9ea54d1cef4e296265e7acb70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Mon, 27 Nov 2017 16:40:15 +0100 Subject: [PATCH] add routing api --- src/c3nav/api/urls.py | 3 +++ src/c3nav/routing/api.py | 32 ++++++++++++++++++++++++++++++++ src/c3nav/routing/forms.py | 25 +++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/c3nav/routing/api.py create mode 100644 src/c3nav/routing/forms.py diff --git a/src/c3nav/api/urls.py b/src/c3nav/api/urls.py index 77e2a879..0562098b 100644 --- a/src/c3nav/api/urls.py +++ b/src/c3nav/api/urls.py @@ -12,6 +12,7 @@ from c3nav.mapdata.api import (AccessRestrictionViewSet, AreaViewSet, BuildingVi HoleViewSet, LevelViewSet, LineObstacleViewSet, LocationBySlugViewSet, LocationGroupCategoryViewSet, LocationGroupViewSet, LocationViewSet, MapViewSet, ObstacleViewSet, POIViewSet, RampViewSet, SourceViewSet, SpaceViewSet, StairViewSet) +from c3nav.routing.api import RoutingViewSet router = SimpleRouter() 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'locationgroups', LocationGroupViewSet) +router.register(r'routing', RoutingViewSet, base_name='routing') + router.register(r'editor', EditorViewSet, base_name='editor') router.register(r'changesets', ChangeSetViewSet) diff --git a/src/c3nav/routing/api.py b/src/c3nav/routing/api.py new file mode 100644 index 00000000..96ecbe3d --- /dev/null +++ b/src/c3nav/routing/api.py @@ -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= or ?space= + /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()) diff --git a/src/c3nav/routing/forms.py b/src/c3nav/routing/forms.py new file mode 100644 index 00000000..1ef4fb6a --- /dev/null +++ b/src/c3nav/routing/forms.py @@ -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.'))