2017-12-11 01:23:20 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
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
|
|
|
|
|
2017-11-28 00:41:34 +01:00
|
|
|
from c3nav.mapdata.models.access import AccessPermission
|
2017-11-28 00:16:43 +01:00
|
|
|
from c3nav.mapdata.utils.locations import visible_locations_for_request
|
2017-12-11 01:23:20 +01:00
|
|
|
from c3nav.routing.exceptions import LocationUnreachable, NoRouteFound, NotYetRoutable
|
2017-11-27 16:40:15 +01:00
|
|
|
from c3nav.routing.forms import RouteForm
|
2017-12-12 23:09:15 +01:00
|
|
|
from c3nav.routing.models import RouteOptions
|
2017-11-27 16:40:15 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2017-12-11 01:23:20 +01:00
|
|
|
try:
|
|
|
|
route = Router.load().get_route(origin=form.cleaned_data['origin'],
|
|
|
|
destination=form.cleaned_data['destination'],
|
|
|
|
permissions=AccessPermission.get_for_request(request))
|
|
|
|
except NotYetRoutable:
|
|
|
|
return Response({
|
|
|
|
'error': _('Not yet routable, try again shortly.'),
|
|
|
|
})
|
|
|
|
except LocationUnreachable:
|
|
|
|
return Response({
|
|
|
|
'error': _('Unreachable location.'),
|
|
|
|
})
|
|
|
|
except NoRouteFound:
|
|
|
|
return Response({
|
|
|
|
'error': _('No route found.'),
|
|
|
|
})
|
2017-11-27 16:40:15 +01:00
|
|
|
|
2017-11-28 12:32:46 +01:00
|
|
|
return Response({
|
|
|
|
'request': {
|
|
|
|
'origin': form.cleaned_data['origin'].pk,
|
|
|
|
'destination': form.cleaned_data['destination'].pk,
|
|
|
|
},
|
|
|
|
'result': route.serialize(locations=visible_locations_for_request(request)),
|
|
|
|
})
|
2017-12-12 23:09:15 +01:00
|
|
|
|
|
|
|
@list_route(methods=['get', 'post'])
|
|
|
|
def options(self, request, *args, **kwargs):
|
|
|
|
params = request.POST if request.method == 'POST' else request.GET
|
|
|
|
|
|
|
|
if request.method == 'POST' or 'save' in params:
|
|
|
|
pass
|
|
|
|
|
|
|
|
options = RouteOptions.get_for_request(request)
|
|
|
|
return Response({
|
|
|
|
'options': options.data,
|
|
|
|
'fields': {
|
|
|
|
name: {
|
|
|
|
'type': field.widget.input_type,
|
|
|
|
'label': field.label,
|
|
|
|
'choices': dict(field.choices),
|
|
|
|
}
|
|
|
|
for name, field in options.get_fields().items()},
|
|
|
|
})
|