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