rest_views.py 2.65 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 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, models


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:
            op = False
        else:
            op = True
        response = {'operational': op}
        return Response(response)


class LogoutView(views.APIView):
    def get(self, request, format=None):
        logout(request)
        response = {'logged_in': False}
        return Response(response)


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(
                models.State(request.user)).data

        return Response(response)


class ServiceViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.ServiceSerializer
    queryset = models.Service.objects.all()


class SSHPublicKeyView(views.APIView):
    def get(self, request, format=None):
        pass

    def post(self, request, format=None):
        if 'type' in request.data:
            request_type = request.data['type']

            if request_type == 'remove':
                if 'name' in request.data:
                    key = get_object_or_404(
                            models.SSHPublicKey,
                            name=request.data['name'])
                    key.delete()
                    return redirect('/backend/api/state/')
            elif request_type == 'add':
                if 'key' in request.data:
                    key = models.SSHPublicKey()
                    key.user = request.user
                    key.name = request.data['key']['name']
                    key.key = request.data['key']['key']
                    key.save()
                    return Response({'ok': True},
                                    status=status.HTTP_201_CREATED)

        return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)