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 ##############################################################################
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
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
28 def drop_filter(context):
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)
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)
41 context.update({'installer_filter': json.dumps(installer_filter), 'scenario_filter': json.dumps(scenario_filter)})
44 class BookingView(TemplateView):
45 template_name = "booking/booking_detail.html"
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})
55 class BookingDeleteView(TemplateView):
56 template_name = "booking/booking_delete.html"
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})
66 def bookingDelete(request, booking_id):
67 booking = get_object_or_404(Booking, id=booking_id)
69 messages.add_message(request, messages.SUCCESS, 'Booking deleted')
70 return redirect('../../../../')
73 class BookingListView(TemplateView):
74 template_name = "booking/booking_list.html"
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})
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(
95 return JsonResponse({'bookings': list(bookings)})
98 def booking_detail_view(request, booking_id):
100 if request.user.is_authenticated:
103 return render(request, "dashboard/login.html", {'title': 'Authentication Required'})
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',
113 'pdf': ResourceManager().makePDF(booking.resource),
117 def booking_stats_view(request):
120 "booking/stats.html",
122 "data": StatisticsManager.getContinuousBookingTimeSeries(),
123 "title": "Booking Statistics"
128 def booking_stats_json(request):
130 span = int(request.GET.get("days", 14))
133 return JsonResponse(StatisticsManager.getContinuousBookingTimeSeries(span), safe=False)