add get_newest_subscribed_tenant method, add name field to TenantRoot, fix priveleges
diff --git a/xos/core/models/service.py b/xos/core/models/service.py
index ba3a991..bde89e6 100644
--- a/xos/core/models/service.py
+++ b/xos/core/models/service.py
@@ -2,6 +2,7 @@
from core.models import PlCoreBase,SingletonModel,PlCoreBaseManager
from core.models.plcorebase import StrippedCharField
from xos.exceptions import *
+from operator import attrgetter
import json
class Service(PlCoreBase):
@@ -31,7 +32,7 @@
def can_update(self, user):
return user.can_update_service(self, allow=['admin'])
-
+
def get_scalable_nodes(self, slice, max_per_node=None, exclusive_slices=[]):
"""
Get a list of nodes that can be used to scale up a slice.
@@ -156,6 +157,7 @@
KIND= "generic"
kind = StrippedCharField(max_length=30, default=KIND)
+ name = StrippedCharField(max_length=255, help_text="name", blank=True, null=True)
attribute = models.TextField(blank=True, null=True)
@@ -175,6 +177,15 @@
attributes[name]=value
self.attribute = json.dumps(attributes)
+ def __unicode__(self):
+ if not self.name:
+ return u"%s-tenant_root-#%s" % (str(self.kind), str(self.id))
+ else:
+ return self.name
+
+ def can_update(self, user):
+ return user.can_update_tenant_root(self, allow=['admin'])
+
class Tenant(PlCoreBase):
""" A tenant is a relationship between two entities, a subscriber and a
provider. This object represents an edge.
@@ -257,6 +268,24 @@
if conflicts:
raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={"service_specific_id": "duplicate key"})
+ def save(self, *args, **kwargs):
+ subCount = sum( [1 for e in [self.subscriber_service, self.subscriber_tenant, self.subscriber_user, self.subscriber_root] if e is not None])
+ if (subCount > 1):
+ raise XOSConflictingField("Only one of subscriber_service, subscriber_tenant, subscriber_user, subscriber_root should be set")
+
+ super(Tenant, self).save(*args, **kwargs)
+
+ def get_subscribed_tenants(self, tenant_class):
+ ids = self.subscribed_tenants.filter(kind=tenant_class.KIND)
+ return tenant_class.objects.filter(id__in = ids)
+
+ def get_newest_subscribed_tenant(self, kind):
+ st = list(self.get_subscribed_tenants(kind))
+ if not st:
+ return None
+ return sorted(st, key=attrgetter('id'))[0]
+
+
class CoarseTenant(Tenant):
""" TODO: rename "CoarseTenant" --> "StaticTenant" """
class Meta:
@@ -303,7 +332,7 @@
class Meta:
unique_together = ('user', 'tenant_root', 'role')
- def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
+ def __unicode__(self): return u'%s %s %s' % (self.tenant_root, self.user, self.role)
def save(self, *args, **kwds):
if not self.user.is_active:
@@ -311,13 +340,13 @@
super(SlicePrivilege, self).save(*args, **kwds)
def can_update(self, user):
- return user.can_update_tenant_root(self.tenant_root)
+ return user.tenant_root.can_update(user)
@staticmethod
def select_by_user(user):
if user.is_admin:
- qs = TenantRoot.objects.all()
+ qs = TenantRootPrivilege.objects.all()
else:
- sp_ids = [sp.id for sp in TenantRoot.objects.filter(user=user)]
- qs = TenantRoot.objects.filter(id__in=sp_ids)
+ qs = TenantRootPrivilege.objects.filter(user=user)
return qs
+