Пример #1
0
    def post(self, request, project_id, name):
        """

        :param request:
        :return:
        """
        ag = get_service_account_client()

        if request.is_ajax():
            post_data = json.loads(request.body)
        else:
            post_data = request.POST.copy()
        entity = post_data.get('entity')
        try:
            model_cls = self._lookup_model(name)
            logger.debug('entity: %s', entity)
            model = model_cls(value=entity)
            logger.debug('model uuid: %s', model.uuid)
            file_uuids = []
            if 'filePaths' in entity:
                file_paths = entity.get('filePaths', [])
                project_system = ''.join(['project-', project_id])
                user_ag = request.user.agave_oauth.client
                for file_path in file_paths:
                    file_obj = BaseFileResource.listing(
                        user_ag, project_system, file_path)

                    file_uuids.append(file_obj.uuid)
                for file_uuid in file_uuids:
                    model.files.add(file_uuid)
                model.associate(file_uuids)
            model.project.add(project_id)
            model.associate(project_id)
            saved = model.save(ag)
            resp = model_cls(**saved)
            #TODO: This should happen in a celery task and implemented in a manager
            #Get project's metadata permissions
            pems = BaseMetadataPermissionResource.list_permissions(
                project_id, ag)
            #Loop permissions and set them in whatever metadata object we're saving
            for pem in pems:
                _pem = BaseMetadataPermissionResource(resp.uuid, ag)
                _pem.username = pem.username
                _pem.read = pem.read
                _pem.write = pem.write
                _pem.save()

        except ValueError:
            return HttpResponseBadRequest('Entity not valid.')

        return JsonResponse(resp.to_body_dict(), safe=False)
Пример #2
0
    def team_members(self):
        permissions = BaseMetadataPermissionResource.list_permissions(
            self.uuid, self._agave)
        pi = self.pi

        co_pis = getattr(self, 'co_pis', [])
        logger.info(co_pis)
        # co_pis = [x.username for x in permissions if x.username in co_pis_list]

        team_members_list = [
            x.username for x in permissions if x.username not in co_pis + [pi]
        ]
        return {'pi': pi, 'coPis': co_pis, 'teamMembers': team_members_list}
Пример #3
0
    def post(self, request, project_id, name):
        """
        Create project related metadata object.

        :param request:
        :return:
        """
        sa_client = get_service_account_client()

        if request.is_ajax():
            post_data = json.loads(request.body)
        else:
            post_data = request.POST.copy()
        entity = post_data.get('entity')
        try:
            model_cls = project_lookup_model(name=name)
            model = model_cls(value=entity)
            file_uuids = []
            if 'filePaths' in entity:
                file_paths = entity.get('filePaths', [])
                project_system = ''.join(['project-', project_id])
                client = request.user.agave_oauth.client
                for file_path in file_paths:
                    file_obj = BaseFileResource.listing(
                        client, project_system, file_path)

                    file_uuids.append(file_obj.uuid)
                for file_uuid in file_uuids:
                    model.files.add(file_uuid)
                model.associate(file_uuids)
            model.project.add(project_id)
            model.associate(project_id)
            saved = model.save(sa_client)
            resp = model_cls(**saved)
            # TODO: We should stop using these "Resources" and just use agavepy methods.
            pems = BaseMetadataPermissionResource.list_permissions(
                project_id, sa_client)
            # Loop permissions and set them in whatever metadata object we're saving
            for pem in pems:
                _pem = BaseMetadataPermissionResource(resp.uuid, sa_client)
                _pem.username = pem.username
                _pem.read = pem.read
                _pem.write = pem.write
                _pem.save()

        except ValueError:
            return HttpResponseBadRequest('Entity not valid.')

        return JsonResponse(resp.to_body_dict(), safe=False)
Пример #4
0
    def add_collaborator(self, username):
        logger.info('Adding collaborator "{}" to project "{}"'.format(username, self.uuid))

        # Set permissions on the metadata record
        pem = BaseMetadataPermissionResource(self.uuid, self._agave)
        pem.username = username
        pem.read = True
        pem.write = True
        pem.save()

        # Set roles on project system
        self.project_system.add_role(username, system_roles.USER)
Пример #5
0
    def match_pems_to_project(self, project_uuid = None):
        project_uuid = project_uuid or self.value.get('projectUUID', self.value.get('projectUuid'))
        logger.debug('matchins pems to project: %s', project_uuid)
        if not project_uuid:
            return self

        project_roles = self._agave.systems.listRoles(systemId='project-{}'.format(project_uuid))
        project_roles = filter(lambda x: x['username'] != 'ds_admin', project_roles)
        meta_pems = BaseMetadataPermissionResource.list_permissions(self.uuid, self._agave)
        meta_pems_users = self._update_pems_with_system_roles(project_roles, meta_pems)
        for username, pem in six.iteritems(meta_pems_users):
            pem.delete()

        return self
Пример #6
0
    def _update_pems_with_system_roles(self, system_roles, meta_pems):
        """Updates this metadata object's permissions with those of a system's roles

            :param list system_roles: A list of :class:`dict` representing the user roles of a system.
             This should be the response from :func:`~agavepy.agave.Agave.systems.listRoles`.
            :param list meta_pems: A list of
             :class:`~designsafe.apps.data.models.agave.metadata.BaseMetadataPermissionResource`.
             This should be the response from
             :func:`~designsafe.apps.data.models.agave.metadata.BaseMetadataPermissionResource.list_permissions`

            :returns: A :class:`dict` where the keys are the usernames who do not have a role set on the system and the value
             of each key is the :class:`~designsafe.apps.data.models.agave.metadata.BaseMetadataPermissionResource` object.
            :rtype: dict
        """
        meta_pems_users = {pem.username: pem for pem in meta_pems}
        for role_obj in system_roles:
            username = role_obj['username']
            role = role_obj['role']
            try:
                pem = meta_pems_users.pop(username)
            except KeyError:
                pem = BaseMetadataPermissionResource(self.uuid, self._agave)
                pem.username = username

            if role == system_roles_list.GUEST and \
                (not pem.read or pem.write):
                pem.read = True
                pem.save()
                logger.debug('Created or Updated %s', pem)
            elif role == system_roles_list.USER and \
                (not pem.read or pem.write):
                pem.read = True
                pem.write = True
                pem.save()
                logger.debug('Created or Updated %s', pem)

        return meta_pems_users
Пример #7
0
    def remove_collaborator(self, username):
        logger.info('Removing collaborator "{}" from project "{}"'.format(username, self.uuid))

        team_members = self.value.get('teamMembers', [])
        # logger.info(coPis)
        team_members = [uname for uname in team_members if uname != username]

        self.value['teamMembers'] = team_members

        # Set permissions on the metadata record
        pem = BaseMetadataPermissionResource(self.uuid, self._agave)
        pem.username = username
        pem.read = False
        pem.write = False
        pem.save()

        # Set roles on project system
        self.project_system.remove_role(username)
Пример #8
0
 def collaborators(self):
     permissions = BaseMetadataPermissionResource.list_permissions(
         self.uuid, self._agave)
     return [pem.username for pem in permissions]