save users supported in tenant view
authorScott Baker <smbaker@gmail.com>
Wed, 14 Jan 2015 08:34:45 +0000 (00:34 -0800)
committerScott Baker <smbaker@gmail.com>
Wed, 14 Jan 2015 08:34:45 +0000 (00:34 -0800)
planetstack/core/xoslib/dashboards/xosTenant.html
planetstack/core/xoslib/objects/sliceplus.py
planetstack/core/xoslib/static/js/xosTenant.js

index 01b354c..4ccf10c 100644 (file)
@@ -41,8 +41,8 @@
 </script>
 
 <script type="text/template" id="tenant-edit-users">
-    <%= xosPickerTemplate({pickedItems: model.attributes.users,
-                          unpickedItems: array_diff(xos.tenant().current_user_site_users, model.attributes.users),
+    <%= xosPickerTemplate({pickedItems: model.usersBuffer,
+                          unpickedItems: array_diff(xos.tenant().current_user_site_users, model.usersBuffer),
                           id: "users",
                           fieldName: "users",
                           detailView: detailView,
index bbab060..73017ee 100644 (file)
@@ -1,4 +1,4 @@
-from core.models import Slice, SlicePrivilege, Sliver, Site, Node
+from core.models import Slice, SlicePrivilege, SliceRole, Sliver, Site, Node, User
 from plus import PlusObjectMixin
 from operator import itemgetter, attrgetter
 
@@ -80,7 +80,7 @@ class SlicePlus(Slice, PlusObjectMixin):
             qs = SlicePlus.objects.filter(id__in=slice_ids)
         return qs
 
-    def get_site_node_allocation(self, siteList):
+    def get_node_allocation(self, siteList):
         siteIDList = [site.id for site in siteList]
         nodeList = []
         for node in Node.objects.all():
@@ -97,12 +97,20 @@ class SlicePlus(Slice, PlusObjectMixin):
 
         if self._update_site_allocation:
             self.save_site_allocation(noAct=True)
+
+        if self._update_users:
+            self.save_users(noAct=True)
+
+        if self._update_site_allocation:
             self.save_site_allocation()
 
+        if self._update_users:
+            self.save_users()
+
     def save_site_allocation(self, noAct = False):
         new_site_allocation = self._update_site_allocation
 
-        all_slice_slivers = self.slivers.all() # Sliver.objects.filter(slice=self)
+        all_slice_slivers = self.slivers.all()
         for site_name in new_site_allocation.keys():
             desired_allocation = new_site_allocation[site_name]
 
@@ -122,7 +130,7 @@ class SlicePlus(Slice, PlusObjectMixin):
             # add more slivers
             if (len(slivers) < desired_allocation):
                 site = Site.objects.get(name = site_name)
-                nodes = self.get_site_node_allocation([site])
+                nodes = self.get_node_allocation([site])
 
                 if (not nodes):
                     raise ValueError("no nodes in site %s" % site_name)
@@ -147,4 +155,35 @@ class SlicePlus(Slice, PlusObjectMixin):
 
                     node.sliverCount = node.sliverCount + 1
 
+    def save_users(self, noAct = False):
+        new_users = self._update_users
+
+        default_role = SliceRole.objects.get(role="default")
+
+        slice_privs = self.sliceprivileges.all()
+        slice_user_ids = [priv.user.id for priv in slice_privs]
+
+        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()
+
+                print "added user id", user_id
+
+        for priv in slice_privs:
+             if (priv.role.id != default_role.id):
+                 # only mess with 'default' users; don't kill an admin
+                 continue
+
+             if (priv.user.id not in new_users):
+                 if (not noAct):
+                     priv.delete()
+
+                 print "deleted user id", user_id
+
+
+
+
 
index 4c70f70..48ac835 100644 (file)
@@ -30,7 +30,7 @@ XOSTenantSiteCollection = XOSCollection.extend( {
     putToSlice: function(slice) {
         slice.attributes.site_allocation = {};
         for (index in this.models) {
-            model = this.models[index];
+            var model = this.models[index];
             slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated;
         }
     },
@@ -75,6 +75,7 @@ XOSTenantButtonView = Marionette.ItemView.extend({
             saveClicked: function(e) {
                      model = this.options.linkedView.model;
                      model.tenantSiteCollection.putToSlice(model);
+                     model.attributes.users = model.usersBuffer;
                      this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
                      },
             });
@@ -181,7 +182,7 @@ XOSTenantApp.addSlice = function() {
        modal: true,
        width: 640,
        buttons : {
-            "Save" : function() {
+            "Create Slice" : function() {
               var addDialog = this;
               console.log("SAVE!!!");
               detailView.synchronous = true;
@@ -206,11 +207,11 @@ XOSTenantApp.editUsers = function(model) {
        modal: true,
        width: 640,
        buttons : {
-            "Save" : function() {
+            "Ok" : function() {
               var editDialog = this;
               user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
               user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
-              model.attributes.users = user_ids;
+              model.usersBuffer = user_ids;
               $(editDialog).dialog("close");
             },
             "Cancel" : function() {
@@ -248,6 +249,7 @@ XOSTenantApp.viewSlice = function(model) {
 \r
         tenantSites = new XOSTenantSiteCollection();\r
         tenantSites.getFromSlice(model);\r
+        model.usersBuffer = model.attributes.users; /* save a copy of 'users' that we can edit. This prevents another view (developer) from overwriting our copy with a fetch from the server */\r
         model.tenantSiteCollection = tenantSites;\r
         XOSTenantApp.tenantSites = tenantSites;\r
 \r