1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
12 from resource_inventory.models import ResourceBundle, ConfigBundle
13 from account.models import Lab
14 from django.contrib.auth.models import User
15 from django.db import models
16 import resource_inventory.resource_manager
19 class Scenario(models.Model):
20 id = models.AutoField(primary_key=True)
21 name = models.CharField(max_length=300)
27 class Installer(models.Model):
28 id = models.AutoField(primary_key=True)
29 name = models.CharField(max_length=30)
30 sup_scenarios = models.ManyToManyField(Scenario, blank=True)
36 class Opsys(models.Model):
37 id = models.AutoField(primary_key=True)
38 name = models.CharField(max_length=100)
39 sup_installers = models.ManyToManyField(Installer, blank=True)
45 class Booking(models.Model):
46 id = models.AutoField(primary_key=True)
47 owner = models.ForeignKey(User, models.CASCADE, related_name='owner')
48 collaborators = models.ManyToManyField(User, related_name='collaborators')
49 start = models.DateTimeField()
50 end = models.DateTimeField()
51 reset = models.BooleanField(default=False)
52 jira_issue_id = models.IntegerField(null=True, blank=True)
53 jira_issue_status = models.CharField(max_length=50, blank=True)
54 purpose = models.CharField(max_length=300, blank=False)
55 ext_count = models.IntegerField(default=2)
56 resource = models.ForeignKey(ResourceBundle, on_delete=models.SET_NULL, null=True)
57 config_bundle = models.ForeignKey(ConfigBundle, on_delete=models.SET_NULL, null=True)
58 project = models.CharField(max_length=100, default="", blank=True, null=True)
59 lab = models.ForeignKey(Lab, null=True, on_delete=models.SET_NULL)
60 pdf = models.TextField(blank=True, default="")
65 def save(self, *args, **kwargs):
67 Save the booking if self.user is authorized and there is no overlapping booking.
68 Raise PermissionError if the user is not authorized
69 Raise ValueError if there is an overlapping booking
71 if self.start >= self.end:
72 raise ValueError('Start date is after end date')
73 # conflicts end after booking starts, and start before booking ends
74 conflicting_dates = Booking.objects.filter(resource=self.resource).exclude(id=self.id)
75 conflicting_dates = conflicting_dates.filter(end__gt=self.start)
76 conflicting_dates = conflicting_dates.filter(start__lt=self.end)
77 if conflicting_dates.count() > 0:
78 raise ValueError('This booking overlaps with another booking')
79 return super(Booking, self).save(*args, **kwargs)
81 def delete(self, *args, **kwargs):
85 resource_inventory.resource_manager.ResourceManager.getInstance().deleteResourceBundle(res)
86 return super(self.__class__, self).delete(*args, **kwargs)
89 return str(self.purpose) + ' from ' + str(self.start) + ' until ' + str(self.end)