blob: 4256e7513d628bef4d6c9f1dc0b14339891aa66f [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
Siobhan Tullycf04fb62014-01-11 11:25:57 -05004#from core.models import Deployment
Siobhan Tullyde5450d2013-06-21 11:35:33 -04005from core.models import Tag
6from django.contrib.contenttypes import generic
Siobhan Tully567e3e62013-06-21 18:03:16 -04007from geoposition.fields import GeopositionField
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008
9class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050010 """
11 A logical grouping of Nodes that are co-located at the same geographic location, which also typically corresponds to the Nodes' location in the physical network.
12 """
Tony Mack85d18832013-05-09 17:02:31 -040013 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014 name = models.CharField(max_length=200, help_text="Name for this Site")
15 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
16 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -040017 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018 longitude = models.FloatField(null=True, blank=True)
19 latitude = models.FloatField(null=True, blank=True)
20 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
21 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
22 abbreviated_name = models.CharField(max_length=80)
23
Tony Macke4be32f2014-03-11 20:45:25 -040024 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
25 deployments = models.ManyToManyField('Deployment', through='SiteDeployments', blank=True)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040026 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040027
28 def __unicode__(self): return u'%s' % (self.name)
29
Tony Mack5b061472014-02-04 07:57:10 -050030 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050031 if user.is_readonly:
32 return False
Tony Mack5b061472014-02-04 07:57:10 -050033 if user.is_admin:
34 return True
35 site_privs = SitePrivilege.objects.filter(user=user, site=self)
36 for site_priv in site_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -050037 if site_priv.role.role == 'pi':
Tony Mack5b061472014-02-04 07:57:10 -050038 return True
39 return False
40
Tony Mack5b061472014-02-04 07:57:10 -050041 @staticmethod
42 def select_by_user(user):
43 if user.is_admin:
44 qs = Site.objects.all()
45 else:
46 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
47 site_ids.append(user.site.id)
48 qs = Site.objects.filter(id__in=site_ids)
49 return qs
50
51
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040052class SiteRole(PlCoreBase):
53
Siobhan Tullycf04fb62014-01-11 11:25:57 -050054 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040055 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
56
57 def __unicode__(self): return u'%s' % (self.role)
58
Siobhan Tully4bc09f22013-04-10 21:15:21 -040059class SitePrivilege(PlCoreBase):
60
Siobhan Tully30fd4292013-05-10 08:59:56 -040061 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040062 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040063 role = models.ForeignKey('SiteRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040064
65 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
66
Tony Mack00d361f2013-04-28 10:28:42 -040067 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -040068 super(SitePrivilege, self).save(*args, **kwds)
69
70 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -040071 super(SitePrivilege, self).delete(*args, **kwds)
72
Tony Mack5b061472014-02-04 07:57:10 -050073 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050074 return self.site.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -050075
Tony Mack5b061472014-02-04 07:57:10 -050076 @staticmethod
77 def select_by_user(user):
78 if user.is_admin:
79 qs = SitePrivilege.objects.all()
80 else:
81 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
82 qs = SitePrivilege.objects.filter(id__in=sp_ids)
83 return qs
84
Siobhan Tullycf04fb62014-01-11 11:25:57 -050085class Deployment(PlCoreBase):
86 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
87 #sites = models.ManyToManyField('Site', through='SiteDeployments', blank=True)
88
89 def __unicode__(self): return u'%s' % (self.name)
90
Tony Macke4be32f2014-03-11 20:45:25 -040091 @staticmethod
92 def select_by_user(user):
93 return Deployment.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -050094
95class DeploymentRole(PlCoreBase):
96
97 ROLE_CHOICES = (('admin','Admin'),)
98 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
99
100 def __unicode__(self): return u'%s' % (self.role)
101
102class DeploymentPrivilege(PlCoreBase):
103
104 user = models.ForeignKey('User', related_name='deployment_privileges')
105 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
106 role = models.ForeignKey('DeploymentRole')
107
108 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
109
Tony Mack5b061472014-02-04 07:57:10 -0500110 def can_update(self, user):
111 if user.is_readonly:
112 return False
113 if user.is_admin:
114 return True
115 dprivs = DeploymentPrivilege.objects.filter(user=user)
116 for dpriv in dprivs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500117 if dpriv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -0500118 return True
119 return False
120
Tony Mack5b061472014-02-04 07:57:10 -0500121 @staticmethod
122 def select_by_user(user):
123 if user.is_admin:
124 qs = DeploymentPrivilege.objects.all()
125 else:
126 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
127 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
128 return qs
129
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500130class SiteDeployments(PlCoreBase):
131 site = models.ForeignKey(Site)
132 deployment = models.ForeignKey(Deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400133 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
134
135 @staticmethod
136 def select_by_user(user):
137 return SiteDeployments.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500138
Tony Mack929af702014-02-04 19:36:52 -0500139 #class Meta:
140 # db_table = 'core_site_deployments'
141 # #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400142