refactor sliceplus core for users, return user ids and names in sliceplus object
Scott Baker [Sat, 17 Jan 2015 03:07:36 +0000 (19:07 -0800)]
planetstack/core/xoslib/methods/sliceplus.py
planetstack/core/xoslib/objects/sliceplus.py
planetstack/core/xoslib/static/js/xoslib/xos-util.js

index c48d036..4d15d41 100644 (file)
@@ -44,6 +44,7 @@ class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
         network_ports = NetworkPortsField(required=False)\r
         site_allocation = DictionaryField(required=False)\r
         users = ListField(required=False)\r
+        user_names = ListField(required=False) # readonly = True ?\r
 \r
         def getSliceInfo(self, slice):\r
             return slice.getSliceInfo(user=self.context['request'].user)\r
@@ -57,7 +58,7 @@ class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
             model = SlicePlus\r
             fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','service','network','mount_data_sets',
                       'default_image', 'default_flavor',
-                      'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation','users')
+                      'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation','users',"user_names")
 
 class SlicePlusList(PlusListCreateAPIView): #generics.ListCreateAPIView):
     queryset = SlicePlus.objects.select_related().all()
index 73017ee..4fdc824 100644 (file)
@@ -10,27 +10,39 @@ class SlicePlus(Slice, PlusObjectMixin):
         super(SlicePlus, self).__init__(*args, **kwargs)
         self._update_site_allocation = None
         self._update_users = None
+        self._sliceInfo = None
 
     def getSliceInfo(self, user=None):
-        used_sites = {}
-        used_deployments = {}
-        sliverCount = 0
-        for sliver in self.slivers.all():
-            site = sliver.node.site_deployment.site
-            deployment = sliver.node.site_deployment.deployment
-            used_sites[site.name] = used_sites.get(site.name, 0) + 1
-            used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
-            sliverCount = sliverCount + 1
-
-        roles = []
-        if (user!=None):
-            roles = [x.role.role for x in self.sliceprivileges.filter(user=user)]
-
-        return {"sitesUsed": used_sites,
-                "deploymentsUsed": used_deployments,
-                "sliverCount": sliverCount,
-                "siteCount": len(used_sites.keys()),
-                "roles": roles}
+        if not self._sliceInfo:
+            used_sites = {}
+            used_deployments = {}
+            sliverCount = 0
+            for sliver in self.slivers.all():
+                site = sliver.node.site_deployment.site
+                deployment = sliver.node.site_deployment.deployment
+                used_sites[site.name] = used_sites.get(site.name, 0) + 1
+                used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
+                sliverCount = sliverCount + 1
+
+            users = {}
+            for priv in SlicePrivilege.objects.filter(slice=self):
+                if not (priv.user.id in users.keys()):
+                    users[priv.user.id] = {"name": priv.user.email, "id": priv.user.id, "roles": []}
+                users[priv.user.id]["roles"].append(priv.role.role)
+
+            self._sliceInfo= {"sitesUsed": used_sites,
+                    "deploymentsUsed": used_deployments,
+                    "sliverCount": sliverCount,
+                    "siteCount": len(used_sites.keys()),
+                    "users": users,
+                    "roles": []}
+
+        if user:
+            auser = self._sliceInfo["users"].get(user.id, None)
+            if (auser):
+                self._sliceInfo["roles"] = auser["roles"]
+
+        return self._sliceInfo
 
     @property
     def site_allocation(self):
@@ -42,12 +54,12 @@ class SlicePlus(Slice, PlusObjectMixin):
         #print "XXX set sitesUsed to", value
 
     @property
+    def user_names(self):
+        return [user["name"] for user in self.getSliceInfo()["users"].values()]
+
+    @property
     def users(self):
-        user_ids = []
-        for priv in SlicePrivilege.objects.filter(slice=self):
-            if not (priv.user.id in user_ids):
-                user_ids.append(priv.user.id)
-        return user_ids
+        return [user["id"] for user in self.getSliceInfo()["users"].values()]
 
     @users.setter
     def users(self, value):
@@ -165,7 +177,6 @@ class SlicePlus(Slice, PlusObjectMixin):
 
         for user_id in new_users:
             if (user_id not in slice_user_ids):
-                print "XXX", user_id
                 priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role)
                 if (not noAct):
                     priv.save()
index 15aa1b0..7a57d8a 100644 (file)
@@ -100,7 +100,7 @@ function array_pair_lookup(x, names, values)
             return names[index];
         }
     }
-    return undefined;
+    return "object #" + x;
 }
 
 function all_options(selector) {