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.auth.models import User
13 from django.db import models
15 from jira import JIRAError
16 from django.utils.crypto import get_random_string
19 from dashboard.models import Resource
22 class Installer(models.Model):
23 id = models.AutoField(primary_key=True)
24 name = models.CharField(max_length=30)
29 class Scenario(models.Model):
30 id = models.AutoField(primary_key=True)
31 name = models.CharField(max_length=300)
36 class Opsys(models.Model):
37 id = models.AutoField(primary_key=True)
38 name = models.CharField(max_length=100)
43 class Booking(models.Model):
44 id = models.AutoField(primary_key=True)
45 changeid = models.TextField(default='initial', blank=True, null=True)
46 user = models.ForeignKey(User, models.CASCADE) # delete if user is deleted
47 resource = models.ForeignKey(Resource, models.PROTECT)
48 start = models.DateTimeField()
49 end = models.DateTimeField()
50 reset = models.BooleanField(default=False)
51 jira_issue_id = models.IntegerField(null=True)
52 jira_issue_status = models.CharField(max_length=50)
54 opsys = models.ForeignKey(Opsys, models.DO_NOTHING, null=True)
55 installer = models.ForeignKey(Installer, models.DO_NOTHING, null=True)
56 scenario = models.ForeignKey(Scenario, models.DO_NOTHING, null=True)
57 purpose = models.CharField(max_length=300, blank=False)
58 ext_count = models.IntegerField(default=2)
63 def get_jira_issue(self):
65 jira = JIRA(server=settings.JIRA_URL,
66 basic_auth=(settings.JIRA_USER_NAME, settings.JIRA_USER_PASSWORD))
67 issue = jira.issue(self.jira_issue_id)
72 def save(self, *args, **kwargs):
74 Save the booking if self.user is authorized and there is no overlapping booking.
75 Raise PermissionError if the user is not authorized
76 Raise ValueError if there is an overlapping booking
78 if self.start >= self.end:
79 raise ValueError('Start date is after end date')
80 # conflicts end after booking starts, and start before booking ends
81 conflicting_dates = Booking.objects.filter(resource=self.resource).exclude(id=self.id)
82 conflicting_dates = conflicting_dates.filter(end__gt=self.start)
83 conflicting_dates = conflicting_dates.filter(start__lt=self.end)
84 if conflicting_dates.count() > 0:
85 raise ValueError('This booking overlaps with another booking')
87 self.changeid = self.id
89 self.changeid = hashlib.md5(self.changeid.encode() + get_random_string(length=32).encode()).hexdigest()
90 return super(Booking, self).save(*args, **kwargs)
93 return str(self.resource) + ' from ' + str(self.start) + ' until ' + str(self.end)