Scott Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 1 | import os |
Scott Baker | e444a00 | 2013-05-19 16:04:49 -0700 | [diff] [blame] | 2 | import datetime |
Scott Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 3 | from django.db import models |
| 4 | from core.models import PlCoreBase |
| 5 | from core.models import Sliver |
| 6 | from core.models import Slice |
| 7 | from core.models import ServiceResource |
| 8 | |
| 9 | # Create your models here. |
| 10 | |
| 11 | class Reservation(PlCoreBase): |
| 12 | startTime = models.DateTimeField() |
| 13 | slice = models.ForeignKey(Slice, related_name="reservations") |
| 14 | duration = models.IntegerField(default=1) |
| 15 | |
Scott Baker | fc7879e | 2013-05-19 16:19:48 -0700 | [diff] [blame] | 16 | def __unicode__(self): return u'%s to %s' % (self.startTime, self.endTime) |
Scott Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 17 | |
Scott Baker | e444a00 | 2013-05-19 16:04:49 -0700 | [diff] [blame] | 18 | @property |
| 19 | def endTime(self): |
| 20 | return self.startTime + datetime.timedelta(hours=self.duration) |
| 21 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 22 | def can_update(self, user): |
| 23 | return self.slice.can_update(user) |
| 24 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 25 | @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 Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 34 | class ReservedResource(PlCoreBase): |
Sapan Bhatia | 6bfa2ca | 2014-11-11 21:47:45 -0500 | [diff] [blame^] | 35 | sliver = models.ForeignKey(Sliver, related_name="reservedresources") |
| 36 | resource = models.ForeignKey(ServiceResource, related_name="reservedresources") |
Scott Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 37 | quantity = models.IntegerField(default=1) |
Sapan Bhatia | 6bfa2ca | 2014-11-11 21:47:45 -0500 | [diff] [blame^] | 38 | reservationSet = models.ForeignKey(Reservation, related_name="reservedresources") |
Scott Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 39 | |
| 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 45 | def can_update(self, user): |
| 46 | return self.sliver.slice.can_update(user) |
Scott Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 47 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 48 | @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 Baker | ea03c93 | 2013-05-17 09:06:44 -0700 | [diff] [blame] | 56 | |
| 57 | |