Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
| 3 | from plstackapi.core.models import PlCoreBase |
| 4 | from plstackapi.core.models import DeploymentNetwork |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 5 | |
| 6 | |
| 7 | class Site(PlCoreBase): |
| 8 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 9 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 10 | name = models.CharField(max_length=200, help_text="Name for this Site") |
| 11 | site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page") |
| 12 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 13 | longitude = models.FloatField(null=True, blank=True) |
| 14 | latitude = models.FloatField(null=True, blank=True) |
| 15 | login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site") |
| 16 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 17 | abbreviated_name = models.CharField(max_length=80) |
| 18 | |
| 19 | deployments = models.ManyToManyField(DeploymentNetwork, blank=True, related_name='sites') |
| 20 | |
| 21 | def __unicode__(self): return u'%s' % (self.name) |
| 22 | |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 23 | def save(self, *args, **kwds): |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 24 | if not self.tenant_id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 25 | tenant = self.driver.create_tenant(tenant_name=self.login_base, |
| 26 | description=self.name, |
| 27 | enabled=self.enabled) |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 28 | self.tenant_id = tenant.id |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 29 | # give caller an admin role at the tenant they've created |
| 30 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 31 | |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 32 | # update the record |
| 33 | if self.id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 34 | self.driver.update_tenant(self.tenant_id, |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 35 | description=self.name, |
| 36 | enabled=self.enabled) |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 37 | |
| 38 | super(Site, self).save(*args, **kwds) |
| 39 | |
| 40 | |
| 41 | def delete(self, *args, **kwds): |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 42 | if self.tenant_id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 43 | self.driver.delete_tenant(self.tenant_id) |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 44 | super(Site, self).delete(*args, **kwds) |
| 45 | |
| 46 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 47 | class SitePrivilege(PlCoreBase): |
| 48 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 49 | user = models.ForeignKey('PLUser', related_name='site_privileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 50 | site = models.ForeignKey('Site', related_name='site_privileges') |
| 51 | role = models.ForeignKey('Role') |
| 52 | |
| 53 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 54 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 55 | def save(self, *args, **kwds): |
| 56 | self.driver.add_user_role(self.user.user_id, self.site.tenant_id, self.role.role_type) |
| 57 | super(SitePrivilege, self).save(*args, **kwds) |
| 58 | |
| 59 | def delete(self, *args, **kwds): |
| 60 | self.driver.delete_user_role(self.user.user_id, self.site.tenant_id, self.role.role_type) |
| 61 | super(SitePrivilege, self).delete(*args, **kwds) |
| 62 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 63 | |