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 opsys=form.cleaned_data['opsys'],
74 installer=form.cleaned_data['installer'],
75 scenario=form.cleaned_data['scenario'],
76 resource=self.resource, user=user)
79 except ValueError as err:
80 messages.add_message(self.request, messages.ERROR, err)
81 return super(BookingFormView, self).form_invalid(form)
83 if settings.CREATE_JIRA_TICKET:
84 create_jira_ticket(user, booking)
86 messages.add_message(self.request, messages.ERROR, 'Failed to create Jira Ticket. '
87 'Please check your Jira '
90 return super(BookingFormView, self).form_invalid(form)
91 messages.add_message(self.request, messages.SUCCESS, 'Booking saved')
92 return super(BookingFormView, self).form_valid(form)
95 class BookingView(TemplateView):
96 template_name = "booking/booking_detail.html"
98 def get_context_data(self, **kwargs):
99 booking = get_object_or_404(Booking, id=self.kwargs['booking_id'])
100 title = 'Booking Details'
101 context = super(BookingView, self).get_context_data(**kwargs)
102 context.update({'title': title, 'booking': booking})
106 class BookingListView(TemplateView):
107 template_name = "booking/booking_list.html"
109 def get_context_data(self, **kwargs):
110 bookings = Booking.objects.filter(end__gte=timezone.now())
111 title = 'Search Booking'
112 context = super(BookingListView, self).get_context_data(**kwargs)
113 context.update({'title': title, 'bookings': bookings})
117 class ResourceBookingsJSON(View):
118 def get(self, request, *args, **kwargs):
119 resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
120 bookings = resource.booking_set.get_queryset().values('id', 'start', 'end', 'purpose',
121 'jira_issue_status', 'opsys__name',
122 'installer__name', 'scenario__name')
123 return JsonResponse({'bookings': list(bookings)})