1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from django.conf import settings
12 from django.contrib import messages
13 from django.contrib.auth.mixins import LoginRequiredMixin
14 from django.http import JsonResponse
15 from django.shortcuts import get_object_or_404
16 from django.urls import reverse
17 from django.utils import timezone
18 from django.views import View
19 from django.views.generic import FormView
20 from django.views.generic import TemplateView
21 from jira import JIRAError
23 from account.jira_util import get_jira
24 from booking.forms import BookingForm
25 from booking.models import Booking
26 from dashboard.models import Resource
29 def create_jira_ticket(user, booking):
33 'summary': str(booking.resource) + ': Access Request',
34 'description': booking.purpose,
35 'issuetype': {'name': 'Task'},
36 'components': [{'name': 'POD Access Request'}],
37 'assignee': {'name': booking.resource.owner.username}
39 issue = jira.create_issue(fields=issue_dict)
40 jira.add_attachment(issue, user.userprofile.pgp_public_key)
41 jira.add_attachment(issue, user.userprofile.ssh_public_key)
42 booking.jira_issue_id = issue.id
46 class BookingFormView(FormView):
47 template_name = "booking/booking_calendar.html"
48 form_class = BookingForm
50 def dispatch(self, request, *args, **kwargs):
51 self.resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
52 return super(BookingFormView, self).dispatch(request, *args, **kwargs)
54 def get_context_data(self, **kwargs):
55 title = 'Booking: ' + self.resource.name
56 context = super(BookingFormView, self).get_context_data(**kwargs)
57 context.update({'title': title, 'resource': self.resource})
60 def get_success_url(self):
61 return reverse('booking:create', kwargs=self.kwargs)
63 def form_valid(self, form):
64 if not self.request.user.is_authenticated:
65 messages.add_message(self.request, messages.ERROR,
66 'You need to be logged in to book a Pod.')
67 return super(BookingFormView, self).form_invalid(form)
69 user = self.request.user
70 booking = Booking(start=form.cleaned_data['start'],
71 end=form.cleaned_data['end'],
72 purpose=form.cleaned_data['purpose'],
73 installer=form.cleaned_data['installer'],
74 scenario=form.cleaned_data['scenario'],
75 resource=self.resource, user=user)
78 except ValueError as err:
79 messages.add_message(self.request, messages.ERROR, err)
80 return super(BookingFormView, self).form_invalid(form)
82 if settings.CREATE_JIRA_TICKET:
83 create_jira_ticket(user, booking)
85 messages.add_message(self.request, messages.ERROR, 'Failed to create Jira Ticket. '
86 'Please check your Jira '
89 return super(BookingFormView, self).form_invalid(form)
90 messages.add_message(self.request, messages.SUCCESS, 'Booking saved')
91 return super(BookingFormView, self).form_valid(form)
94 class BookingView(TemplateView):
95 template_name = "booking/booking_detail.html"
97 def get_context_data(self, **kwargs):
98 booking = get_object_or_404(Booking, id=self.kwargs['booking_id'])
99 title = 'Booking Details'
100 context = super(BookingView, self).get_context_data(**kwargs)
101 context.update({'title': title, 'booking': booking})
105 class BookingListView(TemplateView):
106 template_name = "booking/booking_list.html"
108 def get_context_data(self, **kwargs):
109 bookings = Booking.objects.filter(end__gte=timezone.now())
110 title = 'Search Booking'
111 context = super(BookingListView, self).get_context_data(**kwargs)
112 context.update({'title': title, 'bookings': bookings})
116 class ResourceBookingsJSON(View):
117 def get(self, request, *args, **kwargs):
118 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
119 bookings = resource.booking_set.get_queryset().values('id', 'start', 'end', 'purpose',
121 'installer__name', 'scenario__name')
122 return JsonResponse({'bookings': list(bookings)})