def related_people(related_type, related_ids): """Get people related to the specified object. Returns the following people: for each object type: the users mapped via PeopleObjects, for Program: the users that have a Program-wide role, for Audit: the users that have a Program-wide or Audit-wide role, for Workflow: the users mapped via WorkflowPeople and the users that have a Workflow-wide role. Args: related_type: the name of the class of the related objects. related_ids: the ids of related objects. Returns: sqlalchemy.sql.elements.BinaryExpression if an object of `object_class` is related to the given users. """ if "Person" not in [object_class.__name__, related_type]: return sa.sql.false() model = inflector.get_model(related_type) res = [] res.extend( RelationshipHelper.person_object( object_class.__name__, related_type, related_ids, )) if related_type in ('Program', 'Audit'): res.extend( db.session.query(UserRole.person_id).join( model, sa.and_( UserRole.context_id == model.context_id, model.id.in_(related_ids), ))) if related_type == "Audit": res.extend( db.session.query(UserRole.person_id).join( models.Program, UserRole.context_id == models.Program.context_id, ).join( model, sa.and_( models.Program.id == model.program_id, model.id.in_(related_ids), ))) if "Workflow" in (object_class.__name__, related_type): try: from ggrc_workflows.models import (relationship_helper as wf_relationship_handler) except ImportError: # ggrc_workflows module is not enabled return sa.sql.false() else: res.extend( wf_relationship_handler.workflow_person( object_class.__name__, related_type, related_ids, )) if res: return object_class.id.in_([obj[0] for obj in res]) return sa.sql.false()
def related_people(exp, object_class, target_class, query): """Get people related to the specified object. Returns the following people: for each object type: the users mapped via PeopleObjects, for Program: the users that have a Program-wide role, for Audit: the users that have a Program-wide or Audit-wide role, for Workflow: the users mapped via WorkflowPeople and the users that have a Workflow-wide role. Args: related_type: the name of the class of the related objects. related_ids: the ids of related objects. Returns: sqlalchemy.sql.elements.BinaryExpression if an object of `object_class` is related to the given users. """ if "Person" not in [object_class.__name__, exp['object_name']]: return sqlalchemy.sql.false() model = inflector.get_model(exp['object_name']) res = [] res.extend(relationship_helper.person_object( object_class.__name__, exp['object_name'], exp['ids'], )) if exp['object_name'] in ('Program', 'Audit'): res.extend( db.session.query(UserRole.person_id).join(model, sqlalchemy.and_( UserRole.context_id == model.context_id, model.id.in_(exp['ids']), )) ) if exp['object_name'] == "Audit": res.extend( db.session.query(UserRole.person_id).join( models.Program, UserRole.context_id == models.Program.context_id, ).join(model, sqlalchemy.and_( models.Program.id == model.program_id, model.id.in_(exp['ids']), )) ) if "Workflow" in (object_class.__name__, exp['object_name']): try: from ggrc_workflows.models import (relationship_helper as wf_relationship_handler) except ImportError: # ggrc_workflows module is not enabled return sqlalchemy.sql.false() else: res.extend(wf_relationship_handler.workflow_person( object_class.__name__, exp['object_name'], exp['ids'], )) if res: return object_class.id.in_([obj[0] for obj in res]) return sqlalchemy.sql.false()