team-3/src/c3nav/routing/api.py

24 lines
852 B
Python
Raw Normal View History

2017-11-27 16:40:15 +01:00
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from c3nav.mapdata.utils.locations import visible_locations_for_request
2017-11-27 16:40:15 +01:00
from c3nav.routing.forms import RouteForm
from c3nav.routing.router import Router
class RoutingViewSet(ViewSet):
2017-11-27 16:43:41 +01:00
@list_route(methods=['get', 'post'])
2017-11-27 16:40:15 +01:00
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(locations=visible_locations_for_request(request)))