def get_or_create_placeholder_child(parent_entity: DatabaseEntity,
                                    child_field_name: str,
                                    child_class: Type[DatabaseEntity],
                                    **child_kwargs):
    """Checks all the entities in the |parent_entity|'s field |child_field_name|. If there is a placeholder entity,
    returns that. Otherwise creates a new placeholder entity of type |child_class| on the parent's |child_field_name|
    using |child_kw_args|.
    """
    children = parent_entity.get_field_as_list(child_field_name)
    placeholder_children = [c for c in children if is_placeholder(c)]

    if placeholder_children:
        return placeholder_children[0]

    logging.info(
        'No placeholder children on entity with id [%s] of type [%s] exist on field [%s]. Have to create one.',
        parent_entity.get_external_id(), parent_entity.get_entity_name(),
        child_field_name)
    new_child = child_class(**child_kwargs)
    if not is_placeholder(new_child):
        raise EntityMatchingError(
            f'Child created with kwargs is not a placeholder [{child_kwargs}]',
            parent_entity.get_entity_name())

    children.append(new_child)
    parent_entity.set_field_from_list(child_field_name, children)
    return new_child
Exemplo n.º 2
0
def get_external_id_keys_from_multiple_id_entity(
        entity: DatabaseEntity) -> List[str]:
    """Returns a list of strings that uniquely represent all external ids
    on the given entity.
    """
    external_str_ids = []
    for external_id in entity.get_field_as_list('external_ids'):
        str_id = external_id.id_type + '|' + external_id.external_id
        external_str_ids.append(str_id)
    return external_str_ids
Exemplo n.º 3
0
def _get_root_entity_helper(
        entity: DatabaseEntity) -> Optional[Type[DatabaseEntity]]:
    if not is_placeholder(entity):
        return entity.__class__

    for field_name in get_set_entity_field_names(
            entity, EntityFieldType.FORWARD_EDGE):
        field = entity.get_field_as_list(field_name)[0]
        result = _get_root_entity_helper(field)
        if result is not None:
            return result
    return None
Exemplo n.º 4
0
def get_all_db_objs_from_tree(db_obj: DatabaseEntity,
                              result=None) -> Set[DatabaseEntity]:
    if result is None:
        result = set()

    if db_obj in result:
        return result

    result.add(db_obj)

    set_fields = get_set_entity_field_names(
        db_obj, EntityFieldType.FORWARD_EDGE)
    for field in set_fields:
        child = db_obj.get_field_as_list(field)
        get_all_db_objs_from_trees(child, result)

    return result