예제 #1
0
파일: index.py 프로젝트: ubc/ontask_b
def get_detail_context(workflow: models.Workflow) -> Dict:
    """Create the context to render the details page.

    :param workflow: Workflow being manipulated
    :return: Dictionary to render the details page
    """
    context = {
        'workflow': workflow,
        'table_info': None}
    if workflow.has_table():
        context['table_info'] = {
            'num_rows': workflow.nrows,
            'num_cols': workflow.ncols,
            'num_actions': workflow.actions.count(),
            'num_attributes': len(workflow.attributes)}

    # put the number of key columns in the context
    context['num_key_columns'] = workflow.columns.filter(
        is_key=True,
    ).count()

    # Guarantee that column position is set for backward compatibility
    columns = workflow.columns.all()
    if any(col.position == 0 for col in columns):
        # At least a column has index equal to zero, so reset all of them
        for idx, col in enumerate(columns):
            col.position = idx + 1
            col.save(update_fields=['position'])

    return context
def _render_table_display_page(
    request: HttpRequest,
    workflow: Workflow,
    view: Optional[View],
    columns,
    ajax_url: str,
):
    """Render content of the display page.

    Function to render the content of the display page taking into account
    the columns to show and the AJAX url to use to render the table content.

    :param request: HTTP request

    :param workflow: Workflow object used to access the data frame

    :param view: View to use to render the table (or None)

    :param columns: Columns to display in the page

    :param ajax_url: URL to invoke to populate the table

    :return: HTTP Response
    """
    # Create the initial context
    context = {
        'workflow': workflow,
        'query_builder_ops': workflow.get_query_builder_ops_as_str(),
        'ajax_url': ajax_url,
        'views': workflow.views.all(),
        'no_actions': workflow.actions.count() == 0,
        'vis_scripts': PlotlyHandler.get_engine_scripts(),
    }

    # If there is a DF, add the columns
    if workflow.has_table():
        context['columns'] = columns
        context['column_types'] = str([''] +
                                      [col.data_type for col in columns])
        context['columns_datatables'] = [{
            'data': 'Operations'
        }] + [{
            'data': col.name.replace('.', '\\.')
        } for col in columns]
        context['stat_columns'] = workflow.columns.filter(is_key=False)
    else:
        context['columns'] = None
        context['columns_datatables'] = []

    # If using a view, add it to the context
    if view:
        context['view'] = view

    return render(request, 'table/display.html', context)
예제 #3
0
def edit_action_in(
    request: HttpRequest,
    workflow: Workflow,
    action: Action,
) -> HttpResponse:
    """Edit an action in.

    :param request: Request object
    :param workflow: workflow
    :param action: Action
    :return: HTTP response
    """
    # All tuples (action, column, condition) to consider
    tuples = action.column_condition_pair.all()

    # Columns
    all_columns = workflow.columns

    # Conditions
    filter_condition = action.get_filter()
    all_conditions = action.conditions.filter(is_filter=False)

    # Create the context info.
    context = {
        'action': action,
        # Workflow elements
        'total_rows': workflow.nrows,
        'query_builder_ops': workflow.get_query_builder_ops_as_str(),
        'has_data': workflow.has_table(),
        'selected_rows':
            filter_condition.n_rows_selected if filter_condition else -1,
        'all_false_conditions': any(
            cond.n_rows_selected == 0 for cond in all_conditions
        ),
        # Column elements
        'key_columns': all_columns.filter(is_key=True),
        'stat_columns': all_columns.filter(is_key=False),
        'key_selected': tuples.filter(column__is_key=True).first(),
        'has_no_key': tuples.filter(column__is_key=False).exists(),
        'any_empty_description': tuples.filter(
            column__description_text='',
            column__is_key=False,
        ).exists(),
        'columns_to_insert': all_columns.exclude(
            column_condition_pair__action=action,
        ).exclude(
            is_key=True,
        ).distinct().order_by('position'),
        'column_selected_table': ColumnSelectedTable(
            tuples.filter(column__is_key=False).values(
                'id',
                'column__id',
                'column__name',
                'column__description_text',
                'condition__name',
            ),
            orderable=False,
            extra_columns=[(
                'operations',
                OperationsColumn(
                    verbose_name='',
                    template_file=ColumnSelectedTable.ops_template,
                    template_context=lambda record: {
                        'id': record['column__id'],
                        'aid': action.id}),
            )],
            condition_list=all_conditions,
        ),
        # Conditions
        'filter_condition': filter_condition,
        'conditions': all_conditions,
        'vis_scripts': PlotlyHandler.get_engine_scripts(),
        'other_conditions': Condition.objects.filter(
            action__workflow=workflow, is_filter=False,
        ).exclude(action=action),
    }

    return render(request, 'action/edit_in.html', context)