Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 3 | from core.models import PlCoreBase |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 4 | #from core.models import Deployment |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 5 | from core.models import Tag |
| 6 | from django.contrib.contenttypes import generic |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 7 | from geoposition.fields import GeopositionField |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 8 | |
| 9 | class Site(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 10 | """ |
| 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 Mack | 85d1883 | 2013-05-09 17:02:31 -0400 | [diff] [blame] | 13 | tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 14 | 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 Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 17 | location = GeopositionField() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 18 | 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 Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 24 | #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites') |
| 25 | deployments = models.ManyToManyField('Deployment', through='SiteDeployments', blank=True) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 26 | tags = generic.GenericRelation(Tag) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 27 | |
| 28 | def __unicode__(self): return u'%s' % (self.name) |
| 29 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 30 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 31 | if user.is_readonly: |
| 32 | return False |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 33 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 37 | if site_priv.role.role == 'pi': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 38 | return True |
| 39 | return False |
| 40 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 41 | @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 Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 52 | class SiteRole(PlCoreBase): |
| 53 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 54 | ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 55 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 56 | |
| 57 | def __unicode__(self): return u'%s' % (self.role) |
| 58 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 59 | class SitePrivilege(PlCoreBase): |
| 60 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 61 | user = models.ForeignKey('User', related_name='site_privileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 62 | site = models.ForeignKey('Site', related_name='site_privileges') |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 63 | role = models.ForeignKey('SiteRole') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 64 | |
| 65 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 66 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 67 | def save(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 68 | super(SitePrivilege, self).save(*args, **kwds) |
| 69 | |
| 70 | def delete(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 71 | super(SitePrivilege, self).delete(*args, **kwds) |
| 72 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 73 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 74 | return self.site.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 75 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 76 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 85 | class 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 Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 91 | @staticmethod |
| 92 | def select_by_user(user): |
| 93 | return Deployment.objects.all() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 94 | |
| 95 | class 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 | |
| 102 | class 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 110 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 117 | if dpriv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 118 | return True |
| 119 | return False |
| 120 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 121 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 130 | class SiteDeployments(PlCoreBase): |
| 131 | site = models.ForeignKey(Site) |
| 132 | deployment = models.ForeignKey(Deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 133 | 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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 138 | |
Tony Mack | 929af70 | 2014-02-04 19:36:52 -0500 | [diff] [blame] | 139 | #class Meta: |
| 140 | # db_table = 'core_site_deployments' |
| 141 | # #auto_created = Site |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 142 | |