aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/views.py
diff options
context:
space:
mode:
authorParker Berberian <pberberian@iol.unh.edu>2019-08-12 14:18:25 -0400
committerParker Berberian <pberberian@iol.unh.edu>2019-08-13 12:49:44 -0400
commitd78fcef6ec55dbaa225c6607bdac430539bf3f0b (patch)
tree653cc5a73b3f2508c8ec93bf3055a3d87497929d /src/api/views.py
parent3418c7a7baae772f1bb58e9a827c1e6198dbed54 (diff)
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 <pberberian@iol.unh.edu>
Diffstat (limited to 'src/api/views.py')
-rw-r--r--src/api/views.py36
1 files changed, 36 insertions, 0 deletions
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)