airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal] 02/10: AIRAVATA-2711 REST API to update group admins
Date Thu, 15 Nov 2018 16:13:15 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 f39ed7fcd6b8c779cb7b35d0caf3a880d4c50f42
Author: Marcus Christie <machrist@iu.edu>
AuthorDate: Wed Nov 7 11:11:11 2018 -0500

    AIRAVATA-2711 REST API to update group admins
---
 django_airavata/apps/api/serializers.py | 36 +++++++++++++++++++++++----------
 django_airavata/apps/api/views.py       | 22 ++++++--------------
 2 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/django_airavata/apps/api/serializers.py b/django_airavata/apps/api/serializers.py
index 73d5e25..82499fc 100644
--- a/django_airavata/apps/api/serializers.py
+++ b/django_airavata/apps/api/serializers.py
@@ -138,17 +138,11 @@ class OrderedListField(serializers.ListField):
         return validated_data
 
 
-class GroupSerializer(serializers.Serializer):
+class GroupSerializer(thrift_utils.create_serializer_class(GroupModel)):
     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], allow_null=True)
-    name = serializers.CharField(required=True)
-    description = serializers.CharField(allow_null=True, allow_blank=True)
-    ownerId = serializers.CharField(read_only=True)
-    members = serializers.ListSerializer(child=serializers.CharField())
     isAdmin = serializers.SerializerMethodField()
     isOwner = serializers.SerializerMethodField()
     isMember = serializers.SerializerMethodField()
@@ -156,10 +150,15 @@ class GroupSerializer(serializers.Serializer):
     isReadOnlyGatewayAdminsGroup = serializers.SerializerMethodField()
     isDefaultGatewayUsersGroup = serializers.SerializerMethodField()
 
+    class Meta:
+        required = ('name',)
+        read_only = ('id', 'ownerId')
+
     def create(self, validated_data):
-        validated_data['ownerId'] = self.context['request'].user.username + \
+        group = super().create(validated_data)
+        group.ownerId = self.context['request'].user.username + \
             "@" + settings.GATEWAY_ID
-        return GroupModel(**validated_data)
+        return group
 
     def update(self, instance, validated_data):
         instance.name = validated_data.get('name', instance.name)
@@ -173,16 +172,31 @@ class GroupSerializer(serializers.Serializer):
         instance._removed_members = list(removed_members)
         instance._added_members = list(added_members)
         instance.members = validated_data.get('members', instance.members)
+        # Calculate added and removed admins
+        old_admins = set(instance.admins)
+        new_admins = set(validated_data.get('admins', instance.admins))
+        removed_admins = old_admins - new_admins
+        added_admins = new_admins - old_admins
+        instance._removed_admins = list(removed_admins)
+        instance._added_admins = list(added_admins)
+        instance.admins = validated_data.get('admins', instance.admins)
+        # Add new admins that aren't members to the added_members list
+        instance._added_members.extend(list(added_admins - new_members))
+        instance.members.extend(list(added_admins - new_members))
         return instance
 
     def get_isAdmin(self, group):
         request = self.context['request']
         return request.profile_service['group_manager'].hasAdminAccess(
-            request.authz_token, group.id, request.user.username + "@" + settings.GATEWAY_ID)
+            request.authz_token,
+            group.id,
+            request.user.username + "@" + settings.GATEWAY_ID)
 
     def get_isOwner(self, group):
         request = self.context['request']
-        return group.ownerId == request.user.username + "@" + settings.GATEWAY_ID
+        return group.ownerId == (request.user.username +
+                                 "@" +
+                                 settings.GATEWAY_ID)
 
     def get_isMember(self, group):
         request = self.context['request']
diff --git a/django_airavata/apps/api/views.py b/django_airavata/apps/api/views.py
index 4569d55..88fd2f2 100644
--- a/django_airavata/apps/api/views.py
+++ b/django_airavata/apps/api/views.py
@@ -79,6 +79,12 @@ class GroupViewSet(APIBackedViewSet):
         if len(group._removed_members) > 0:
             group_manager_client.removeUsersFromGroup(
                 self.authz_token, group._removed_members, group.id)
+        if len(group._added_admins) > 0:
+            group_manager_client.addGroupAdmins(
+                self.authz_token, group.id, group._added_admins)
+        if len(group._removed_admins) > 0:
+            group_manager_client.removeGroupAdmins(
+                self.authz_token, group.id, group._removed_admins)
         group_manager_client.updateGroup(self.authz_token, group)
 
     def perform_destroy(self, group):
@@ -86,22 +92,6 @@ class GroupViewSet(APIBackedViewSet):
         group_manager_client.deleteGroup(
             self.authz_token, group.id, group.ownerId)
 
-    @detail_route(methods=['post'])
-    def add_admins(self, request, group_id=None):
-        admin_ids = request.data
-        group_manager_client = self.request.profile_service['group_manager']
-        result = group_manager_client.addGroupAdmins(
-            self.authz_token, group_id, admin_ids)
-        return Response({'success': result})
-
-    @detail_route(methods=['post'])
-    def remove_admins(self, request, group_id=None):
-        admin_ids = request.data
-        group_manager_client = self.request.profile_service['group_manager']
-        result = group_manager_client.removeGroupAdmins(
-            self.authz_token, group_id, admin_ids)
-        return Response({'success': result})
-
 
 class ProjectViewSet(APIBackedViewSet):
     serializer_class = serializers.ProjectSerializer


Mime
View raw message