Skip to content
Snippets Groups Projects
person.py 4.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • from .utils import VERBOSE
    
    
    class Person(OpenBisObject):
    
    
        def __init__(self, openbis_obj, data=None, **kwargs):
    
            self.__dict__["openbis"] = openbis_obj
            self.__dict__["a"] = AttrHolder(openbis_obj, "person")
    
    
            if kwargs is not None:
                for key in kwargs:
                    setattr(self, key, kwargs[key])
    
        def __dir__(self):
            """all the available methods and attributes that should be displayed
            when using the autocompletion feature (TAB) in Jupyter
            """
            return [
    
                "permId",
                "userId",
                "firstName",
                "lastName",
                "email",
                "registrator",
                "registrationDate",
                "space",
                "get_roles()",
                "assign_role(role, space)",
                "revoke_role(role)",
    
            """Get all roles that are assigned to this person.
    
            Provide additional search arguments to refine your search.
    
            Usage::
                person.get_roles()
                person.get_roles(space='TEST_SPACE')
            """
            return self.openbis.get_role_assignments(person=self, **search_args)
    
        def assign_role(self, role, **kwargs):
            try:
                self.openbis.assign_role(role=role, person=self, **kwargs)
                if VERBOSE:
    
                    print(f"Role {role} successfully assigned to person {self.userId}")
    
                        print(f"Role {role} already assigned to person {self.userId}")
    
        def revoke_role(self, role, space=None, project=None, reason="no reason specified"):
            """Revoke a role from this person."""
    
    
            techId = None
            if isinstance(role, int):
                techId = role
            else:
    
                    if isinstance(space, str):
                        query["space"] = space.upper()
                    else:
                        query["space"] = space.code.upper()
    
                    if isinstance(project, str):
                        query["project"] = project.upper()
                    else:
                        query["project"] = project.code.upper()
    
                querystr = " & ".join(f'{key} == "{value}"' for key, value in query.items())
    
                roles = self.get_roles().df
                if len(roles) == 0:
                    if VERBOSE:
    
                        print(
                            f"Role {role} has already been revoked from person {self.code}"
                        )
    
                techId = roles.query(querystr)["techId"].values[0]
    
    
            # finally delete the role assignment
            ra = self.openbis.get_role_assignment(techId)
            ra.delete(reason)
            if VERBOSE:
    
                print(f"Role {role} successfully revoked from person {self.code}")
    
            return f'{self.get("firstName")} {self.get("lastName")}'
    
    
        def delete(self, reason):
            raise ValueError("Persons cannot be deleted")
    
        def save(self):
            if self.is_new:
                request = self._new_attrs()
                resp = self.openbis._post_request(self.openbis.as_v3, request)
    
                if VERBOSE:
                    print("Person successfully created.")
                new_person_data = self.openbis.get_person(resp[0]["permId"], only_data=True)
    
                self._set_data(new_person_data)
                return self
    
            else:
                request = self._up_attrs()
                self.openbis._post_request(self.openbis.as_v3, request)
    
                if VERBOSE:
                    print("Person successfully updated.")
    
                new_person_data = self.openbis.get_person(self.permId, only_data=True)
                self._set_data(new_person_data)