summaryrefslogtreecommitdiffstats
path: root/tools/pharos-dashboard/dashboard/views/booking.py
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2016-07-29 12:43:43 +0200
committerMax Breitenfeldt <max.breitenfeldt@gmail.com>2016-07-29 11:25:20 +0000
commit35c34c690ae9616d791c39fa218fe1621fa8d8d2 (patch)
tree6168caaeb4be9144ed6bfde0150850d6559c8b6f /tools/pharos-dashboard/dashboard/views/booking.py
parent7ae1f204de947a3c12804c04f32e20d45eb99eba (diff)
import pharos dashboard code
JIRA: RELENG-12 The last commit was missing some JS/CSS dependencies of the site. This happened because they are in folders that are named 'build' or 'dist'. This commit adds a bower.json file, that specifies dependencies. Dependencies can now be installed by running 'bower install' in the dashboard/static folder. Change-Id: I054f319c66771f767e97711cb678d79d3bd6bee4 Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'tools/pharos-dashboard/dashboard/views/booking.py')
-rw-r--r--tools/pharos-dashboard/dashboard/views/booking.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/tools/pharos-dashboard/dashboard/views/booking.py b/tools/pharos-dashboard/dashboard/views/booking.py
new file mode 100644
index 00000000..1499edb7
--- /dev/null
+++ b/tools/pharos-dashboard/dashboard/views/booking.py
@@ -0,0 +1,69 @@
+from dashboard.forms.booking_form import BookingForm
+from dashboard.models import Resource, Booking
+from dashboard.views.registration import BookingUserTestMixin
+from django.contrib import messages
+from django.contrib.auth.decorators import login_required
+from django.http import Http404, JsonResponse
+from django.shortcuts import get_object_or_404
+from django.utils.decorators import method_decorator
+from django.views.generic import FormView, View
+
+
+@method_decorator(login_required, name='dispatch')
+class BookingCalendarView(BookingUserTestMixin, FormView):
+ template_name = "dashboard/booking_calendar.html"
+ form_class = BookingForm
+
+ # set instance variables
+ def dispatch(self, request, *args, **kwargs):
+ self.foo = request.GET.get('foo', False)
+ self.resource = get_object_or_404(Resource, resource_id=self.kwargs['resource_id'])
+ return super(BookingCalendarView, self).dispatch(request, *args, **kwargs)
+
+ def form_valid(self, form):
+ self.success_url = self.request.path
+ booking = None
+ # change existing booking
+ if form.cleaned_data['booking_id'] is not None:
+ booking = get_object_or_404(Booking, booking_id=form.cleaned_data['booking_id'])
+ # create new booking
+ else:
+ booking = Booking()
+ booking.resource = self.resource
+ booking.user = self.request.user
+ booking.start_date_time = form.cleaned_data['start_date_time']
+ booking.end_date_time = form.cleaned_data['end_date_time']
+ booking.purpose = form.cleaned_data['purpose']
+ try:
+ booking.save()
+ except ValueError:
+ messages.add_message(self.request, messages.ERROR,
+ 'This booking overlaps with another booking')
+ return super(BookingCalendarView, self).form_invalid(form)
+ messages.add_message(self.request, messages.SUCCESS,
+ 'Booking saved')
+ return super(BookingCalendarView, self).form_valid(form)
+
+ def get_context_data(self, **kwargs):
+ title = 'Booking: ' + self.resource.name
+ context = super(BookingCalendarView, self).get_context_data(**kwargs)
+ context.update({'title': title, 'resource': self.resource})
+ return context
+
+
+@method_decorator(login_required, name='dispatch')
+class ResourceBookingsView(BookingUserTestMixin, View):
+ def get(self, request, *args, **kwargs):
+ resource = Resource.objects.get(resource_id=self.kwargs['resource_id'])
+ bookings = resource.booking_set.get_queryset().values(
+ 'booking_id', 'user__username', 'user__email',
+ 'start_date_time', 'end_date_time', 'purpose')
+ return JsonResponse({'bookings': list(bookings)})
+
+
+@method_decorator(login_required, name='dispatch')
+class DeleteBookingView(BookingUserTestMixin, View):
+ def post(self, request, *args, **kwargs):
+ booking = get_object_or_404(Booking, booking_id=self.kwargs['booking_id'])
+ booking.delete()
+ return JsonResponse({True: self.kwargs['booking_id']})