From dd7e16215d3bf6bb21a6d4325171d9d556c5da2d Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Mon, 26 Feb 2018 17:52:25 -0500 Subject: Limit User Booking Length and Extensions Jira: PHAROS-363 Bookings can now only be 3 weeks upon creation with a maximum of 2 1 week extensions. Change-Id: I677770de3f62f188d23e60be6d71b42b25bf007e Signed-off-by: Sawyer Bergeron --- .../booking/migrations/0004_booking_ext_count.py | 27 ++++++++++++++++++++++ dashboard/src/booking/models.py | 1 + dashboard/src/booking/views.py | 22 ++++++++++++++++-- .../src/templates/booking/booking_calendar.html | 15 +++++++++++- .../src/templates/booking/booking_detail.html | 6 ++++- 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 dashboard/src/booking/migrations/0004_booking_ext_count.py (limited to 'dashboard/src') diff --git a/dashboard/src/booking/migrations/0004_booking_ext_count.py b/dashboard/src/booking/migrations/0004_booking_ext_count.py new file mode 100644 index 0000000..6bcc3ce --- /dev/null +++ b/dashboard/src/booking/migrations/0004_booking_ext_count.py @@ -0,0 +1,27 @@ +############################################################################## +# Copyright (c) 2018 Sawyer Bergeron and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('booking', '0003_auto_20180108_2024'), + ] + + operations = [ + migrations.AddField( + model_name='booking', + name='ext_count', + field=models.IntegerField(default=2), + ), + ] diff --git a/dashboard/src/booking/models.py b/dashboard/src/booking/models.py index 9156484..8762d46 100644 --- a/dashboard/src/booking/models.py +++ b/dashboard/src/booking/models.py @@ -55,6 +55,7 @@ class Booking(models.Model): installer = models.ForeignKey(Installer, models.DO_NOTHING, null=True) scenario = models.ForeignKey(Scenario, models.DO_NOTHING, null=True) purpose = models.CharField(max_length=300, blank=False) + ext_count = models.IntegerField(default=2) class Meta: db_table = 'booking' diff --git a/dashboard/src/booking/views.py b/dashboard/src/booking/views.py index 7e35af2..a52cfe2 100644 --- a/dashboard/src/booking/views.py +++ b/dashboard/src/booking/views.py @@ -66,6 +66,11 @@ class BookingFormView(FormView): 'You need to be logged in to book a Pod.') return super(BookingFormView, self).form_invalid(form) + if form.cleaned_data['end'] - form.cleaned_data['start'] > timezone.timedelta(days=21): + messages.add_message(self.request, messages.ERROR, + 'Bookings can be no more than 3 weeks long.') + return super(BookingFormView, self).form_invalid(form) + user = self.request.user booking = Booking(start=form.cleaned_data['start'], end=form.cleaned_data['end'], @@ -107,7 +112,7 @@ class BookingEditFormView(FormView): def get_context_data(self, **kwargs): title = 'Editing Booking on: ' + self.resource.name context = super(BookingEditFormView, self).get_context_data(**kwargs) - context.update({'title': title, 'resource': self.resource}) + context.update({'title': title, 'resource': self.resource, 'booking': self.original_booking}) return context def get_form_kwargs(self): @@ -141,6 +146,19 @@ class BookingEditFormView(FormView): return super(BookingEditFormView, self).form_invalid(form) #Do Conflict Checks + if self.original_booking.end != form.cleaned_data['end']: + if form.cleaned_data['end'] - self.original_booking.end > timezone.timedelta(days=7): + messages.add_message(self.request, messages.ERROR, + 'Extensions can not be longer than one week.') + return super(BookingEditFormView, self).form_invalid(form) + elif self.original_booking.ext_count <= 0: + messages.add_message(self.request, messages.ERROR, + 'Cannot change end date after maximum number of extensions reached.') + return super(BookingEditFormView, self).form_invalid(form) + + else: + self.original_booking.ext_count -= 1 + if self.original_booking.start != form.cleaned_data['start']: if timezone.now() > form.cleaned_data['start']: messages.add_message(self.request, messages.ERROR, @@ -205,4 +223,4 @@ class ResourceBookingsJSON(View): bookings = resource.booking_set.get_queryset().values('id', 'start', 'end', 'purpose', 'jira_issue_status', 'opsys__name', 'installer__name', 'scenario__name') - return JsonResponse({'bookings': list(bookings)}) \ No newline at end of file + return JsonResponse({'bookings': list(bookings)}) diff --git a/dashboard/src/templates/booking/booking_calendar.html b/dashboard/src/templates/booking/booking_calendar.html index 81efbe5..b60db3c 100644 --- a/dashboard/src/templates/booking/booking_calendar.html +++ b/dashboard/src/templates/booking/booking_calendar.html @@ -64,13 +64,26 @@ {% bootstrap_form_errors form type='non_fields' %}
{% csrf_token %} - +
{% bootstrap_field form.start addon_after='' %}
{% bootstrap_field form.end addon_after='' %}
+
+ {% bootstrap_field form.purpose %} {% bootstrap_field form.installer %} {% bootstrap_field form.scenario %} diff --git a/dashboard/src/templates/booking/booking_detail.html b/dashboard/src/templates/booking/booking_detail.html index c88cd84..4628a62 100644 --- a/dashboard/src/templates/booking/booking_detail.html +++ b/dashboard/src/templates/booking/booking_detail.html @@ -44,6 +44,10 @@

Scenario: {{ booking.scenario }}

+

+ Extensions Remaining: {{ booking.ext_count }} +

+ {% if user.is_authenticated %} {% if user.get_username == booking.user.username %}

@@ -67,4 +71,4 @@

{% endif %} -{% endif %} \ No newline at end of file +{% endif %} -- cgit 1.2.3-korg