Merge "Fixes a typo"
[pharos-tools.git] / dashboard / src / api / views.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11
12 from django.contrib.auth.decorators import login_required
13 from django.shortcuts import redirect
14 from django.utils.decorators import method_decorator
15 from django.views import View
16 from django.http.response import JsonResponse, HttpResponse
17 from rest_framework import viewsets
18 from rest_framework.authtoken.models import Token
19 from django.views.decorators.csrf import csrf_exempt
20
21 from api.serializers.booking_serializer import BookingSerializer
22 from api.serializers.old_serializers import UserSerializer
23 from account.models import UserProfile
24 from booking.models import Booking
25 from api.models import LabManagerTracker, get_task
26 from notifier.manager import NotificationHandler
27
28
29 class BookingViewSet(viewsets.ModelViewSet):
30     queryset = Booking.objects.all()
31     serializer_class = BookingSerializer
32     filter_fields = ('resource', 'id')
33
34
35 class UserViewSet(viewsets.ModelViewSet):
36     queryset = UserProfile.objects.all()
37     serializer_class = UserSerializer
38
39
40 @method_decorator(login_required, name='dispatch')
41 class GenerateTokenView(View):
42     def get(self, request, *args, **kwargs):
43         user = self.request.user
44         token, created = Token.objects.get_or_create(user=user)
45         if not created:
46             token.delete()
47             Token.objects.create(user=user)
48         return redirect('account:settings')
49
50
51 def lab_inventory(request, lab_name=""):
52     lab_token = request.META.get('HTTP_AUTH_TOKEN')
53     lab_manager = LabManagerTracker.get(lab_name, lab_token)
54     return JsonResponse(lab_manager.get_inventory(), safe=False)
55
56
57 @csrf_exempt
58 def lab_host(request, lab_name="", host_id=""):
59     lab_token = request.META.get('HTTP_AUTH_TOKEN')
60     lab_manager = LabManagerTracker.get(lab_name, lab_token)
61     if request.method == "GET":
62         return JsonResponse(lab_manager.get_host(host_id), safe=False)
63     if request.method == "POST":
64         return JsonResponse(lab_manager.update_host(host_id, request.POST), safe=False)
65
66
67 def get_pdf(request, lab_name="", booking_id=""):
68     lab_token = request.META.get('HTTP_AUTH_TOKEN')
69     lab_manager = LabManagerTracker.get(lab_name, lab_token)
70     return HttpResponse(lab_manager.get_pdf(booking_id), content_type="text/plain")
71
72
73 def get_idf(request, lab_name="", booking_id=""):
74     lab_token = request.META.get('HTTP_AUTH_TOKEN')
75     lab_manager = LabManagerTracker.get(lab_name, lab_token)
76     return HttpResponse(lab_manager.get_idf(booking_id), content_type="text/plain")
77
78
79 def lab_status(request, lab_name=""):
80     lab_token = request.META.get('HTTP_AUTH_TOKEN')
81     lab_manager = LabManagerTracker.get(lab_name, lab_token)
82     if request.method == "POST":
83         return JsonResponse(lab_manager.set_status(request.POST), safe=False)
84     return JsonResponse(lab_manager.get_status(), safe=False)
85
86
87 @csrf_exempt
88 def update_host_bmc(request, lab_name="", host_id=""):
89     lab_token = request.META.get('HTTP_AUTH_TOKEN')
90     lab_manager = LabManagerTracker.get(lab_name, lab_token)
91     if request.method == "POST":
92         # update / create RemoteInfo for host
93         return JsonResponse(
94             lab_manager.update_host_remote_info(request.POST, host_id),
95             safe=False
96         )
97
98
99 def lab_profile(request, lab_name=""):
100     lab_token = request.META.get('HTTP_AUTH_TOKEN')
101     lab_manager = LabManagerTracker.get(lab_name, lab_token)
102     return JsonResponse(lab_manager.get_profile(), safe=False)
103
104
105 @csrf_exempt
106 def specific_task(request, lab_name="", job_id="", task_id=""):
107     lab_token = request.META.get('HTTP_AUTH_TOKEN')
108     LabManagerTracker.get(lab_name, lab_token)  # Authorize caller, but we dont need the result
109
110     if request.method == "POST":
111         task = get_task(task_id)
112         if 'status' in request.POST:
113             task.status = request.POST.get('status')
114         if 'message' in request.POST:
115             task.message = request.POST.get('message')
116         if 'lab_token' in request.POST:
117             task.lab_token = request.POST.get('lab_token')
118         task.save()
119         NotificationHandler.task_updated(task)
120         d = {}
121         d['task'] = task.config.get_delta()
122         m = {}
123         m['status'] = task.status
124         m['job'] = str(task.job)
125         m['message'] = task.message
126         d['meta'] = m
127         return JsonResponse(d, safe=False)
128     elif request.method == "GET":
129         return JsonResponse(get_task(task_id).config.get_delta())
130
131
132 @csrf_exempt
133 def specific_job(request, lab_name="", job_id=""):
134     lab_token = request.META.get('HTTP_AUTH_TOKEN')
135     lab_manager = LabManagerTracker.get(lab_name, lab_token)
136     if request.method == "POST":
137         return JsonResponse(lab_manager.update_job(job_id, request.POST), safe=False)
138     return JsonResponse(lab_manager.get_job(job_id), safe=False)
139
140
141 def new_jobs(request, lab_name=""):
142     lab_token = request.META.get('HTTP_AUTH_TOKEN')
143     lab_manager = LabManagerTracker.get(lab_name, lab_token)
144     return JsonResponse(lab_manager.get_new_jobs(), safe=False)
145
146
147 def current_jobs(request, lab_name=""):
148     lab_token = request.META.get('HTTP_AUTH_TOKEN')
149     lab_manager = LabManagerTracker.get(lab_name, lab_token)
150     return JsonResponse(lab_manager.get_current_jobs(), safe=False)
151
152
153 def done_jobs(request, lab_name=""):
154     lab_token = request.META.get('HTTP_AUTH_TOKEN')
155     lab_manager = LabManagerTracker.get(lab_name, lab_token)
156     return JsonResponse(lab_manager.get_done_jobs(), safe=False)