blob: b97432b5038319d254aac5aab0a3d2baf4a6ab28 [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")
Siobhan Tullycf04fb62014-01-11 11:25:57 -050015# sites = ManyToManyField_NoSyncdb('Site', through=Site.deployments.through, blank=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040016
17 def __unicode__(self): return u'%s' % (self.name)
18
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040019
20class DeploymentRole(PlCoreBase):
21
22 ROLE_CHOICES = (('admin','Admin'),)
23 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
24
25 def __unicode__(self): return u'%s' % (self.role)
26
27class DeploymentPrivilege(PlCoreBase):
28
29 user = models.ForeignKey('User', related_name='deployment_privileges')
30 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
31 role = models.ForeignKey('DeploymentRole')
32
33 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
34
Tony Mack5b061472014-02-04 07:57:10 -050035
36 def can_update(self, user):
37 if user.is_readonly:
38 return False
39 if user.is_admin:
40 return True
41 dprivs = DeploymentPrivilege.objects.filter(user=user)
42 for dpriv in dprivs:
43 if dpriv.role.role_type == 'admin':
44 return True
45 return False
46
Tony Mack5b061472014-02-04 07:57:10 -050047 @staticmethod
48 def select_by_user(user):
49 if user.is_admin:
50 qs = DeploymentPrivilege.objects.all()
51 else:
52 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
53 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
54 return qs