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 datetime import timedelta
13 from django.conf import settings
14 from django.contrib import messages
15 from django.contrib.auth.mixins import LoginRequiredMixin
16 from django.http import JsonResponse
17 from django.shortcuts import get_object_or_404
18 from django.shortcuts import redirect
19 from django.urls import reverse
20 from django.utils import timezone
21 from django.views import View
22 from django.views.generic import FormView
23 from django.views.generic import TemplateView
24 from jira import JIRAError
26 from account.jira_util import get_jira
27 from booking.forms import BookingForm
28 from booking.models import Booking
29 from dashboard.models import Resource
32 def create_jira_ticket(user, booking):
36 'summary': str(booking.resource) + ': Access Request',
37 'description': booking.purpose,
38 'issuetype': {'name': 'Task'},
39 'components': [{'name': 'POD Access Request'}],
40 'assignee': {'name': booking.resource.owner.username}
42 issue = jira.create_issue(fields=issue_dict)
43 jira.add_attachment(issue, user.userprofile.pgp_public_key)
44 jira.add_attachment(issue, user.userprofile.ssh_public_key)
45 booking.jira_issue_id = issue.id
49 class BookingFormView(LoginRequiredMixin, FormView):
50 template_name = "booking/booking_calendar.html"
51 form_class = BookingForm
53 def dispatch(self, request, *args, **kwargs):
54 self.resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
55 return super(BookingFormView, self).dispatch(request, *args, **kwargs)
57 def get_context_data(self, **kwargs):
58 title = 'Booking: ' + self.resource.name
59 context = super(BookingFormView, self).get_context_data(**kwargs)
60 context.update({'title': title, 'resource': self.resource})
63 def get_success_url(self):
64 return reverse('booking:create', kwargs=self.kwargs)
66 def form_valid(self, form):
67 user = self.request.user
68 if not user.userprofile.ssh_public_key or not user.userprofile.pgp_public_key:
69 messages.add_message(self.request, messages.INFO,
70 'Please upload your private keys before booking')
71 return redirect('account:settings')
72 booking = Booking(start=form.cleaned_data['start'], end=form.cleaned_data['end'],
73 purpose=form.cleaned_data['purpose'], resource=self.resource,
77 except ValueError as err:
78 messages.add_message(self.request, messages.ERROR, err)
79 return super(BookingFormView, self).form_invalid(form)
80 except PermissionError as err:
81 messages.add_message(self.request, messages.ERROR, err)
82 return super(BookingFormView, self).form_invalid(form)
84 if settings.CREATE_JIRA_TICKET:
85 create_jira_ticket(user, booking)
87 messages.add_message(self.request, messages.ERROR, 'Failed to create Jira Ticket. '
88 'Please check your Jira '
91 return super(BookingFormView, self).form_invalid(form)
92 messages.add_message(self.request, messages.SUCCESS, 'Booking saved')
93 return super(BookingFormView, self).form_valid(form)
96 class BookingView(TemplateView):
97 template_name = "booking/booking_detail.html"
99 def get_context_data(self, **kwargs):
100 booking = get_object_or_404(Booking, id=self.kwargs['booking_id'])
101 jira_issue = booking.get_jira_issue()
102 title = 'Booking Details'
103 context = super(BookingView, self).get_context_data(**kwargs)
104 context.update({'title': title, 'booking': booking, 'jira_issue': jira_issue})
108 class ResourceBookingsJSON(View):
109 def get(self, request, *args, **kwargs):
110 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
111 bookings = resource.booking_set.get_queryset().values('id', 'start', 'end', 'purpose',
113 return JsonResponse({'bookings': list(bookings)})