Exemplo n.º 1
0
def get_concept_relationship(db: PartitionedDatabase,
                             relationship_id_or_name: str) -> JsonDict:
    relationship = None
    with db.transaction() as tx:
        try:
            if is_model_relationship_id(relationship_id_or_name):
                # Relationship ID:
                id_: ModelRelationshipId = get_model_relationship_id(
                    relationship_id_or_name)
                relationship = db.get_model_relationship_tx(
                    tx, id_) or db.get_model_relationship_stub_tx(tx, id_)

            else:
                # DANGER: Do *not* change the order of this. Relationship stubs must
                # be returned *first* to not break compatibility with the Python client.
                # See https://app.clickup.com/t/426zh9 for motivation.

                # Relationship name:
                relation = to_relationship_id_or_name(relationship_id_or_name)

                try:
                    relationship = db.get_model_relationship_stub_tx(
                        tx=tx, relation=relation)
                except OperationError:
                    relationships = db.get_model_relationships_tx(
                        tx=tx, relation=relation, one_to_many=True)
                    if relationships:
                        relationship = relationships[0]
                    else:
                        relationship = None

        except OperationError:
            # Treat as not found
            pass

    if relationship is None:
        raise NotFound(
            f"Could not get model relationship [{relationship_id_or_name}]")

    return to_legacy_relationship(relationship)
Exemplo n.º 2
0
def get_concept_instance_relationships(
        db: PartitionedDatabase,
        relationship_id_or_name: ModelRelationshipId) -> List[JsonDict]:
    with db.transaction() as tx:
        try:
            stub = db.get_model_relationship_stub_tx(
                tx=tx, relation=relationship_id_or_name)
        except OperationError:
            stub = None

        if stub == None:
            return [
                to_legacy_relationship_instance(r)
                for r in db.get_record_relationships_by_model_tx(
                    tx, relationship_id_or_name)
            ]
        else:
            return [
                to_legacy_relationship_instance(r) for r in
                db.get_record_relationships_by_model_with_relationship_stub_tx(
                    tx, stub)
            ]