diff options
Diffstat (limited to 'tools/pharos-dashboard/booking')
7 files changed, 46 insertions, 90 deletions
diff --git a/tools/pharos-dashboard/booking/migrations/0001_initial.py b/tools/pharos-dashboard/booking/migrations/0001_initial.py deleted file mode 100644 index 9706b812..00000000 --- a/tools/pharos-dashboard/booking/migrations/0001_initial.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10 on 2016-08-15 12:19 -from __future__ import unicode_literals - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ('dashboard', '0001_initial'), - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ] - - operations = [ - migrations.CreateModel( - name='Booking', - fields=[ - ('id', models.AutoField(primary_key=True, serialize=False)), - ('start', models.DateTimeField()), - ('end', models.DateTimeField()), - ('purpose', models.CharField(max_length=300)), - ('resource', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='dashboard.Resource')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - options={ - 'db_table': 'booking', - }, - ), - ] diff --git a/tools/pharos-dashboard/booking/migrations/0002_remove_booking_deleted.py b/tools/pharos-dashboard/booking/migrations/0002_remove_booking_deleted.py deleted file mode 100644 index 335379d5..00000000 --- a/tools/pharos-dashboard/booking/migrations/0002_remove_booking_deleted.py +++ /dev/null @@ -1,19 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10 on 2016-08-13 12:50 -from __future__ import unicode_literals - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('booking', '0001_initial'), - ] - - operations = [ - migrations.RemoveField( - model_name='booking', - name='deleted', - ), - ] diff --git a/tools/pharos-dashboard/booking/migrations/0003_remove_booking_status.py b/tools/pharos-dashboard/booking/migrations/0003_remove_booking_status.py deleted file mode 100644 index 95089a7a..00000000 --- a/tools/pharos-dashboard/booking/migrations/0003_remove_booking_status.py +++ /dev/null @@ -1,19 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10 on 2016-08-13 12:51 -from __future__ import unicode_literals - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('booking', '0002_remove_booking_deleted'), - ] - - operations = [ - migrations.RemoveField( - model_name='booking', - name='status', - ), - ] diff --git a/tools/pharos-dashboard/booking/models.py b/tools/pharos-dashboard/booking/models.py index 719dd9bf..8011fa41 100644 --- a/tools/pharos-dashboard/booking/models.py +++ b/tools/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/tools/pharos-dashboard/booking/tests/test_models.py b/tools/pharos-dashboard/booking/tests/test_models.py index 00f6b266..7a572c5f 100644 --- a/tools/pharos-dashboard/booking/tests/test_models.py +++ b/tools/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/tools/pharos-dashboard/booking/tests/test_views.py b/tools/pharos-dashboard/booking/tests/test_views.py index b0c4b498..c5dff586 100644 --- a/tools/pharos-dashboard/booking/tests/test_views.py +++ b/tools/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/tools/pharos-dashboard/booking/views.py b/tools/pharos-dashboard/booking/views.py index c461aef4..c2f437f3 100644 --- a/tools/pharos-dashboard/booking/views.py +++ b/tools/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) |