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, 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
19 class Booking(models.Model):
20 id = models.AutoField(primary_key=True)
21 # All bookings are owned by the user who requested it
22 owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='owner')
23 # an owner can add other users to the booking
24 collaborators = models.ManyToManyField(User, blank=True, related_name='collaborators')
26 start = models.DateTimeField()
27 end = models.DateTimeField()
28 reset = models.BooleanField(default=False)
29 jira_issue_id = models.IntegerField(null=True, blank=True)
30 jira_issue_status = models.CharField(max_length=50, blank=True)
31 purpose = models.CharField(max_length=300, blank=False)
32 # bookings can be extended a limited number of times
33 ext_count = models.IntegerField(default=2)
34 # the hardware that the user has booked
35 resource = models.ForeignKey(ResourceBundle, on_delete=models.SET_NULL, null=True, blank=True)
36 opnfv_config = models.ForeignKey(OPNFVConfig, on_delete=models.SET_NULL, null=True, blank=True)
37 project = models.CharField(max_length=100, default="", blank=True, null=True)
38 lab = models.ForeignKey(Lab, null=True, on_delete=models.SET_NULL)
39 pdf = models.TextField(blank=True, default="")
40 idf = models.TextField(blank=True, default="")
42 complete = models.BooleanField(default=False)
47 def save(self, *args, **kwargs):
49 Save the booking if self.user is authorized and there is no overlapping booking.
51 Raise PermissionError if the user is not authorized
52 Raise ValueError if there is an overlapping booking
54 if self.start >= self.end:
55 raise ValueError('Start date is after end date')
56 # conflicts end after booking starts, and start before booking ends
57 conflicting_dates = Booking.objects.filter(resource=self.resource).exclude(id=self.id)
58 conflicting_dates = conflicting_dates.filter(end__gt=self.start)
59 conflicting_dates = conflicting_dates.filter(start__lt=self.end)
60 if conflicting_dates.count() > 0:
61 raise ValueError('This booking overlaps with another booking')
62 return super(Booking, self).save(*args, **kwargs)
64 def delete(self, *args, **kwargs):
68 resource_inventory.resource_manager.ResourceManager.getInstance().deleteResourceBundle(res)
69 return super(self.__class__, self).delete(*args, **kwargs)
72 return str(self.purpose) + ' from ' + str(self.start) + ' until ' + str(self.end)