Example #1
0
def default_merge_flat_fields(
        *, new_entity: DatabaseEntity, old_entity: DatabaseEntity) -> DatabaseEntity:
    """Merges all set non-relationship fields on the |new_entity| onto the |old_entity|. Returns the newly merged
    entity."""
    for child_field_name in get_set_entity_field_names(new_entity, EntityFieldType.FLAT_FIELD):
        if child_field_name == old_entity.get_class_id_name():
            continue
        # Do not overwrite with default status
        if child_field_name == 'status' and new_entity.has_default_status():
            continue

        old_entity.set_field(child_field_name, new_entity.get_field(child_field_name))

    return old_entity
def remove_child_from_entity(*, entity: DatabaseEntity, child_field_name: str,
                             child_to_remove: DatabaseEntity):
    """If present, removes the |child_to_remove| from the |child_field_name|
    field on the |entity|.
    """

    child_field = entity.get_field(child_field_name)

    if isinstance(child_field, list):
        if child_to_remove in child_field:
            child_field.remove(child_to_remove)
    elif isinstance(child_field, DatabaseEntity):
        if child_field == child_to_remove:
            child_field = None
    entity.set_field(child_field_name, child_field)
def add_child_to_entity(*, entity: DatabaseEntity, child_field_name: str,
                        child_to_add: DatabaseEntity):
    """Adds the |child_to_add| to the |child_field_name| field on the
    |entity|.
    """

    child_field = entity.get_field(child_field_name)

    if isinstance(child_field, list):
        if child_to_add not in child_field:
            child_field.append(child_to_add)
    else:
        if child_field and child_field != child_to_add:
            raise EntityMatchingError(
                f"Attempting to add child {child_to_add} to entity {entity}, "
                f"but {child_field_name} already had different value "
                f"{child_field}", entity.get_entity_name())
        child_field = child_to_add
        entity.set_field(child_field_name, child_field)
Example #4
0
def convert_to_placeholder(entity: DatabaseEntity):
    for field_name in get_set_entity_field_names(entity, EntityFieldType.FLAT_FIELD):
        if field_name == entity.get_class_id_name():
            continue
        if field_name == 'state_code':
            continue
        if field_name == 'status':
            entity.set_field(field_name, enum_canonical_strings.present_without_info)
            continue
        if field_name == 'incarceration_type':
            entity.set_field(field_name, StateIncarcerationType.STATE_PRISON.value)
            continue
        if field_name == 'court_type':
            entity.set_field(field_name, StateCourtType.PRESENT_WITHOUT_INFO.value)
            continue
        if field_name == 'agent_type':
            entity.set_field(field_name, StateAgentType.PRESENT_WITHOUT_INFO.value)
            continue
        entity.clear_field(field_name)