示例#1
0
def get_row_values(
    action: Action,
    row_idx: Union[int, Tuple[str, str]],
) -> Dict[str, Union[str, int, float, datetime]]:
    """Get the values in a row either by index or by key.

    Given an action and a row index, obtain the appropriate row of values
    from the data frame.

    :param action: Action object
    :param row_idx: Row index to use for evaluation
    :return Dictionary with the data row
    """
    # Step 1: Get the row of data from the DB
    filter_formula = action.get_filter_formula()

    # If row_idx is an integer, get the data by index, otherwise, by key
    if isinstance(row_idx, int):
        row = get_table_row_by_index(
            action.workflow,
            filter_formula,
            row_idx,
        )
    else:

        row = dataops.sql.row_queries.get_row(
            action.workflow.get_data_frame_table_name(),
            row_idx[0],
            row_idx[1],
            column_names=action.workflow.get_column_names(),
            filter_formula=filter_formula,
        )
    return row
示例#2
0
def evaluate_action(
    action: Action,
    extra_string: str = None,
    column_name: str = None,
    exclude_values: List[str] = None,
) -> List[List]:
    """Evaluate the content in an action based on the values in the columns.

    Given an action object and an optional string:
    1) Access the attached workflow
    2) Obtain the data from the appropriate data frame
    3) Loop over each data row and
      3.1) Evaluate the conditions with respect to the values in the row
      3.2) Create a context with the result of evaluating the conditions,
           attributes and column names to values
      3.3) Run the template with the context
      3.4) Run the optional string argument with the template and the context
      3.5) Select the optional column_name
    4) Return the resulting objects:
       List of (HTMLs body, extra string, column name value)
        or an error message

    :param action: Action object with pointers to conditions, filter,
                   workflow, etc.
    :param extra_string: An extra string to process (something like the email
           subject line) with the same dictionary as the text in the action.
    :param column_name: Column from where to extract the special value (
           typically the email address) and include it in the result.
    :param exclude_values: List of values in the column to exclude
    :return: list of lists resulting from the evaluation of the action
    """
    # Get the table data
    rows = get_rows(action.workflow.get_data_frame_table_name(),
                    filter_formula=action.get_filter_formula())
    list_of_renders = []
    for row in rows:
        if exclude_values and str(row[column_name]) in exclude_values:
            # Skip the row with the col_name in exclude values
            continue

        # Step 4: Create the context with the attributes, the evaluation of the
        # conditions and the values of the columns.
        context = get_action_evaluation_context(action, row)

        # Append result
        list_of_renders.append(
            render_tuple_result(action, context, extra_string, column_name), )

    # Check field n_rows_selected (table may have been modified)
    action_filter = action.get_filter()
    if action_filter and action_filter.n_rows_selected != rows.rowcount:
        # Filter now returns different number of rows. Action conditions need
        # to be refreshed
        action_filter.n_rows_selected = rows.rowcount
        action.update_n_rows_selected(filter_formula=action_filter.formula)

    return list_of_renders