airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal] 01/07: AIRAVATA-2835 Persist changes to SharedEntity
Date Fri, 13 Jul 2018 00:18:06 GMT
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit 4e1569ec18d8535e80586322520b4b7d43e33374
Author: Marcus Christie <machrist@iu.edu>
AuthorDate: Wed Jul 11 16:52:55 2018 -0400

    AIRAVATA-2835 Persist changes to SharedEntity
---
 django_airavata/apps/api/serializers.py            | 42 +++++++++++----------
 django_airavata/apps/api/views.py                  | 34 ++++++++---------
 .../static/common/js/components/ShareButton.vue    | 43 +++++++++++++++++++++-
 3 files changed, 80 insertions(+), 39 deletions(-)

diff --git a/django_airavata/apps/api/serializers.py b/django_airavata/apps/api/serializers.py
index af48f68..e361273 100644
--- a/django_airavata/apps/api/serializers.py
+++ b/django_airavata/apps/api/serializers.py
@@ -130,7 +130,7 @@ class StoredJSONField(serializers.JSONField):
 
 class GroupSerializer(serializers.Serializer):
     url = FullyEncodedHyperlinkedIdentityField(view_name='django_airavata_api:group-detail',
lookup_field='id', lookup_url_kwarg='group_id')
-    id = serializers.CharField(default=GroupModel.thrift_spec[1][4], read_only=True)
+    id = serializers.CharField(default=GroupModel.thrift_spec[1][4])
     name = serializers.CharField(required=True)
     description = serializers.CharField(allow_null=True, allow_blank=True)
     ownerId = serializers.CharField(read_only=True)
@@ -482,34 +482,36 @@ class SharedEntitySerializer(serializers.Serializer):
 
     def update(self, instance, validated_data):
         # Compute lists of ids to grant/revoke READ/WRITE
-        existing_user_permissions = {user.user.userId: user.permissionType
-                                     for user in instance.userPermissions}
-        new_user_permissions = {user.user.userId: user.permissionType
-                                for user in validated_data.userPermissions}
+        existing_user_permissions = {user['user'].airavataInternalUserId: user['permissionType']
+                                     for user in instance['userPermissions']}
+        new_user_permissions = {user['user']['airavataInternalUserId']: user['permissionType']
+                                for user in validated_data['userPermissions']}
 
         (user_grant_read_permission, user_grant_write_permission,
          user_revoke_read_permission, user_revoke_write_permission) = \
             self._compute_all_revokes_and_grants(existing_user_permissions,
                                                  new_user_permissions)
 
-        existing_group_permissions = {group.group.groupId: group.permissionType
-                                      for group in instance.groupPermissions}
-        new_group_permissions = {group.group.groupId: group.permissionType
-                                 for group in validated_data.groupPermissions}
+        existing_group_permissions = {
+            group['group'].id: group['permissionType']
+            for group in instance['groupPermissions']}
+        new_group_permissions = {
+            group['group']['id']: group['permissionType']
+            for group in validated_data['groupPermissions']}
 
         (group_grant_read_permission, group_grant_write_permission,
          group_revoke_read_permission, group_revoke_write_permission) = \
             self._compute_all_revokes_and_grants(existing_group_permissions,
                                                  new_group_permissions)
 
-        instance._user_grant_read_permission = user_grant_read_permission
-        instance._user_grant_write_permission = user_grant_write_permission
-        instance._user_revoke_read_permission = user_revoke_read_permission
-        instance._user_revoke_write_permission = user_revoke_write_permission
-        instance._group_grant_read_permission = group_grant_read_permission
-        instance._group_grant_write_permission = group_grant_write_permission
-        instance._group_revoke_read_permission = group_revoke_read_permission
-        instance._group_revoke_write_permission = group_revoke_write_permission
+        instance['_user_grant_read_permission'] = user_grant_read_permission
+        instance['_user_grant_write_permission'] = user_grant_write_permission
+        instance['_user_revoke_read_permission'] = user_revoke_read_permission
+        instance['_user_revoke_write_permission'] = user_revoke_write_permission
+        instance['_group_grant_read_permission'] = group_grant_read_permission
+        instance['_group_grant_write_permission'] = group_grant_write_permission
+        instance['_group_revoke_read_permission'] = group_revoke_read_permission
+        instance['_group_revoke_write_permission'] = group_revoke_write_permission
         return instance
 
     def _compute_all_revokes_and_grants(self, existing_permissions,
@@ -538,9 +540,9 @@ class SharedEntitySerializer(serializers.Serializer):
 
     def _compute_revokes_and_grants(self, current_permission=None,
                                     new_permission=None):
-        read_permissions = set(ResourcePermissionType.READ)
-        write_permissions = set(ResourcePermissionType.READ,
-                                ResourcePermissionType.WRITE)
+        read_permissions = set((ResourcePermissionType.READ,))
+        write_permissions = set((ResourcePermissionType.READ,
+                                ResourcePermissionType.WRITE))
         current_permissions_set = set()
         new_permissions_set = set()
         if current_permission == ResourcePermissionType.READ:
diff --git a/django_airavata/apps/api/views.py b/django_airavata/apps/api/views.py
index a792715..0b84d4f 100644
--- a/django_airavata/apps/api/views.py
+++ b/django_airavata/apps/api/views.py
@@ -803,39 +803,39 @@ class SharedEntityViewSet(mixins.RetrieveModelMixin,
 
     def perform_update(self, serializer):
         shared_entity = serializer.save()
-        entity_id = shared_entity.entityId
-        if len(shared_entity._user_grant_read_permission) > 0:
+        entity_id = shared_entity['entityId']
+        if len(shared_entity['_user_grant_read_permission']) > 0:
             self._share_with_users(
                 entity_id, ResourcePermissionType.READ,
-                shared_entity._user_grant_read_permission)
-        if len(shared_entity._user_grant_write_permission) > 0:
+                shared_entity['_user_grant_read_permission'])
+        if len(shared_entity['_user_grant_write_permission']) > 0:
             self._share_with_users(
                 entity_id, ResourcePermissionType.WRITE,
-                shared_entity._user_grant_write_permission)
-        if len(shared_entity._user_revoke_read_permission) > 0:
+                shared_entity['_user_grant_write_permission'])
+        if len(shared_entity['_user_revoke_read_permission']) > 0:
             self._revoke_from_users(
                 entity_id, ResourcePermissionType.READ,
-                shared_entity._user_revoke_read_permission)
-        if len(shared_entity._user_revoke_write_permission) > 0:
+                shared_entity['_user_revoke_read_permission'])
+        if len(shared_entity['_user_revoke_write_permission']) > 0:
             self._revoke_from_users(
                 entity_id, ResourcePermissionType.WRITE,
-                shared_entity._user_revoke_write_permission)
-        if len(shared_entity._group_grant_read_permission) > 0:
+                shared_entity['_user_revoke_write_permission'])
+        if len(shared_entity['_group_grant_read_permission']) > 0:
             self._share_with_groups(
                 entity_id, ResourcePermissionType.READ,
-                shared_entity._group_grant_read_permission)
-        if len(shared_entity._group_grant_write_permission) > 0:
+                shared_entity['_group_grant_read_permission'])
+        if len(shared_entity['_group_grant_write_permission']) > 0:
             self._share_with_groups(
                 entity_id, ResourcePermissionType.WRITE,
-                shared_entity._group_grant_write_permission)
-        if len(shared_entity._group_revoke_read_permission) > 0:
+                shared_entity['_group_grant_write_permission'])
+        if len(shared_entity['_group_revoke_read_permission']) > 0:
             self._revoke_from_groups(
                 entity_id, ResourcePermissionType.READ,
-                shared_entity._group_revoke_read_permission)
-        if len(shared_entity._group_revoke_write_permission) > 0:
+                shared_entity['_group_revoke_read_permission'])
+        if len(shared_entity['_group_revoke_write_permission']) > 0:
             self._revoke_from_groups(
                 entity_id, ResourcePermissionType.WRITE,
-                shared_entity._group_revoke_write_permission)
+                shared_entity['_group_revoke_write_permission'])
 
     def _share_with_users(self, entity_id, permission_type, user_ids):
         self.request.airavata_client.shareResourceWithUsers(
diff --git a/django_airavata/static/common/js/components/ShareButton.vue b/django_airavata/static/common/js/components/ShareButton.vue
index fa04c15..08dc3bd 100644
--- a/django_airavata/static/common/js/components/ShareButton.vue
+++ b/django_airavata/static/common/js/components/ShareButton.vue
@@ -4,7 +4,12 @@
             Share
             <b-badge>{{ totalCount }}</b-badge>
         </b-button>
-        <b-modal id="modal-share-settings" title="Sharing Settings">
+        <b-modal id="modal-share-settings" title="Sharing Settings"
+                ref="modalSharingSettings"
+                ok-title="Save" @ok="saveSharedEntity" @cancel="cancelEditSharedEntity"
+                no-close-on-esc no-close-on-backdrop hide-header-close
+                :ok-disabled="loading" :cancel-disabled="loading">
+            <b-alert variant="danger" :show="!!errorMessage">{{errorMessage}}</b-alert>
             <b-form-group label="Search for users/groups" labelFor="user-groups-autocomplete">
                 <autocomplete-text-input id="user-groups-autocomplete"
                     :suggestions="usersAndGroupsSuggestions"
@@ -66,7 +71,7 @@ export default {
     },
     data: function() {
         return {
-            sharedEntity: this.value ? this.value.clone() : new models.SharedEntity(),
+            sharedEntity: this.cloneSharedEntity(this.value),
             userFields: [
                 {key: 'name', label: 'User Name'},
                 {key: 'email', label: 'Email'},
@@ -80,6 +85,8 @@ export default {
             ],
             users: [],
             groups: [],
+            errorMessage: null,
+            loading: false,
         }
     },
     computed: {
@@ -178,12 +185,44 @@ export default {
                     'permissionType': models.ResourcePermissionType.READ
                 }));
             }
+        },
+        saveSharedEntity: function(event) {
+            // Prevent hiding the modal, hide it programmatically
+            this.errorMessage = null;
+            this.loading = true;
+            event.preventDefault();
+            services.ServiceFactory.service("SharedEntities")
+                .update({'data': this.sharedEntity, 'lookup': this.sharedEntity.entityId})
+                .then(sharedEntity => {
+                    this.emitValueChanged();
+                    this.$refs.modalSharingSettings.hide();
+                })
+                .catch(error => {
+                    console.log("Error occurred while saving:", error);
+                    this.errorMessage = "Error occurred while saving: " + JSON.stringify(error);
+                })
+                .then(() => this.loading = false, () => this.loading = false);
+        },
+        cancelEditSharedEntity: function(event) {
+            this.errorMessage = null;
+            this.sharedEntity = this.cloneSharedEntity(this.value);
+        },
+        emitValueChanged: function() {
+            this.$emit('input', this.sharedEntity);
+        },
+        cloneSharedEntity: function(sharedEntity) {
+            return sharedEntity ? sharedEntity.clone() : new models.SharedEntity();
         }
     },
     mounted: function() {
         // Load all of the groups and users
         services.ServiceFactory.service("Groups").list({limit: -1}).then(groups => this.groups
= groups);
         services.ServiceFactory.service("UserProfiles").list().then(users => this.users
= users);
+    },
+    watch: {
+        value: function(newValue) {
+            this.sharedEntity = this.cloneSharedEntity(newValue);
+        }
     }
 }
 </script>


Mime
View raw message