Removed document.open()
[laas.git] / src / booking / 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 from django.contrib import messages
12 from django.shortcuts import get_object_or_404
13 from django.http import JsonResponse
14 from django.utils import timezone
15 from django.views import View
16 from django.views.generic import TemplateView
17 from django.shortcuts import redirect, render
18 import json
19
20 from resource_inventory.models import ResourceBundle
21 from resource_inventory.resource_manager import ResourceManager
22 from booking.models import Booking, Installer, Opsys
23 from booking.stats import StatisticsManager
24
25
26 def drop_filter(context):
27     installer_filter = {}
28     for os in Opsys.objects.all():
29         installer_filter[os.id] = []
30         for installer in os.sup_installers.all():
31             installer_filter[os.id].append(installer.id)
32
33     scenario_filter = {}
34     for installer in Installer.objects.all():
35         scenario_filter[installer.id] = []
36         for scenario in installer.sup_scenarios.all():
37             scenario_filter[installer.id].append(scenario.id)
38
39     context.update({'installer_filter': json.dumps(installer_filter), 'scenario_filter': json.dumps(scenario_filter)})
40
41
42 class BookingView(TemplateView):
43     template_name = "booking/booking_detail.html"
44
45     def get_context_data(self, **kwargs):
46         booking = get_object_or_404(Booking, id=self.kwargs['booking_id'])
47         title = 'Booking Details'
48         context = super(BookingView, self).get_context_data(**kwargs)
49         context.update({'title': title, 'booking': booking})
50         return context
51
52
53 class BookingDeleteView(TemplateView):
54     template_name = "booking/booking_delete.html"
55
56     def get_context_data(self, **kwargs):
57         booking = get_object_or_404(Booking, id=self.kwargs['booking_id'])
58         title = 'Delete Booking'
59         context = super(BookingDeleteView, self).get_context_data(**kwargs)
60         context.update({'title': title, 'booking': booking})
61         return context
62
63
64 def bookingDelete(request, booking_id):
65     booking = get_object_or_404(Booking, id=booking_id)
66     booking.delete()
67     messages.add_message(request, messages.SUCCESS, 'Booking deleted')
68     return redirect('../../../../')
69
70
71 class BookingListView(TemplateView):
72     template_name = "booking/booking_list.html"
73
74     def get_context_data(self, **kwargs):
75         bookings = Booking.objects.filter(end__gte=timezone.now())
76         title = 'Search Booking'
77         context = super(BookingListView, self).get_context_data(**kwargs)
78         context.update({'title': title, 'bookings': bookings})
79         return context
80
81
82 class ResourceBookingsJSON(View):
83     def get(self, request, *args, **kwargs):
84         resource = get_object_or_404(ResourceBundle, id=self.kwargs['resource_id'])
85         bookings = resource.booking_set.get_queryset().values(
86             'id',
87             'start',
88             'end',
89             'purpose',
90             'jira_issue_status',
91             'config_bundle__name'
92         )
93         return JsonResponse({'bookings': list(bookings)})
94
95
96 def booking_detail_view(request, booking_id):
97     user = None
98     if request.user.is_authenticated:
99         user = request.user
100     else:
101         return render(request, "dashboard/login.html", {'title': 'Authentication Required'})
102
103     booking = get_object_or_404(Booking, id=booking_id)
104     allowed_users = set(list(booking.collaborators.all()))
105     allowed_users.add(booking.owner)
106     if user not in allowed_users:
107         return render(request, "dashboard/login.html", {'title': 'This page is private'})
108
109     return render(
110         request,
111         "booking/booking_detail.html",
112         {
113             'title': 'Booking Details',
114             'booking': booking,
115             'pdf': ResourceManager().makePDF(booking.resource),
116             'user_id': user.id
117         })
118
119
120 def booking_stats_view(request):
121     return render(
122         request,
123         "booking/stats.html",
124         context={"data": StatisticsManager.getContinuousBookingTimeSeries(), "title": "Booking Statistics"}
125     )
126
127
128 def booking_stats_json(request):
129     try:
130         span = int(request.GET.get("days", 14))
131     except:
132         span = 14
133     return JsonResponse(StatisticsManager.getContinuousBookingTimeSeries(span), safe=False)