diff options
author | Sawyer Bergeron <sbergeron@iol.unh.edu> | 2018-01-05 16:07:13 -0500 |
---|---|---|
committer | Sawyer Bergeron <sbergeron@iol.unh.edu> | 2018-01-09 15:03:34 -0500 |
commit | a1df798486c60c911dfb2e6c2c487f7bcb7f6d01 (patch) | |
tree | 60bec95b5de53c332ab1101240095940e5a4554b /src/booking/views.py | |
parent | c81da17a046f1ad81f05de8a242b14ec02cf7c9a (diff) |
Implement Booking Modification Interface
Jira: PHAROS-330
Users can change start date if it has not already occurred, and
can change end date, purpose, and both installer and scenario.
Standard checks apply similar to when initially creating a booking.
Change-Id: Ibae7fe91a58bd6e0741db065265c05c3823bdc27
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
Diffstat (limited to 'src/booking/views.py')
-rw-r--r-- | src/booking/views.py | 74 |
1 files changed, 71 insertions, 3 deletions
diff --git a/src/booking/views.py b/src/booking/views.py index da2fe11..ab3a04e 100644 --- a/src/booking/views.py +++ b/src/booking/views.py @@ -7,7 +7,6 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## - from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin @@ -21,11 +20,10 @@ from django.views.generic import TemplateView from jira import JIRAError from account.jira_util import get_jira -from booking.forms import BookingForm +from booking.forms import BookingForm, BookingEditForm from booking.models import Booking from dashboard.models import Resource - def create_jira_ticket(user, booking): jira = get_jira(user) issue_dict = { @@ -55,6 +53,7 @@ class BookingFormView(FormView): title = 'Booking: ' + self.resource.name context = super(BookingFormView, self).get_context_data(**kwargs) context.update({'title': title, 'resource': self.resource}) + #raise PermissionDenied('check') return context def get_success_url(self): @@ -92,6 +91,75 @@ class BookingFormView(FormView): return super(BookingFormView, self).form_valid(form) +class BookingEditFormView(FormView): + template_name = "booking/booking_calendar.html" + form_class = BookingEditForm + + def is_valid(self): + return True + + def dispatch(self, request, *args, **kwargs): + self.resource = get_object_or_404(Resource, id=self.kwargs['resource_id']) + self.original_booking = get_object_or_404(Booking, id=self.kwargs['booking_id']) + return super(BookingEditFormView, self).dispatch(request, *args, **kwargs) + + 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}) + return context + + def get_form_kwargs(self): + kwargs = super(BookingEditFormView, self).get_form_kwargs() + kwargs['purpose'] = self.original_booking.purpose + kwargs['start'] = self.original_booking.start + kwargs['end'] = self.original_booking.end + try: + kwargs['installer'] = self.original_booking.installer + except AttributeError: + pass + try: + kwargs['scenario'] = self.original_booking.scenario + except AttributeError: + pass + return kwargs + + def get_success_url(self): + return reverse('booking:create', args=(self.resource.id,)) + + def form_valid(self, form): + + if not self.request.user.is_authenticated: + messages.add_message(self.request, messages.ERROR, + 'You need to be logged in to book a Pod.') + return super(BookingEditFormView, self).form_invalid(form) + + if not self.request.user == self.original_booking.user: + messages.add_message(self.request, messages.ERROR, + 'You are not the owner of this booking.') + return super(BookingEditFormView, self).form_invalid(form) + + #Do Conflict Checks + if self.original_booking.start != form.cleaned_data['start']: + if timezone.now() > form.cleaned_data['start']: + messages.add_message(self.request, messages.ERROR, + 'Cannot change start date after it has occurred.') + return super(BookingEditFormView, self).form_invalid(form) + self.original_booking.start = form.cleaned_data['start'] + self.original_booking.end = form.cleaned_data['end'] + self.original_booking.purpose = form.cleaned_data['purpose'] + self.original_booking.installer = form.cleaned_data['installer'] + self.original_booking.scenario = form.cleaned_data['scenario'] + self.original_booking.reset = form.cleaned_data['reset'] + try: + self.original_booking.save() + except ValueError as err: + messages.add_message(self.request, messages.ERROR, err) + return super(BookingEditFormView, self).form_invalid(form) + + user = self.request.user + return super(BookingEditFormView, self).form_valid(form) + class BookingView(TemplateView): template_name = "booking/booking_detail.html" |