blob: 4dd127404e423cc3c757f19815134ab0dc215ab0 [file] [log] [blame]
Scott Bakerea03c932013-05-17 09:06:44 -07001import os
Scott Bakere444a002013-05-19 16:04:49 -07002import datetime
Scott Bakerea03c932013-05-17 09:06:44 -07003from django.db import models
4from core.models import PlCoreBase
5from core.models import Sliver
6from core.models import Slice
7from core.models import ServiceResource
8
9# Create your models here.
10
11class Reservation(PlCoreBase):
12 startTime = models.DateTimeField()
13 slice = models.ForeignKey(Slice, related_name="reservations")
14 duration = models.IntegerField(default=1)
15
Scott Bakerfc7879e2013-05-19 16:19:48 -070016 def __unicode__(self): return u'%s to %s' % (self.startTime, self.endTime)
Scott Bakerea03c932013-05-17 09:06:44 -070017
Scott Bakere444a002013-05-19 16:04:49 -070018 @property
19 def endTime(self):
20 return self.startTime + datetime.timedelta(hours=self.duration)
21
Tony Mack5b061472014-02-04 07:57:10 -050022 def can_update(self, user):
23 return self.slice.can_update(user)
24
Tony Mack5b061472014-02-04 07:57:10 -050025 @staticmethod
26 def select_by_user(user):
27 if user.is_admin:
28 qs = Reservation.objects.all()
29 else:
30 slice_ids = [s.id for s in Slice.select_by_user(user)]
31 qs = Reservation.objects.filter(id__in=slice_ids)
32 return qs
33
Scott Bakerea03c932013-05-17 09:06:44 -070034class ReservedResource(PlCoreBase):
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -050035 sliver = models.ForeignKey(Sliver, related_name="reservedresources")
36 resource = models.ForeignKey(ServiceResource, related_name="reservedresources")
Scott Bakerea03c932013-05-17 09:06:44 -070037 quantity = models.IntegerField(default=1)
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -050038 reservationSet = models.ForeignKey(Reservation, related_name="reservedresources")
Scott Bakerea03c932013-05-17 09:06:44 -070039
40 class Meta(PlCoreBase.Meta):
41 verbose_name_plural = "Reserved Resources"
42
43 def __unicode__(self): return u'%d %s on %s' % (self.quantity, self.resource, self.sliver)
44
Tony Mack5b061472014-02-04 07:57:10 -050045 def can_update(self, user):
46 return self.sliver.slice.can_update(user)
Scott Bakerea03c932013-05-17 09:06:44 -070047
Tony Mack5b061472014-02-04 07:57:10 -050048 @staticmethod
49 def select_by_user(user):
50 if user.is_admin:
51 qs = ReservedResource.objects.all()
52 else:
53 sliver_ids = [s.id for s in Sliver.select_by_user(user)]
54 qs = ReservedResource.objects.filter(id__in=sliver_ids)
55 return qs
Scott Bakerea03c932013-05-17 09:06:44 -070056
57