1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
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 ##############################################################################
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
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
29 class BookingViewSet(viewsets.ModelViewSet):
30 queryset = Booking.objects.all()
31 serializer_class = BookingSerializer
32 filter_fields = ('resource', 'id')
35 class UserViewSet(viewsets.ModelViewSet):
36 queryset = UserProfile.objects.all()
37 serializer_class = UserSerializer
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)
47 Token.objects.create(user=user)
48 return redirect('account:settings')
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)
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)
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")
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")
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)
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
94 lab_manager.update_host_remote_info(request.POST, host_id),
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)
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
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')
119 NotificationHandler.task_updated(task)
121 d['task'] = task.config.get_delta()
123 m['status'] = task.status
124 m['job'] = str(task.job)
125 m['message'] = task.message
127 return JsonResponse(d, safe=False)
128 elif request.method == "GET":
129 return JsonResponse(get_task(task_id).config.get_delta())
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)
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)
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)
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)