def get_relationship_info(db, target_type, source_type, source_entity_ids, includes_data): """Get information related to relationships between different entities. Keep in mind that includes_data (dict) is altered to contain the relationship objects keyed by the source entity MBIDs. Args: db (Session object): Session object. target_type (str): Type of target entity. source_type (str): Type of source entity. source_entity_ids (list): IDs of the source entity. includes_data (dict): Dictionary containing includes data of entities. """ source_model = ENTITY_MODELS[source_type] target_model = ENTITY_MODELS[target_type] relation = get_link_model(source_model, target_model) query = db.query(relation).\ options(joinedload("link", innerjoin=True)).\ options(joinedload("link.link_type", innerjoin=True)) if relation.entity0.property.mapper.class_ == relation.entity1.property.mapper.class_: _relationship_link_helper(relation, query, "entity0", "entity1", target_type, source_entity_ids, includes_data) _relationship_link_helper(relation, query, "entity1", "entity0", target_type, source_entity_ids, includes_data) else: if source_model == relation.entity0.property.mapper.class_: _relationship_link_helper(relation, query, "entity0", "entity1", target_type, source_entity_ids, includes_data) else: _relationship_link_helper(relation, query, "entity1", "entity0", target_type, source_entity_ids, includes_data)
def _load_links_by_types(db, objs, attr, source_model, target_type, include): target_model = get_entity_type_model(target_type) model = get_link_model(source_model, target_model) query = db.query(model).\ options(joinedload("link", innerjoin=True)).\ options(joinedload("link.link_type", innerjoin=True)) if model.entity0.property.mapper.class_ == model.entity1.property.mapper.class_: _load_links_by_types_one_side(model, query, objs, attr, include, "entity0", "entity1", target_type) _load_links_by_types_one_side(model, query, objs, attr, include, "entity1", "entity0", target_type) else: if source_model == model.entity0.property.mapper.class_: _load_links_by_types_one_side(model, query, objs, attr, include, "entity0", "entity1", target_type) else: _load_links_by_types_one_side(model, query, objs, attr, include, "entity1", "entity0", target_type)