예제 #1
0
def update_column(
    user,
    workflow: models.Workflow,
    column: models.Column,
    old_name: str,
    old_position: int,
    action_column_item: Optional[models.Action] = None,
    action_column_event: Optional[str] = None,
):
    """Update information in a column.

    :param user: User performing the operation
    :param workflow: Workflow being processed
    :param column: Column being modified
    :param old_name: Old name to detect renaming
    :param old_position: old position to detect repositioning
    :param action_column_item: Action/column item in case the action is stored
    in that table.
    :param action_column_event: Event to record the operation on the
    action/column
    :return: Nothing. Side effect in the workflow.
    """
    # If there is a new name, rename the data frame columns
    if old_name != column.name:
        sql.db_rename_column(
            workflow.get_data_frame_table_name(),
            old_name,
            column.name)
        pandas.rename_df_column(workflow, old_name, column.name)

    if old_position != column.position:
        # Update the positions of the appropriate columns
        workflow.reposition_columns(old_position, column.position)

    column.save()

    # Go back to the DB because the prefetch columns are not valid
    # any more
    workflow = models.Workflow.objects.prefetch_related('columns').get(
        id=workflow.id,
    )

    # Changes in column require rebuilding the query_builder_ops
    workflow.set_query_builder_ops()

    # Save the workflow
    workflow.save()

    if action_column_item:
        action_column_item.log(user, action_column_event)

    # Log the event
    column.log(user, models.Log.COLUMN_EDIT)
예제 #2
0
def column_edit(
    request: HttpRequest,
    pk: int,
    workflow: Optional[Workflow] = None,
    column: Optional[Column] = None,
) -> JsonResponse:
    """Edit a column.

    :param request:

    :param pk:

    :return:
    """
    # Detect if this operation is to edit a new column or a new question (in
    # the edit in page)
    is_question = 'question_edit' in request.path_info
    # Form to read/process data
    if is_question:
        form = QuestionRenameForm(request.POST or None,
                                  workflow=workflow,
                                  instance=column)
    else:
        form = ColumnRenameForm(request.POST or None,
                                workflow=workflow,
                                instance=column)

    if request.method == 'POST' and form.is_valid():
        if not form.has_changed():
            return JsonResponse({'html_redirect': None})

        # Some field changed value, so save the result, but
        # no commit as we need to propagate the info to the df
        column = form.save(commit=False)

        # If there is a new name, rename the data frame columns
        if form.old_name != form.cleaned_data['name']:
            db_rename_column(workflow.get_data_frame_table_name(),
                             form.old_name, column.name)
            rename_df_column(workflow, form.old_name, column.name)

        if form.old_position != form.cleaned_data['position']:
            # Update the positions of the appropriate columns
            workflow.reposition_columns(form.old_position, column.position)

        # Save the column information
        column.save()

        # Go back to the DB because the prefetch columns are not valid
        # any more
        workflow = Workflow.objects.prefetch_related('columns').get(
            id=workflow.id, )

        # Changes in column require rebuilding the query_builder_ops
        workflow.set_query_builder_ops()

        # Save the workflow
        workflow.save()

        # Log the event
        if is_question:
            event_type = Log.QUESTION_ADD
        else:
            event_type = Log.COLUMN_ADD
        Log.objects.register(request.user, event_type, workflow, {
            'id': workflow.id,
            'name': workflow.name,
            'column_name': column.name
        })

        # Done processing the correct POST request
        return JsonResponse({'html_redirect': ''})

    if is_question:
        template = 'workflow/includes/partial_question_addedit.html'
    else:
        template = 'workflow/includes/partial_column_addedit.html'
    return JsonResponse({
        'html_form':
        render_to_string(template, {
            'form': form,
            'cname': column.name,
            'pk': pk
        },
                         request=request),
    })
예제 #3
0
def criterion_edit(
    request: HttpRequest,
    pk: int,
    workflow: Optional[Workflow] = None,
) -> JsonResponse:
    """Edit a criterion in a rubric.

    :param request:

    :param pk: For the Action/Column/condition triplet

    :return: JSON Response
    """
    triplet = ActionColumnConditionTuple.objects.filter(pk=pk).first()
    if not triplet:
        messages.error(
            request,
            _('Incorrect invocation of criterion edit function')
        )
        return JsonResponse({'html_redirect': ''})

    action = triplet.action
    column = triplet.column
    form = CriterionForm(
        request.POST or None,
        workflow=workflow,
        other_criterion=ActionColumnConditionTuple.objects.filter(
            action=action
        ).exclude(column=column.id).first(),
        instance=column)

    if request.method == 'POST' and form.is_valid():
        if not form.has_changed():
            return JsonResponse({'html_redirect': None})

        # Some field changed value, so save the result, but
        # no commit as we need to propagate the info to the df
        column = form.save(commit=False)

        # If there is a new name, rename the data frame columns
        if form.old_name != form.cleaned_data['name']:
            db_rename_column(
                workflow.get_data_frame_table_name(),
                form.old_name,
                column.name)
            rename_df_column(workflow, form.old_name, column.name)

        if form.old_position != form.cleaned_data['position']:
            # Update the positions of the appropriate columns
            workflow.reposition_columns(form.old_position, column.position)

        # Save the column information
        column.save()

        # Go back to the DB because the prefetch columns are not valid
        # any more
        workflow = Workflow.objects.prefetch_related('columns').get(
            id=workflow.id,
        )

        # Changes in column require rebuilding the query_builder_ops
        workflow.set_query_builder_ops()

        # Save the workflow
        workflow.save()

        # Log the event
        triplet.log(request.user, Log.ACTION_RUBRIC_CRITERION_EDIT)

        # Done processing the correct POST request
        return JsonResponse({'html_redirect': ''})

    return JsonResponse({
        'html_form': render_to_string(
            'workflow/includes/partial_criterion_addedit.html',
            {'form': form,
             'cname': column.name,
             'pk': pk},
            request=request),
    })