| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 1 | from django.db import models |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 2 | from core.models import PlCoreBase,SingletonModel,PlCoreBaseManager |
| Tony Mack | d84b1ff | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 3 | from core.models.plcorebase import StrippedCharField |
| Scott Baker | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 4 | from xos.exceptions import * |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 5 | from operator import attrgetter |
| Scott Baker | f57e559 | 2015-04-14 17:18:51 -0700 | [diff] [blame] | 6 | import json |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 7 | |
| Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 8 | class AttributeMixin(object): |
| 9 | # helper for extracting things from a json-encoded service_specific_attribute |
| 10 | def get_attribute(self, name, default=None): |
| 11 | if self.service_specific_attribute: |
| 12 | attributes = json.loads(self.service_specific_attribute) |
| 13 | else: |
| 14 | attributes = {} |
| 15 | return attributes.get(name, default) |
| 16 | |
| 17 | def set_attribute(self, name, value): |
| 18 | if self.service_specific_attribute: |
| 19 | attributes = json.loads(self.service_specific_attribute) |
| 20 | else: |
| 21 | attributes = {} |
| 22 | attributes[name]=value |
| 23 | self.service_specific_attribute = json.dumps(attributes) |
| 24 | |
| 25 | def get_initial_attribute(self, name, default=None): |
| 26 | if self._initial["service_specific_attribute"]: |
| 27 | attributes = json.loads(self._initial["service_specific_attribute"]) |
| 28 | else: |
| 29 | attributes = {} |
| 30 | return attributes.get(name, default) |
| 31 | |
| Scott Baker | 74404fe | 2015-07-13 13:54:06 -0700 | [diff] [blame] | 32 | @classmethod |
| 33 | def setup_simple_attributes(cls): |
| 34 | for (attrname, default) in cls.simple_attributes: |
| Scott Baker | 096dce8 | 2015-07-13 14:27:51 -0700 | [diff] [blame] | 35 | setattr(cls, attrname, property(lambda self, attrname=attrname, default=default: self.get_attribute(attrname, default), |
| 36 | lambda self, value, attrname=attrname: self.set_attribute(attrname, value), |
| 37 | None, |
| 38 | attrname)) |
| Scott Baker | 74404fe | 2015-07-13 13:54:06 -0700 | [diff] [blame] | 39 | |
| Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 40 | class Service(PlCoreBase, AttributeMixin): |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 41 | # when subclassing a service, redefine KIND to describe the new service |
| 42 | KIND = "generic" |
| 43 | |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 44 | description = models.TextField(max_length=254,null=True, blank=True,help_text="Description of Service") |
| 45 | enabled = models.BooleanField(default=True) |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 46 | kind = StrippedCharField(max_length=30, help_text="Kind of service", default=KIND) |
| Tony Mack | d84b1ff | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 47 | name = StrippedCharField(max_length=30, help_text="Service Name") |
| 48 | versionNumber = StrippedCharField(max_length=30, help_text="Version of Service Definition") |
| Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 49 | published = models.BooleanField(default=True) |
| Tony Mack | d84b1ff | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 50 | view_url = StrippedCharField(blank=True, null=True, max_length=1024) |
| 51 | icon_url = StrippedCharField(blank=True, null=True, max_length=1024) |
| Scott Baker | 6894474 | 2015-04-30 14:30:56 -0700 | [diff] [blame] | 52 | public_key = models.TextField(null=True, blank=True, max_length=1024, help_text="Public key string") |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 53 | |
| Scott Baker | 2f0828e | 2015-07-13 12:33:28 -0700 | [diff] [blame] | 54 | # Service_specific_attribute and service_specific_id are opaque to XOS |
| 55 | service_specific_id = StrippedCharField(max_length=30, blank=True, null=True) |
| 56 | service_specific_attribute = models.TextField(blank=True, null=True) |
| 57 | |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 58 | def __init__(self, *args, **kwargs): |
| 59 | # for subclasses, set the default kind appropriately |
| 60 | self._meta.get_field("kind").default = self.KIND |
| 61 | super(Service, self).__init__(*args, **kwargs) |
| 62 | |
| 63 | @classmethod |
| 64 | def get_service_objects(cls): |
| 65 | return cls.objects.filter(kind = cls.KIND) |
| 66 | |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 67 | def __unicode__(self): return u'%s' % (self.name) |
| 68 | |
| Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 69 | def can_update(self, user): |
| 70 | return user.can_update_service(self, allow=['admin']) |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 71 | |
| Scott Baker | 9843673 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 72 | def get_scalable_nodes(self, slice, max_per_node=None, exclusive_slices=[]): |
| 73 | """ |
| 74 | Get a list of nodes that can be used to scale up a slice. |
| 75 | |
| 76 | slice - slice to scale up |
| 77 | max_per_node - maximum numbers of slivers that 'slice' can have on a single node |
| 78 | exclusive_slices - list of slices that must have no nodes in common with 'slice'. |
| 79 | """ |
| 80 | |
| 81 | from core.models import Node, Sliver # late import to get around order-of-imports constraint in __init__.py |
| 82 | |
| 83 | nodes = list(Node.objects.all()) |
| 84 | |
| 85 | conflicting_slivers = Sliver.objects.filter(slice__in = exclusive_slices) |
| 86 | conflicting_nodes = Node.objects.filter(slivers__in = conflicting_slivers) |
| 87 | |
| 88 | nodes = [x for x in nodes if x not in conflicting_nodes] |
| 89 | |
| 90 | # If max_per_node is set, then limit the number of slivers this slice |
| 91 | # can have on a single node. |
| 92 | if max_per_node: |
| 93 | acceptable_nodes = [] |
| 94 | for node in nodes: |
| 95 | existing_count = node.slivers.filter(slice=slice).count() |
| 96 | if existing_count < max_per_node: |
| 97 | acceptable_nodes.append(node) |
| 98 | nodes = acceptable_nodes |
| 99 | |
| 100 | return nodes |
| 101 | |
| 102 | def pick_node(self, slice, max_per_node=None, exclusive_slices=[]): |
| 103 | # Pick the best node to scale up a slice. |
| 104 | |
| 105 | nodes = self.get_scalable_nodes(slice, max_per_node, exclusive_slices) |
| 106 | nodes = sorted(nodes, key=lambda node: node.slivers.all().count()) |
| 107 | if not nodes: |
| 108 | return None |
| 109 | return nodes[0] |
| 110 | |
| 111 | def adjust_scale(self, slice_hint, scale, max_per_node=None, exclusive_slices=[]): |
| 112 | from core.models import Sliver # late import to get around order-of-imports constraint in __init__.py |
| 113 | |
| 114 | slices = [x for x in self.slices.all() if slice_hint in x.name] |
| 115 | for slice in slices: |
| 116 | while slice.slivers.all().count() > scale: |
| 117 | s = slice.slivers.all()[0] |
| 118 | # print "drop sliver", s |
| 119 | s.delete() |
| 120 | |
| 121 | while slice.slivers.all().count() < scale: |
| 122 | node = self.pick_node(slice, max_per_node, exclusive_slices) |
| 123 | if not node: |
| 124 | # no more available nodes |
| 125 | break |
| 126 | |
| 127 | image = slice.default_image |
| 128 | if not image: |
| 129 | raise XOSConfigurationError("No default_image for slice %s" % slice.name) |
| 130 | |
| 131 | flavor = slice.default_flavor |
| 132 | if not flavor: |
| 133 | raise XOSConfigurationError("No default_flavor for slice %s" % slice.name) |
| 134 | |
| 135 | s = Sliver(slice=slice, |
| 136 | node=node, |
| 137 | creator=slice.creator, |
| 138 | image=image, |
| 139 | flavor=flavor, |
| 140 | deployment=node.site_deployment.deployment) |
| 141 | s.save() |
| 142 | |
| 143 | # print "add sliver", s |
| Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 144 | |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 145 | class ServiceAttribute(PlCoreBase): |
| 146 | name = models.SlugField(help_text="Attribute Name", max_length=128) |
| Tony Mack | d84b1ff | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 147 | value = StrippedCharField(help_text="Attribute Value", max_length=1024) |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 148 | service = models.ForeignKey(Service, related_name='serviceattributes', help_text="The Service this attribute is associated with") |
| 149 | |
| Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 150 | class ServiceRole(PlCoreBase): |
| 151 | ROLE_CHOICES = (('admin','Admin'),) |
| 152 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 153 | |
| 154 | def __unicode__(self): return u'%s' % (self.role) |
| 155 | |
| 156 | class ServicePrivilege(PlCoreBase): |
| 157 | user = models.ForeignKey('User', related_name='serviceprivileges') |
| 158 | service = models.ForeignKey('Service', related_name='serviceprivileges') |
| 159 | role = models.ForeignKey('ServiceRole',related_name='serviceprivileges') |
| 160 | |
| 161 | class Meta: |
| Tony Mack | 9ec754e | 2015-05-13 12:21:28 -0400 | [diff] [blame] | 162 | unique_together = ('user', 'service', 'role') |
| Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 163 | |
| 164 | def __unicode__(self): return u'%s %s %s' % (self.service, self.user, self.role) |
| 165 | |
| 166 | def can_update(self, user): |
| 167 | if not self.service.enabled: |
| 168 | raise PermissionDenied, "Cannot modify permission(s) of a disabled service" |
| 169 | return self.service.can_update(user) |
| 170 | |
| 171 | def save(self, *args, **kwds): |
| 172 | if not self.service.enabled: |
| 173 | raise PermissionDenied, "Cannot modify permission(s) of a disabled service" |
| 174 | super(ServicePrivilege, self).save(*args, **kwds) |
| 175 | |
| 176 | def delete(self, *args, **kwds): |
| 177 | if not self.service.enabled: |
| 178 | raise PermissionDenied, "Cannot modify permission(s) of a disabled service" |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 179 | super(ServicePrivilege, self).delete(*args, **kwds) |
| 180 | |
| Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 181 | @staticmethod |
| 182 | def select_by_user(user): |
| 183 | if user.is_admin: |
| 184 | qs = ServicePrivilege.objects.all() |
| 185 | else: |
| 186 | qs = SitePrivilege.objects.filter(user=user) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 187 | return qs |
| 188 | |
| Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 189 | class TenantRoot(PlCoreBase, AttributeMixin): |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 190 | """ A tenantRoot is one of the things that can sit at the root of a chain |
| 191 | of tenancy. This object represents a node. |
| 192 | """ |
| 193 | |
| 194 | KIND= "generic" |
| 195 | kind = StrippedCharField(max_length=30, default=KIND) |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 196 | name = StrippedCharField(max_length=255, help_text="name", blank=True, null=True) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 197 | |
| Scott Baker | efcec63 | 2015-07-07 12:12:42 -0700 | [diff] [blame] | 198 | service_specific_attribute = models.TextField(blank=True, null=True) |
| 199 | service_specific_id = StrippedCharField(max_length=30, blank=True, null=True) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 200 | |
| Scott Baker | db66fd3 | 2015-07-07 17:59:44 -0700 | [diff] [blame] | 201 | def __init__(self, *args, **kwargs): |
| 202 | # for subclasses, set the default kind appropriately |
| 203 | self._meta.get_field("kind").default = self.KIND |
| 204 | super(TenantRoot, self).__init__(*args, **kwargs) |
| 205 | |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 206 | def __unicode__(self): |
| 207 | if not self.name: |
| 208 | return u"%s-tenant_root-#%s" % (str(self.kind), str(self.id)) |
| 209 | else: |
| 210 | return self.name |
| 211 | |
| 212 | def can_update(self, user): |
| 213 | return user.can_update_tenant_root(self, allow=['admin']) |
| 214 | |
| Scott Baker | efcec63 | 2015-07-07 12:12:42 -0700 | [diff] [blame] | 215 | def get_subscribed_tenants(self, tenant_class): |
| 216 | ids = self.subscribed_tenants.filter(kind=tenant_class.KIND) |
| 217 | return tenant_class.objects.filter(id__in = ids) |
| 218 | |
| 219 | def get_newest_subscribed_tenant(self, kind): |
| 220 | st = list(self.get_subscribed_tenants(kind)) |
| 221 | if not st: |
| 222 | return None |
| 223 | return sorted(st, key=attrgetter('id'))[0] |
| 224 | |
| 225 | @classmethod |
| 226 | def get_tenant_objects(cls): |
| 227 | return cls.objects.filter(kind = cls.KIND) |
| 228 | |
| Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 229 | class Tenant(PlCoreBase, AttributeMixin): |
| Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 230 | """ A tenant is a relationship between two entities, a subscriber and a |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 231 | provider. This object represents an edge. |
| Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 232 | |
| 233 | The subscriber can be a User, a Service, or a Tenant. |
| 234 | |
| 235 | The provider is always a Service. |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 236 | |
| 237 | TODO: rename "Tenant" to "Tenancy" |
| Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 238 | """ |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 239 | |
| Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 240 | CONNECTIVITY_CHOICES = (('public', 'Public'), ('private', 'Private'), ('na', 'Not Applicable')) |
| 241 | |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 242 | # when subclassing a service, redefine KIND to describe the new service |
| 243 | KIND = "generic" |
| 244 | |
| 245 | kind = StrippedCharField(max_length=30, default=KIND) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 246 | provider_service = models.ForeignKey(Service, related_name='provided_tenants') |
| 247 | |
| 248 | # The next four things are the various type of objects that can be subscribers of this Tenancy |
| 249 | # relationship. One and only one can be used at a time. |
| 250 | subscriber_service = models.ForeignKey(Service, related_name='subscribed_tenants', blank=True, null=True) |
| 251 | subscriber_tenant = models.ForeignKey("Tenant", related_name='subscribed_tenants', blank=True, null=True) |
| 252 | subscriber_user = models.ForeignKey("User", related_name='subscribed_tenants', blank=True, null=True) |
| 253 | subscriber_root = models.ForeignKey("TenantRoot", related_name="subscribed_tenants", blank=True, null=True) |
| 254 | |
| 255 | # Service_specific_attribute and service_specific_id are opaque to XOS |
| Scott Baker | 76934d8 | 2015-05-06 19:49:31 -0700 | [diff] [blame] | 256 | service_specific_id = StrippedCharField(max_length=30, blank=True, null=True) |
| 257 | service_specific_attribute = models.TextField(blank=True, null=True) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 258 | |
| 259 | # Connect_method is only used by Coarse tenants |
| Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 260 | connect_method = models.CharField(null=False, blank=False, max_length=30, choices=CONNECTIVITY_CHOICES, default="na") |
| Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 261 | |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 262 | def __init__(self, *args, **kwargs): |
| 263 | # for subclasses, set the default kind appropriately |
| 264 | self._meta.get_field("kind").default = self.KIND |
| 265 | super(Tenant, self).__init__(*args, **kwargs) |
| 266 | |
| Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 267 | def __unicode__(self): |
| Scott Baker | f996b76 | 2015-05-20 20:42:04 -0700 | [diff] [blame] | 268 | return u"%s-tenant-%s" % (str(self.kind), str(self.id)) |
| Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 269 | |
| Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 270 | @classmethod |
| 271 | def get_tenant_objects(cls): |
| 272 | return cls.objects.filter(kind = cls.KIND) |
| 273 | |
| Scott Baker | e7fc9f5 | 2015-05-05 17:52:03 -0700 | [diff] [blame] | 274 | @classmethod |
| 275 | def get_deleted_tenant_objects(cls): |
| 276 | return cls.deleted_objects.filter(kind = cls.KIND) |
| 277 | |
| Scott Baker | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 278 | # helper function to be used in subclasses that want to ensure service_specific_id is unique |
| 279 | def validate_unique_service_specific_id(self): |
| 280 | if self.pk is None: |
| 281 | if self.service_specific_id is None: |
| 282 | raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={"service_specific_id": "cannot be none"}) |
| 283 | |
| 284 | conflicts = self.get_tenant_objects().filter(service_specific_id=self.service_specific_id) |
| 285 | if conflicts: |
| 286 | raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={"service_specific_id": "duplicate key"}) |
| 287 | |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 288 | def save(self, *args, **kwargs): |
| 289 | subCount = sum( [1 for e in [self.subscriber_service, self.subscriber_tenant, self.subscriber_user, self.subscriber_root] if e is not None]) |
| 290 | if (subCount > 1): |
| 291 | raise XOSConflictingField("Only one of subscriber_service, subscriber_tenant, subscriber_user, subscriber_root should be set") |
| 292 | |
| 293 | super(Tenant, self).save(*args, **kwargs) |
| 294 | |
| 295 | def get_subscribed_tenants(self, tenant_class): |
| 296 | ids = self.subscribed_tenants.filter(kind=tenant_class.KIND) |
| 297 | return tenant_class.objects.filter(id__in = ids) |
| 298 | |
| 299 | def get_newest_subscribed_tenant(self, kind): |
| 300 | st = list(self.get_subscribed_tenants(kind)) |
| 301 | if not st: |
| 302 | return None |
| 303 | return sorted(st, key=attrgetter('id'))[0] |
| 304 | |
| 305 | |
| Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 306 | class CoarseTenant(Tenant): |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 307 | """ TODO: rename "CoarseTenant" --> "StaticTenant" """ |
| Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 308 | class Meta: |
| 309 | proxy = True |
| Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 310 | |
| Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 311 | KIND = "coarse" |
| 312 | |
| 313 | def save(self, *args, **kwargs): |
| 314 | if (not self.subscriber_service): |
| 315 | raise XOSValidationError("subscriber_service cannot be null") |
| 316 | if (self.subscriber_tenant or self.subscriber_user): |
| 317 | raise XOSValidationError("subscriber_tenant and subscriber_user must be null") |
| 318 | |
| 319 | super(CoarseTenant,self).save() |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 320 | |
| 321 | class Subscriber(TenantRoot): |
| 322 | """ Intermediate class for TenantRoots that are to be Subscribers """ |
| 323 | |
| 324 | class Meta: |
| 325 | proxy = True |
| 326 | |
| 327 | KIND = "Subscriber" |
| 328 | |
| 329 | class Provider(TenantRoot): |
| 330 | """ Intermediate class for TenantRoots that are to be Providers """ |
| 331 | |
| 332 | class Meta: |
| 333 | proxy = True |
| 334 | |
| 335 | KIND = "Provider" |
| 336 | |
| 337 | class TenantRootRole(PlCoreBase): |
| 338 | ROLE_CHOICES = (('admin','Admin'),) |
| 339 | |
| 340 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 341 | |
| 342 | def __unicode__(self): return u'%s' % (self.role) |
| 343 | |
| 344 | class TenantRootPrivilege(PlCoreBase): |
| 345 | user = models.ForeignKey('User', related_name="tenant_root_privileges") |
| 346 | tenant_root = models.ForeignKey('TenantRoot', related_name="tenant_root_privileges") |
| 347 | role = models.ForeignKey('TenantRootRole', related_name="tenant_root_privileges") |
| 348 | |
| 349 | class Meta: |
| 350 | unique_together = ('user', 'tenant_root', 'role') |
| 351 | |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 352 | def __unicode__(self): return u'%s %s %s' % (self.tenant_root, self.user, self.role) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 353 | |
| 354 | def save(self, *args, **kwds): |
| 355 | if not self.user.is_active: |
| 356 | raise PermissionDenied, "Cannot modify role(s) of a disabled user" |
| Scott Baker | 335882a | 2015-07-24 10:15:31 -0700 | [diff] [blame] | 357 | super(TenantRootPrivilege, self).save(*args, **kwds) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 358 | |
| 359 | def can_update(self, user): |
| Scott Baker | 335882a | 2015-07-24 10:15:31 -0700 | [diff] [blame] | 360 | return user.can_update_tenant_root_privilege(self) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 361 | |
| 362 | @staticmethod |
| 363 | def select_by_user(user): |
| 364 | if user.is_admin: |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 365 | qs = TenantRootPrivilege.objects.all() |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 366 | else: |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 367 | qs = TenantRootPrivilege.objects.filter(user=user) |
| Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 368 | return qs |
| Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 369 | |