Add Installer and Scenario fields to bookings
[pharos.git] / tools / pharos-dashboard / src / booking / models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
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 ##############################################################################
9
10
11 from django.contrib.auth.models import User
12 from django.db import models
13 from jira import JIRA
14 from jira import JIRAError
15
16 from dashboard.models import Resource
17 from django.conf import settings
18
19 class Installer(models.Model):
20     id = models.AutoField(primary_key=True)
21     name = models.CharField(max_length=30)
22
23     def __str__(self):
24         return self.name
25
26 class Scenario(models.Model):
27     id = models.AutoField(primary_key=True)
28     name = models.CharField(max_length=300)
29
30     def __str__(self):
31         return self.name
32
33
34 class Booking(models.Model):
35     id = models.AutoField(primary_key=True)
36     user = models.ForeignKey(User, models.CASCADE)  # delete if user is deleted
37     resource = models.ForeignKey(Resource, models.PROTECT)
38     start = models.DateTimeField()
39     end = models.DateTimeField()
40     jira_issue_id = models.IntegerField(null=True)
41     jira_issue_status = models.CharField(max_length=50)
42
43     installer = models.ForeignKey(Installer, models.DO_NOTHING, null=True)
44     scenario = models.ForeignKey(Scenario, models.DO_NOTHING, null=True)
45     purpose = models.CharField(max_length=300, blank=False)
46
47     class Meta:
48         db_table = 'booking'
49
50     def get_jira_issue(self):
51         try:
52             jira = JIRA(server=settings.JIRA_URL,
53                         basic_auth=(settings.JIRA_USER_NAME, settings.JIRA_USER_PASSWORD))
54             issue = jira.issue(self.jira_issue_id)
55             return issue
56         except JIRAError:
57             return None
58
59     def save(self, *args, **kwargs):
60         """
61         Save the booking if self.user is authorized and there is no overlapping booking.
62         Raise PermissionError if the user is not authorized
63         Raise ValueError if there is an overlapping booking
64         """
65         if self.start >= self.end:
66             raise ValueError('Start date is after end date')
67         # conflicts end after booking starts, and start before booking ends
68         conflicting_dates = Booking.objects.filter(resource=self.resource).exclude(id=self.id)
69         conflicting_dates = conflicting_dates.filter(end__gt=self.start)
70         conflicting_dates = conflicting_dates.filter(start__lt=self.end)
71         if conflicting_dates.count() > 0:
72             raise ValueError('This booking overlaps with another booking')
73         return super(Booking, self).save(*args, **kwargs)
74
75     def __str__(self):
76         return str(self.resource) + ' from ' + str(self.start) + ' until ' + str(self.end)