Commit eab92bc0 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Replace redirects with a direct state response

parent 0e54c2a6
from django.contrib.auth import logout
from django.db import connections
from django.db.utils import OperationalError
from django.shortcuts import get_object_or_404, redirect
from django.shortcuts import get_object_or_404
from rest_framework import views, viewsets
from rest_framework.permissions import AllowAny
from rest_framework.authentication import TokenAuthentication
......@@ -12,6 +12,20 @@ from . import serializers, models as frontend_models
from .. import models
# Response for StateView, LogoutView, and all post requests
def state_response(request):
response = {
'logged_in': request.user.is_authenticated()
}
if request.user.is_authenticated():
response['user'] = serializers.UserSerializer(request.user).data
response['state'] = serializers.StateSerializer(
frontend_models.State(request.user)).data
return Response(response)
class OperationalView(views.APIView):
authentication_classes = []
permission_classes = (AllowAny,)
......@@ -33,24 +47,14 @@ class OperationalView(views.APIView):
class LogoutView(views.APIView):
def post(self, request, format=None):
logout(request)
return redirect('/backend/api/state/')
return state_response(request)
class StateView(views.APIView):
permission_classes = (AllowAny,)
def get(self, request, format=None):
response = {
'logged_in': request.user.is_authenticated()
}
if request.user.is_authenticated():
response['user'] = serializers.UserSerializer(request.user).data
response['state'] = serializers.StateSerializer(
frontend_models.State(request.user)).data
return Response(response)
return state_response(request)
class ServiceViewSet(viewsets.ModelViewSet):
......@@ -74,7 +78,7 @@ class SSHPublicKeyView(views.APIView):
# of them until all clients have also deleted them
key.deleted = True
key.save()
return redirect('/backend/api/state/')
return state_response(request)
elif request_type == 'add':
if 'key' in request.data:
key = models.SSHPublicKey(
......@@ -83,7 +87,7 @@ class SSHPublicKeyView(views.APIView):
key=request.data['key']['key'],
)
key.save()
return redirect('/backend/api/state/')
return state_response(request)
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
......@@ -120,11 +124,11 @@ class DeploymentView(views.APIView):
if request_type == 'add':
deployment.ssh_keys.add(request_key)
deployment.save()
return redirect('/backend/api/state/')
return state_response(request)
elif request_type == 'remove':
deployment.ssh_keys.remove(request_key)
deployment.save()
return redirect('/backend/api/state/')
return state_response(request)
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment