blob: 34699d84298423262f883fd4dd1e5ae71a683370 [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
Siobhan Tully320b4622014-01-17 15:11:14 -050024 deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Siobhan Tullycf04fb62014-01-11 11:25:57 -050025 #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):
31 if user.is_admin:
32 return True
33 site_privs = SitePrivilege.objects.filter(user=user, site=self)
34 for site_priv in site_privs:
35 if site_priv.role.role_type == 'pi':
36 return True
37 return False
38
Tony Mack5b061472014-02-04 07:57:10 -050039 @staticmethod
40 def select_by_user(user):
41 if user.is_admin:
42 qs = Site.objects.all()
43 else:
44 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
45 site_ids.append(user.site.id)
46 qs = Site.objects.filter(id__in=site_ids)
47 return qs
48
49
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040050class SiteRole(PlCoreBase):
51
Siobhan Tullycf04fb62014-01-11 11:25:57 -050052 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040053 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
54
55 def __unicode__(self): return u'%s' % (self.role)
56
Siobhan Tully4bc09f22013-04-10 21:15:21 -040057class SitePrivilege(PlCoreBase):
58
Siobhan Tully30fd4292013-05-10 08:59:56 -040059 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040060 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040061 role = models.ForeignKey('SiteRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040062
63 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
64
Tony Mack00d361f2013-04-28 10:28:42 -040065 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -040066 super(SitePrivilege, self).save(*args, **kwds)
67
68 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -040069 super(SitePrivilege, self).delete(*args, **kwds)
70
Tony Mack5b061472014-02-04 07:57:10 -050071 def can_update(self, user):
72 if user.is_admin:
73 return True
74 site_privs = SitePrivilege.objects.filter(user=user, site=self)
75 for site_priv in site_privs:
76 if site_priv.role.role_type == 'pi':
77 return True
78 return False
79
Tony Mack5b061472014-02-04 07:57:10 -050080 @staticmethod
81 def select_by_user(user):
82 if user.is_admin:
83 qs = SitePrivilege.objects.all()
84 else:
85 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
86 qs = SitePrivilege.objects.filter(id__in=sp_ids)
87 return qs
88
Siobhan Tullycf04fb62014-01-11 11:25:57 -050089class Deployment(PlCoreBase):
90 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
91 #sites = models.ManyToManyField('Site', through='SiteDeployments', blank=True)
92
93 def __unicode__(self): return u'%s' % (self.name)
94
95
96class DeploymentRole(PlCoreBase):
97
98 ROLE_CHOICES = (('admin','Admin'),)
99 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
100
101 def __unicode__(self): return u'%s' % (self.role)
102
103class DeploymentPrivilege(PlCoreBase):
104
105 user = models.ForeignKey('User', related_name='deployment_privileges')
106 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
107 role = models.ForeignKey('DeploymentRole')
108
109 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
110
Tony Mack5b061472014-02-04 07:57:10 -0500111 def can_update(self, user):
112 if user.is_readonly:
113 return False
114 if user.is_admin:
115 return True
116 dprivs = DeploymentPrivilege.objects.filter(user=user)
117 for dpriv in dprivs:
118 if dpriv.role.role_type == 'admin':
119 return True
120 return False
121
Tony Mack5b061472014-02-04 07:57:10 -0500122 @staticmethod
123 def select_by_user(user):
124 if user.is_admin:
125 qs = DeploymentPrivilege.objects.all()
126 else:
127 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
128 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
129 return qs
130
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500131class SiteDeployments(PlCoreBase):
132 site = models.ForeignKey(Site)
133 deployment = models.ForeignKey(Deployment)
134
135 class Meta:
136 db_table = 'site_deployments'
137 #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400138