summaryrefslogtreecommitdiffstats
path: root/pharos-dashboard/booking
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2016-08-22 18:59:47 +0200
committermaxbr <maxbr@mi.fu-berlin.de>2016-08-22 19:04:19 +0200
commitf4e8248d126921603280dffab0e907366391bb72 (patch)
tree35b94e14b2e024bafbc8946b2fe50d8aa34e8357 /pharos-dashboard/booking
parentb426489bafaa4d6d09d1b6cefbbd9a17509090d9 (diff)
Create Jira issue for new booking
JIRA: RELENG-12 The issue is assigned to the lab owner and to the POD Access Request Component. The pgp and ssh keys are uploaded to jira as an attachement. Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'pharos-dashboard/booking')
-rw-r--r--pharos-dashboard/booking/models.py4
-rw-r--r--pharos-dashboard/booking/tests/test_models.py12
-rw-r--r--pharos-dashboard/booking/tests/test_views.py4
-rw-r--r--pharos-dashboard/booking/views.py44
4 files changed, 46 insertions, 18 deletions
diff --git a/pharos-dashboard/booking/models.py b/pharos-dashboard/booking/models.py
index 719dd9b..8011fa4 100644
--- a/pharos-dashboard/booking/models.py
+++ b/pharos-dashboard/booking/models.py
@@ -10,6 +10,7 @@ class Booking(models.Model):
resource = models.ForeignKey(Resource, models.PROTECT)
start = models.DateTimeField()
end = models.DateTimeField()
+ jira_issue_id = models.IntegerField(null=True)
purpose = models.CharField(max_length=300, blank=False)
@@ -25,11 +26,10 @@ class Booking(models.Model):
if user.has_perm('booking.add_booking'):
return True
# Check if User owns this resource
- if user in self.resource.owners.all():
+ if user == self.resource.owner:
return True
return False
-
def save(self, *args, **kwargs):
"""
Save the booking if self.user is authorized and there is no overlapping booking.
diff --git a/pharos-dashboard/booking/tests/test_models.py b/pharos-dashboard/booking/tests/test_models.py
index 00f6b26..7a572c5 100644
--- a/pharos-dashboard/booking/tests/test_models.py
+++ b/pharos-dashboard/booking/tests/test_models.py
@@ -4,6 +4,7 @@ from django.contrib.auth.models import User, Permission
from django.test import TestCase
from django.utils import timezone
+from account.models import UserProfile
from booking.models import Booking
from dashboard.models import Resource
from jenkins.models import JenkinsSlave
@@ -12,9 +13,12 @@ from jenkins.models import JenkinsSlave
class BookingModelTestCase(TestCase):
def setUp(self):
self.slave = JenkinsSlave.objects.create(name='test', url='test')
+ self.owner = User.objects.create(username='owner')
- self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x', url='x')
- self.res2 = Resource.objects.create(name='res2', slave=self.slave, description='x', url='x')
+ self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x',
+ url='x',owner=self.owner)
+ self.res2 = Resource.objects.create(name='res2', slave=self.slave, description='x',
+ url='x',owner=self.owner)
self.user1 = User.objects.create(username='user1')
@@ -78,12 +82,14 @@ class BookingModelTestCase(TestCase):
def test_authorization(self):
user = User.objects.create(username='user')
+ user.userprofile = UserProfile.objects.create(user=user)
self.assertRaises(PermissionError, Booking.objects.create, start=timezone.now(),
end=timezone.now() + timedelta(days=1), resource=self.res1, user=user)
- self.res1.owners.add(user)
+ self.res1.owner = user
self.assertTrue(
Booking.objects.create(start=timezone.now(), end=timezone.now() + timedelta(days=1),
resource=self.res1, user=user))
+ self.res1.owner = self.owner
user.user_permissions.add(self.add_booking_perm)
user = User.objects.get(pk=user.id)
self.assertTrue(
diff --git a/pharos-dashboard/booking/tests/test_views.py b/pharos-dashboard/booking/tests/test_views.py
index b0c4b49..c5dff58 100644
--- a/pharos-dashboard/booking/tests/test_views.py
+++ b/pharos-dashboard/booking/tests/test_views.py
@@ -19,7 +19,9 @@ class BookingViewTestCase(TestCase):
def setUp(self):
self.client = Client()
self.slave = JenkinsSlave.objects.create(name='test', url='test')
- self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x', url='x')
+ self.owner = User.objects.create(username='owner')
+ self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x',
+ url='x',owner=self.owner)
self.user1 = User.objects.create(username='user1')
self.user1.set_password('user1')
self.user1profile = UserProfile.objects.create(user=self.user1)
diff --git a/pharos-dashboard/booking/views.py b/pharos-dashboard/booking/views.py
index c461aef..c2f437f 100644
--- a/pharos-dashboard/booking/views.py
+++ b/pharos-dashboard/booking/views.py
@@ -5,6 +5,8 @@ from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views import View
from django.views.generic import FormView
+from django.shortcuts import redirect
+from jira import JIRAError
from account.jira_util import get_jira
from booking.forms import BookingForm
@@ -12,20 +14,26 @@ from booking.models import Booking
from dashboard.models import Resource
+def create_jira_ticket(user, booking):
+ jira = get_jira(user)
+ issue_dict = {
+ 'project': 'PHAROS',
+ 'summary': str(booking.resource) + ': Access Request',
+ 'description': booking.purpose,
+ 'issuetype': {'name': 'Task'},
+ 'components': [{'name': 'POD Access Request'}],
+ 'assignee': {'name': booking.resource.owner.username}
+ }
+ issue = jira.create_issue(fields=issue_dict)
+ jira.add_attachment(issue, user.userprofile.pgp_public_key)
+ jira.add_attachment(issue, user.userprofile.ssh_public_key)
+ booking.jira_issue_id = issue.id
+
+
class BookingFormView(LoginRequiredMixin, FormView):
template_name = "booking/booking_calendar.html"
form_class = BookingForm
- def open_jira_issue(self,booking):
- jira = get_jira(self.request.user)
- issue_dict = {
- 'project': 'PHAROS',
- 'summary': 'Booking: ' + str(self.resource),
- 'description': str(booking),
- 'issuetype': {'name': 'Task'},
- }
- jira.create_issue(fields=issue_dict)
-
def dispatch(self, request, *args, **kwargs):
self.resource = get_object_or_404(Resource, id=self.kwargs['resource_id'])
return super(BookingFormView, self).dispatch(request, *args, **kwargs)
@@ -40,9 +48,14 @@ class BookingFormView(LoginRequiredMixin, FormView):
return reverse('booking:create', kwargs=self.kwargs)
def form_valid(self, form):
+ user = self.request.user
+ if not user.userprofile.ssh_public_key or not user.userprofile.pgp_public_key:
+ messages.add_message(self.request, messages.INFO,
+ 'Please upload your private keys before booking')
+ return redirect('account:settings')
booking = Booking(start=form.cleaned_data['start'], end=form.cleaned_data['end'],
purpose=form.cleaned_data['purpose'], resource=self.resource,
- user=self.request.user)
+ user=user)
try:
booking.save()
except ValueError as err:
@@ -51,7 +64,14 @@ class BookingFormView(LoginRequiredMixin, FormView):
except PermissionError as err:
messages.add_message(self.request, messages.ERROR, err)
return super(BookingFormView, self).form_invalid(form)
- self.open_jira_issue(booking)
+ try:
+ create_jira_ticket(user, booking)
+ except JIRAError:
+ messages.add_message(self.request, messages.ERROR, 'Failed to create Jira Ticket. '
+ 'Please check your Jira '
+ 'permissions.')
+ booking.delete()
+ return super(BookingFormView, self).form_invalid(form)
messages.add_message(self.request, messages.SUCCESS, 'Booking saved')
return super(BookingFormView, self).form_valid(form)