Commit 924dbc80 authored by Lukas Burgey's avatar Lukas Burgey

Lint frontend module

parent 7934e2a1
from .. import models
class State(object):
def __init__(self, user):
if user.is_authenticated:
self.services = (
models.Service.objects
.filter(groups__user=user)
.distinct()
)
else:
self.services = []
......@@ -53,17 +53,13 @@ class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.User
fields = [
'sub', 'email', 'username', 'ssh_keys', 'groups', 'deployments'
]
fields = ['sub', 'email', 'username', 'ssh_keys', 'groups', 'deployments']
class ClientSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.User
fields = [
'name', 'site'
]
fields = ['name', 'site']
class StateSerializer(serializers.Serializer):
......
from django.db import connections
from django.db.utils import OperationalError
import logging
from django.shortcuts import get_object_or_404
from rest_framework import views, viewsets
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework import status
from . import serializers
from .. import models
LOGGER = logging.getLogger(__name__)
from django_backend.backend.frontend import serializers, models as frontend_models
from django_backend.backend import models
import logging
logger = logging.getLogger(__name__)
def user_services(user):
if user.is_authenticated:
return (
models.Service.objects
.filter(groups__user=user)
.distinct()
)
return []
def _api_error_response():
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
......@@ -19,13 +27,15 @@ def _api_error_response():
# Response for StateView, LogoutView, and all post requests
def _api_state_response(request):
response = {
'logged_in': request.user.is_authenticated()
}
'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
response['services'] = serializers.ServiceSerializer(
user_services(request.user),
many=True,
).data
if 'error' in request.session:
response['error'] = request.session['error']
......@@ -35,29 +45,10 @@ def _api_state_response(request):
return Response(response)
class OperationalView(views.APIView):
authentication_classes = []
permission_classes = (AllowAny,)
def get(self, request, format=None):
try:
db_conn = connections['default']
db_conn.cursor()
# we check if we can access the state
request.session
except OperationalError as e:
logging.log_exception(e)
op = False
else:
op = True
response = {'operational': op}
return Response(response)
class StateView(views.APIView):
permission_classes = (AllowAny,)
def get(self, request, format=None):
def get(self, request):
return _api_state_response(request)
......@@ -67,7 +58,7 @@ class ServiceViewSet(viewsets.ModelViewSet):
class SSHPublicKeyView(views.APIView):
def post(self, request, format=None):
def post(self, request):
if 'type' not in request.data:
return _api_error_response()
......@@ -76,8 +67,9 @@ class SSHPublicKeyView(views.APIView):
if request_type == 'remove':
if 'name' in request.data:
key = get_object_or_404(
models.SSHPublicKey,
name=request.data['name'])
models.SSHPublicKey,
name=request.data['name']
)
# we do not delete ssh keys directly, as we need to keep track
# of them until all clients have also deleted them
......@@ -88,10 +80,10 @@ class SSHPublicKeyView(views.APIView):
elif request_type == 'add':
if 'key' in request.data:
key = models.SSHPublicKey(
user=request.user,
name=request.data['key']['name'],
key=request.data['key']['key'],
)
user=request.user,
name=request.data['key']['name'],
key=request.data['key']['key'],
)
# key state: -> (2)
key.save()
return _api_state_response(request)
......@@ -100,26 +92,28 @@ class SSHPublicKeyView(views.APIView):
class DeploymentView(views.APIView):
def post(self, request, format=None):
def post(self, request):
if (
'type' not in request.data or
'key' not in request.data or
'service' not in request.data
):
logger.error(
LOGGER.error(
'Deployment api got invalid request {}'.format(
request.data))
return _api_error_response()
request_type = request.data['type']
request_service = get_object_or_404(
models.Service, name=request.data['service'])
models.Service, name=request.data['service']
)
request_key = get_object_or_404(
models.SSHPublicKey,
name=request.data['key'])
models.SSHPublicKey,
name=request.data['key'],
)
# check if there is already an deployment
if not request.user.deployments.filter(service=request_service).exists():oeuaoeuaoeuoaeuoeauoaeuoeuaoeuoaeuaoeua
# if not request.user.deployments.filter(service=request_service).exists():
try:
deployment = request.user.deployments.get(service=request_service)
except Exception:
......
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