summaryrefslogtreecommitdiffstats
path: root/tools/pharos-dashboard/booking/models.py
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2016-08-19 17:10:31 +0200
committermaxbr <maxbr@mi.fu-berlin.de>2016-08-19 17:10:31 +0200
commit3b5ef3b0a88247eeafeee878de528aad71f9fd4b (patch)
tree8f08dcacdfd5b313f04a46406d0282cdde3670e5 /tools/pharos-dashboard/booking/models.py
parente556a63cc5f78598e890346889948765906a6411 (diff)
Split the dashboard into different apps, add tests
JIRA: RELENG-12 Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'tools/pharos-dashboard/booking/models.py')
-rw-r--r--tools/pharos-dashboard/booking/models.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/tools/pharos-dashboard/booking/models.py b/tools/pharos-dashboard/booking/models.py
new file mode 100644
index 00000000..719dd9bf
--- /dev/null
+++ b/tools/pharos-dashboard/booking/models.py
@@ -0,0 +1,53 @@
+from django.contrib.auth.models import User
+from django.db import models
+
+from dashboard.models import Resource
+
+
+class Booking(models.Model):
+ id = models.AutoField(primary_key=True)
+ user = models.ForeignKey(User, models.CASCADE) # delete if user is deleted
+ resource = models.ForeignKey(Resource, models.PROTECT)
+ start = models.DateTimeField()
+ end = models.DateTimeField()
+
+ purpose = models.CharField(max_length=300, blank=False)
+
+ class Meta:
+ db_table = 'booking'
+
+ def authorization_test(self):
+ """
+ Return True if self.user is authorized to make this booking.
+ """
+ user = self.user
+ # Check if User is troubleshooter / admin
+ if user.has_perm('booking.add_booking'):
+ return True
+ # Check if User owns this resource
+ if user in self.resource.owners.all():
+ return True
+ return False
+
+
+ def save(self, *args, **kwargs):
+ """
+ Save the booking if self.user is authorized and there is no overlapping booking.
+ Raise PermissionError if the user is not authorized
+ Raise ValueError if there is an overlapping booking
+ """
+ if not self.authorization_test():
+ raise PermissionError('Insufficient permissions to save this booking.')
+ if self.start >= self.end:
+ raise ValueError('Start date is after end date')
+ # conflicts end after booking starts, and start before booking ends
+ conflicting_dates = Booking.objects.filter(resource=self.resource)
+ conflicting_dates = conflicting_dates.filter(end__gt=self.start)
+ conflicting_dates = conflicting_dates.filter(start__lt=self.end)
+ if conflicting_dates.count() > 0:
+ raise ValueError('This booking overlaps with another booking')
+ return super(Booking, self).save(*args, **kwargs)
+
+
+ def __str__(self):
+ return str(self.resource) + ' from ' + str(self.start) + ' until ' + str(self.end)