blob: 738276a27623b4529a8b2322d28a1d87763c3b90 [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 Tullybfd11dc2013-09-03 12:59:24 -04004from django.contrib.contenttypes import generic
Siobhan Tully4bc09f22013-04-10 21:15:21 -04005
6# Create your models here.
7
Siobhan Tullycf04fb62014-01-11 11:25:57 -05008class ManyToManyField_NoSyncdb(models.ManyToManyField):
9 def __init__(self, *args, **kwargs):
10 super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)
11 self.creates_table = False
12
Siobhan Tullybf1153a2013-05-27 20:53:48 -040013class Deployment(PlCoreBase):
14 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Tony Mackdd240952014-06-03 23:02:00 -040015 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
16 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
17 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
18 auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
Siobhan Tullycf04fb62014-01-11 11:25:57 -050019# sites = ManyToManyField_NoSyncdb('Site', through=Site.deployments.through, blank=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040020
21 def __unicode__(self): return u'%s' % (self.name)
22
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040023
24class DeploymentRole(PlCoreBase):
25
26 ROLE_CHOICES = (('admin','Admin'),)
27 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
28
29 def __unicode__(self): return u'%s' % (self.role)
30
31class DeploymentPrivilege(PlCoreBase):
32
33 user = models.ForeignKey('User', related_name='deployment_privileges')
34 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
35 role = models.ForeignKey('DeploymentRole')
36
37 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
38
Tony Mack5b061472014-02-04 07:57:10 -050039
40 def can_update(self, user):
41 if user.is_readonly:
42 return False
43 if user.is_admin:
44 return True
45 dprivs = DeploymentPrivilege.objects.filter(user=user)
46 for dpriv in dprivs:
47 if dpriv.role.role_type == 'admin':
48 return True
49 return False
50
Tony Mack5b061472014-02-04 07:57:10 -050051 @staticmethod
52 def select_by_user(user):
53 if user.is_admin:
54 qs = DeploymentPrivilege.objects.all()
55 else:
56 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
57 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
58 return qs