blob: 533165f7e09bc2702f0237e5717685fb88b82276 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Siobhan Tully30fd4292013-05-10 08:59:56 -04003from core.models import PlCoreBase
4from core.models import Site
5from core.models import User
6from core.models import Role
Siobhan Tullybf1153a2013-05-27 20:53:48 -04007from core.models import Deployment
Scott Bakere8d596f2013-05-13 23:17:13 -07008from core.models import ServiceClass
Siobhan Tullyde5450d2013-06-21 11:35:33 -04009from core.models import Tag
10from django.contrib.contenttypes import generic
Siobhan Tullyce652d02013-10-08 21:52:35 -040011from core.models import Service
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012
13# Create your models here.
14
15class Slice(PlCoreBase):
Tony Mackcdec0902013-04-15 00:38:49 -040016 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack48952032013-04-12 11:49:34 -040017 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040019 omf_friendly = models.BooleanField()
20 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
21 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack360afb82013-12-17 13:32:45 -050022 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
Tony Mack85d18832013-05-09 17:02:31 -040023 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
24 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
25 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
Siobhan Tullyce652d02013-10-08 21:52:35 -040026 service = models.ForeignKey(Service, related_name='service', null=True, blank=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040027
Siobhan Tullyde5450d2013-06-21 11:35:33 -040028 tags = generic.GenericRelation(Tag)
29
Scott Bakere8d596f2013-05-13 23:17:13 -070030 serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=ServiceClass.get_default)
Tony Mack2bd5b412013-06-11 21:05:06 -040031 creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)
Siobhan Tully231f4c82013-05-02 05:47:24 -040032
Siobhan Tully4bc09f22013-04-10 21:15:21 -040033 def __unicode__(self): return u'%s' % (self.name)
34
Tony Mack62bc59a2013-04-14 23:27:12 -040035 def save(self, *args, **kwds):
Scott Bakere8d596f2013-05-13 23:17:13 -070036 if self.serviceClass is None:
37 # We allowed None=True for serviceClass because Django evolution
38 # will fail unless it is allowed. But, we we really don't want it to
39 # ever save None, so fix it up here.
40 self.serviceClass = ServiceClass.get_default()
Tony Mack2bd5b412013-06-11 21:05:06 -040041 if not self.creator and hasattr(self, 'caller'):
42 self.creator = self.caller
Tony Mack62bc59a2013-04-14 23:27:12 -040043 super(Slice, self).save(*args, **kwds)
44
Tony Mack5b061472014-02-04 07:57:10 -050045 def can_update(self, user):
46 if user.is_readonly:
47 return False
48 if user.is_admin:
49 return True
50 slice_privs = SlicePrivilege.objects.filter(user=user, slice=self)
51 for slice_priv in slice_privs:
52 if slice_priv.role.role_type == 'admin':
53 return True
54 return False
55
56 def save_by_user(self, user, *args, **kwds):
57 if self.can_update(user):
58 super(Slice, self).save(*args, **kwds)
59
60
61 @staticmethod
62 def select_by_user(user):
63 if user.is_admin:
64 qs = Slice.objects.all()
65 else:
66 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
67 qs = Slice.objects.filter(id__in=slice_ids)
68 return qs
69
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040070class SliceRole(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050071 ROLE_CHOICES = (('admin','Admin'),('default','Default'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040072
73 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
74
75 def __unicode__(self): return u'%s' % (self.role)
76
77class SlicePrivilege(PlCoreBase):
78 user = models.ForeignKey('User', related_name='slice_privileges')
79 slice = models.ForeignKey('Slice', related_name='slice_privileges')
80 role = models.ForeignKey('SliceRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040081
82 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
Tony Mack5b061472014-02-04 07:57:10 -050083
84 def can_update(self, user):
85 if user.is_admin:
86 return True
87 slice_privs = SlicePrivilege.objects.filter(user=user, slice=self)
88 for slice_priv in slice_privs:
89 if slice_priv.role.role_type == 'admin':
90 return True
91 return False
92
93 def save_by_user(self, user, *args, **kwds):
94 if self.can_update(user):
95 super(SlicePrivilege, self).save(*args, **kwds)
96
97 @staticmethod
98 def select_by_user(user):
99 if user.is_admin:
100 qs = SlicePrivilege.objects.all()
101 else:
102 sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
103 qs = SlicePrivilege.objects.filter(id__in=sp_ids)
104 return qs