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.contrib.auth import logout
from django.db import connections from django.db import connections
from django.db.utils import OperationalError 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 import views, viewsets
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from rest_framework.authentication import TokenAuthentication from rest_framework.authentication import TokenAuthentication
...@@ -12,6 +12,20 @@ from . import serializers, models as frontend_models ...@@ -12,6 +12,20 @@ from . import serializers, models as frontend_models
from .. import 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): class OperationalView(views.APIView):
authentication_classes = [] authentication_classes = []
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
...@@ -33,24 +47,14 @@ class OperationalView(views.APIView): ...@@ -33,24 +47,14 @@ class OperationalView(views.APIView):
class LogoutView(views.APIView): class LogoutView(views.APIView):
def post(self, request, format=None): def post(self, request, format=None):
logout(request) logout(request)
return redirect('/backend/api/state/') return state_response(request)
class StateView(views.APIView): class StateView(views.APIView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def get(self, request, format=None): def get(self, request, format=None):
return 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 ServiceViewSet(viewsets.ModelViewSet): class ServiceViewSet(viewsets.ModelViewSet):
...@@ -74,7 +78,7 @@ class SSHPublicKeyView(views.APIView): ...@@ -74,7 +78,7 @@ class SSHPublicKeyView(views.APIView):
# of them until all clients have also deleted them # of them until all clients have also deleted them
key.deleted = True key.deleted = True
key.save() key.save()
return redirect('/backend/api/state/') return state_response(request)
elif request_type == 'add': elif request_type == 'add':
if 'key' in request.data: if 'key' in request.data:
key = models.SSHPublicKey( key = models.SSHPublicKey(
...@@ -83,7 +87,7 @@ class SSHPublicKeyView(views.APIView): ...@@ -83,7 +87,7 @@ class SSHPublicKeyView(views.APIView):
key=request.data['key']['key'], key=request.data['key']['key'],
) )
key.save() key.save()
return redirect('/backend/api/state/') return state_response(request)
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST) return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
...@@ -120,11 +124,11 @@ class DeploymentView(views.APIView): ...@@ -120,11 +124,11 @@ class DeploymentView(views.APIView):
if request_type == 'add': if request_type == 'add':
deployment.ssh_keys.add(request_key) deployment.ssh_keys.add(request_key)
deployment.save() deployment.save()
return redirect('/backend/api/state/') return state_response(request)
elif request_type == 'remove': elif request_type == 'remove':
deployment.ssh_keys.remove(request_key) deployment.ssh_keys.remove(request_key)
deployment.save() deployment.save()
return redirect('/backend/api/state/') return state_response(request)
return Response({'ok': False}, status=status.HTTP_400_BAD_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