Update urls and views

This commit is contained in:
2022-02-14 18:17:04 +01:00
parent 27564be205
commit 9678ba3789
3 changed files with 34 additions and 3 deletions

View File

@@ -24,8 +24,8 @@ from driver import urls as driver_urls
urlpatterns = [
path('ht/', include('health_check.urls')),
path('admin/', admin.site.urls),
path('roster/', include(roster_urls)),
path('dcc/', include(driver_urls)),
path('api/v1/roster/', include(roster_urls)),
path('api/v1/dcc/', include(driver_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# if settings.DEBUG:

View File

@@ -1,7 +1,8 @@
from django.urls import path
from driver.views import SendCommand, Function, Cab, Emergency, Infra
from driver.views import SendCommand, Function, Cab, Emergency, Infra, Test
urlpatterns = [
path('test', Test.as_view()),
path('emergency', Emergency.as_view()),
path('infra', Infra.as_view()),
path('command', SendCommand.as_view()),

View File

@@ -12,6 +12,9 @@ from roster.models import Cab as CabModel
def addresschecker(f):
"""
Check if DCC address does exist in the database
"""
def addresslookup(request, address, *args):
try:
CabModel.objects.get(address=address)
@@ -21,7 +24,22 @@ def addresschecker(f):
return addresslookup
class Test(APIView):
"""
Send a test <s> command
"""
parser_classes = [PlainTextParser]
def get(self, request):
response = Connector().passthrough("<s>")
return Response({"response": response.decode()},
status=status.HTTP_202_ACCEPTED)
class SendCommand(APIView):
"""
Command passthrough
"""
parser_classes = [PlainTextParser]
def put(self, request):
@@ -40,6 +58,9 @@ class SendCommand(APIView):
@method_decorator(addresschecker, name="put")
class Function(APIView):
"""
Send "Function" commands to a valid DCC address
"""
def put(self, request, address):
serializer = FunctionSerializer(data=request.data)
if serializer.is_valid():
@@ -53,6 +74,9 @@ class Function(APIView):
@method_decorator(addresschecker, name="put")
class Cab(APIView):
"""
Send "Cab" commands to a valid DCC address
"""
def put(self, request, address):
serializer = CabSerializer(data=request.data)
if serializer.is_valid():
@@ -65,6 +89,9 @@ class Cab(APIView):
class Infra(APIView):
"""
Send "Infra" commands to a valid DCC address
"""
def put(self, request):
serializer = InfraSerializer(data=request.data)
if serializer.is_valid():
@@ -77,6 +104,9 @@ class Infra(APIView):
class Emergency(APIView):
"""
Send an "Emergency" stop, no matter the HTTP method used
"""
def put(self, request):
Connector().emergency()
return Response({"response": "emergency stop"},