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