From d78fcef6ec55dbaa225c6607bdac430539bf3f0b Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Mon, 12 Aug 2019 14:18:25 -0400 Subject: Adds Downtime Awareness This adds a Downtime model and relevant operations so that the dashboard knows when a lab is down for maintenance and can act accordingly. This change doesn't modify the front end at all, but it does pass relevant downtime info to the templates so that they can be updated in a future change. Change-Id: Idb88b15838b949f352f11a31a1fce9749d283d28 Signed-off-by: Parker Berberian --- src/api/views.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/api/views.py') diff --git a/src/api/views.py b/src/api/views.py index fb28958..a5153d7 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -12,6 +12,7 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from django.utils.decorators import method_decorator +from django.utils import timezone from django.views import View from django.http.response import JsonResponse, HttpResponse from rest_framework import viewsets @@ -20,6 +21,7 @@ from django.views.decorators.csrf import csrf_exempt from api.serializers.booking_serializer import BookingSerializer from api.serializers.old_serializers import UserSerializer +from api.forms import DowntimeForm from account.models import UserProfile from booking.models import Booking from api.models import LabManagerTracker, get_task @@ -150,6 +152,40 @@ def current_jobs(request, lab_name=""): return JsonResponse(lab_manager.get_current_jobs(), safe=False) +def lab_downtime(request, lab_name=""): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + if request.method == "GET": + return JsonResponse(lab_manager.get_downtime_json()) + if request.method == "POST": + return post_lab_downtime(request, lab_manager) + if request.method == "DELETE": + return delete_lab_downtime(lab_manager) + return HttpResponse(status=405) + + +def post_lab_downtime(request, lab_manager): + current_downtime = lab_manager.get_downtime() + if current_downtime.exists(): + return JsonResponse({"error": "Lab is already in downtime"}, status=422) + form = DowntimeForm(request.POST) + if form.is_valid(): + return JsonResponse(lab_manager.create_downtime(form)) + else: + return JsonResponse(form.errors.get_json_data(), status=400) + + +def delete_lab_downtime(lab_manager): + current_downtime = lab_manager.get_downtime() + if current_downtime.exists(): + dt = current_downtime.first() + dt.end = timezone.now() + dt.save() + return JsonResponse(lab_manager.get_downtime_json(), safe=False) + else: + return JsonResponse({"error": "Lab is not in downtime"}, status=422) + + def done_jobs(request, lab_name=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') lab_manager = LabManagerTracker.get(lab_name, lab_token) -- cgit 1.2.3-korg