views.py 2.71 KB
Newer Older
1

2
import logging
3
from rest_framework import generics, views
4
from rest_framework.authentication import BasicAuthentication
5
from rest_framework.response import Response
6
from .serializers import DeploymentTaskSerializer, ServiceSerializer, RabbitMQInstanceSerializer
7
from ..models import RabbitMQInstance
8

9
10
11
LOGGER = logging.getLogger(__name__)


12
# authentication class for the client api
13
AUTHENTICATION_CLASSES = (BasicAuthentication, )
14

15

16
class DeploymentsView(generics.ListAPIView):
Lukas Burgey's avatar
Lukas Burgey committed
17
    authentication_classes = AUTHENTICATION_CLASSES
18
    serializer_class = DeploymentTaskSerializer
19

20
21
    def get_queryset(self):
        return self.request.user.site.tasks
22
23


24
# the client has to fetch the configuration (like services etc.) here
25
class ConfigurationView(views.APIView):
Lukas Burgey's avatar
Lukas Burgey committed
26
    authentication_classes = AUTHENTICATION_CLASSES
27

28
29
30
31
32
33
34
35
36
    def get(self, request):
        response = {
            'services': ServiceSerializer(
                request.user.site.services.all(),
                many=True,
            ).data,
            'rabbitmq_config': RabbitMQInstanceSerializer(
                RabbitMQInstance.load(),
            ).data,
Lukas Burgey's avatar
Lukas Burgey committed
37
            'site': request.user.site.name,
38
39
        }
        return Response(response)
40
41
42


class AckView(views.APIView):
Lukas Burgey's avatar
Lukas Burgey committed
43
    authentication_classes = AUTHENTICATION_CLASSES
44

45
    def delete(self, request, task_id=None):
46
47
        # find the corresponding task for this item
        for item in request.user.site.task_items.all():
48
            if item.task.id == int(task_id):
49
                item.success()
50
51
                return Response({'ok': True})

52
53
54
        # this is no critical
        LOGGER.info('%s executed the obsolete task#%s', request.user, task_id)
        return Response({'ok': True})
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73


class ResponseView(views.APIView):
    authentication_classes = AUTHENTICATION_CLASSES

    def post(self, request):
        status = request.data['output']['status']
        task_id = request.data['id']

        LOGGER.debug('%s responded to task %s:\n%s', request.user, task_id, request.data)

        # find the corresponding task for this item
        task_item = None
        for item in request.user.site.task_items.all():
            if item.task.id == int(task_id):
                task_item = item

        if task_item is not None:
            if status == 'success':
74
                task_item.done()
75
76
77
                return Response({'ok': True})

            elif status == 'fail':
78
                task_item.failed()
79
80
81
                return Response({'ok': True})

            elif status == 'reject':
82
                task_item.rejected(request.data['output']['questionnaire'])
83
84
85
86
                return Response({'ok': True})

        LOGGER.info('%s executed the obsolete task#%s', request.user, task_id)
        return Response({'ok': False})