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.utils import timezone
15 from django.views import View
16 from django.views.generic import TemplateView
17 from django.shortcuts import redirect, render
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
26 def drop_filter(context):
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)
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)
39 context.update({'installer_filter': json.dumps(installer_filter), 'scenario_filter': json.dumps(scenario_filter)})
42 class BookingView(TemplateView):
43 template_name = "booking/booking_detail.html"
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})
53 class BookingDeleteView(TemplateView):
54 template_name = "booking/booking_delete.html"
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})
64 def bookingDelete(request, booking_id):
65 booking = get_object_or_404(Booking, id=booking_id)
67 messages.add_message(request, messages.SUCCESS, 'Booking deleted')
68 return redirect('../../../../')
71 class BookingListView(TemplateView):
72 template_name = "booking/booking_list.html"
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})
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(
93 return JsonResponse({'bookings': list(bookings)})
96 def booking_detail_view(request, booking_id):
98 if request.user.is_authenticated:
101 return render(request, "dashboard/login.html", {'title': 'Authentication Required'})
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'})
111 "booking/booking_detail.html",
113 'title': 'Booking Details',
120 def booking_stats_view(request):
123 "booking/stats.html",
124 context={"data": StatisticsManager.getContinuousBookingTimeSeries(), "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)