Implement OPNFV workflow
[pharos-tools.git] / dashboard / src / booking / models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
4 #
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 ##############################################################################
10
11
12 from resource_inventory.models import ResourceBundle, ConfigBundle, OPNFVConfig
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
17
18
19 class Booking(models.Model):
20     id = models.AutoField(primary_key=True)
21     owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='owner')
22     collaborators = models.ManyToManyField(User, related_name='collaborators')
23     start = models.DateTimeField()
24     end = models.DateTimeField()
25     reset = models.BooleanField(default=False)
26     jira_issue_id = models.IntegerField(null=True, blank=True)
27     jira_issue_status = models.CharField(max_length=50, blank=True)
28     purpose = models.CharField(max_length=300, blank=False)
29     ext_count = models.IntegerField(default=2)
30     resource = models.ForeignKey(ResourceBundle, on_delete=models.SET_NULL, null=True)
31     config_bundle = models.ForeignKey(ConfigBundle, on_delete=models.SET_NULL, null=True)
32     opnfv_config = models.ForeignKey(OPNFVConfig, on_delete=models.SET_NULL, null=True)
33     project = models.CharField(max_length=100, default="", blank=True, null=True)
34     lab = models.ForeignKey(Lab, null=True, on_delete=models.SET_NULL)
35     pdf = models.TextField(blank=True, default="")
36     idf = models.TextField(blank=True, default="")
37
38     class Meta:
39         db_table = 'booking'
40
41     def save(self, *args, **kwargs):
42         """
43         Save the booking if self.user is authorized and there is no overlapping booking.
44         Raise PermissionError if the user is not authorized
45         Raise ValueError if there is an overlapping booking
46         """
47         if self.start >= self.end:
48             raise ValueError('Start date is after end date')
49         # conflicts end after booking starts, and start before booking ends
50         conflicting_dates = Booking.objects.filter(resource=self.resource).exclude(id=self.id)
51         conflicting_dates = conflicting_dates.filter(end__gt=self.start)
52         conflicting_dates = conflicting_dates.filter(start__lt=self.end)
53         if conflicting_dates.count() > 0:
54             raise ValueError('This booking overlaps with another booking')
55         return super(Booking, self).save(*args, **kwargs)
56
57     def delete(self, *args, **kwargs):
58         res = self.resource
59         self.resource = None
60         self.save()
61         resource_inventory.resource_manager.ResourceManager.getInstance().deleteResourceBundle(res)
62         return super(self.__class__, self).delete(*args, **kwargs)
63
64     def __str__(self):
65         return str(self.purpose) + ' from ' + str(self.start) + ' until ' + str(self.end)