예제 #1
0
파일: crud.py 프로젝트: Shawn-ZWJ/ontask_b
def question_add(
    request: http.HttpRequest,
    pk: Optional[int] = None,
    workflow: Optional[models.Workflow] = None,
) -> http.JsonResponse:
    """Add a column.

    :param request: Http Request
    :param pk: Action ID where to add the question
    :param workflow: Workflow being manipulated
    :return: JSON response
    """
    if workflow.nrows == 0:
        messages.error(
            request,
            _('Cannot add question to a workflow without data'),
        )
        return http.JsonResponse({'html_redirect': ''})

    # Get the action and the columns
    action = workflow.actions.filter(pk=pk).first()
    if not action:
        messages.error(
            request,
            _('Cannot find action to add question.'),
        )
        return http.JsonResponse({'html_redirect': reverse('action:index')})
    form = forms.QuestionForm(request.POST or None, workflow=workflow)

    if request.method == 'POST' and form.is_valid():
        # Save the column object attached to the form
        column = form.save(commit=False)
        try:
            services.add_column_to_workflow(request.user, workflow, column,
                                            form.initial_valid_value,
                                            models.Log.ACTION_QUESTION_ADD,
                                            action)
            form.save_m2m()
        except OnTaskServiceException as exc:
            exc.message_to_error(request)
            exc.delete()

        return http.JsonResponse({'html_redirect': ''})

    return http.JsonResponse({
        'html_form':
        render_to_string('workflow/includes/partial_question_addedit.html', {
            'form': form,
            'is_question': True,
            'action_id': action.id,
            'add': True
        },
                         request=request),
    })
예제 #2
0
def todoitem_add(
    request: http.HttpRequest,
    pk: Optional[int] = None,
    workflow: Optional[models.Workflow] = None,
    action: Optional[models.Action] = None,
) -> http.JsonResponse:
    """Add an item to the todo list

    :param request: Http Request
    :param pk: Action ID where to add the question
    :param workflow: Workflow being manipulated
    :return: JSON response
    """
    if workflow.nrows == 0:
        messages.error(
            request,
            _('Cannot add todo items to a workflow without data'),
        )
        return http.JsonResponse({'html_redirect': ''})

    form = forms.TODOItemForm(request.POST or None, workflow=workflow)

    if request.method == 'POST' and form.is_valid():
        # Save the column object attached to the form
        column = form.save(commit=False)
        try:
            services.add_column_to_workflow(request.user, workflow, column,
                                            form.initial_valid_value,
                                            models.Log.ACTION_TODOITEM_ADD,
                                            action)
            form.save_m2m()
        except OnTaskServiceException as exc:
            exc.message_to_error(request)
            exc.delete()

        return http.JsonResponse({'html_redirect': ''})

    return http.JsonResponse({
        'html_form':
        render_to_string('column/includes/partial_todoitem_addedit.html', {
            'form': form,
            'action_id': action.id,
            'add': True
        },
                         request=request),
    })
예제 #3
0
def create(
    request: http.HttpRequest,
    workflow: Optional[models.Workflow] = None,
) -> http.JsonResponse:
    """Add a column.

    :param request: Http Request
    :param workflow: Workflow to add the column
    :return: JSON response
    """
    if workflow.nrows == 0:
        messages.error(
            request,
            _('Cannot add column to a workflow without data'),
        )
        return http.JsonResponse({'html_redirect': ''})

    form = forms.ColumnAddForm(request.POST or None, workflow=workflow)

    if request.method == 'POST' and form.is_valid():
        # Save the column object attached to the form
        column = form.save(commit=False)
        try:
            services.add_column_to_workflow(request.user, workflow, column,
                                            form.initial_valid_value)
            form.save_m2m()
        except OnTaskServiceException as exc:
            exc.message_to_error(request)
            exc.delete()

        return http.JsonResponse({'html_redirect': ''})

    return http.JsonResponse({
        'html_form':
        render_to_string('column/includes/partial_addedit.html', {
            'form': form,
            'is_question': False,
            'add': True
        },
                         request=request),
    })
예제 #4
0
def criterion_create(
    request: http.HttpRequest,
    pk: int,
    workflow: Optional[models.Workflow] = None,
    action: Optional[models.Workflow] = None,
) -> http.JsonResponse:
    """Add a new criteria to an action.

    If it is the first criteria, the form simply asks for a question with a
    non-empty category field.

    If it is not the first criteria, then the criteria are fixed by the
    previous elements in the rubric.

    :param request: Http Request
    :param pk: Action ID where to add the question
    :param workflow: Workflow being used.
    :param action: Action in which the criteria (column) is being created)
    :return: JSON response
    """
    del pk
    if action.action_type != models.Action.RUBRIC_TEXT:
        messages.error(
            request,
            _('Operation only valid or Rubric actions'),
        )
        return http.JsonResponse({'html_redirect': ''})

    if action.workflow.nrows == 0:
        messages.error(
            request,
            _('Cannot add criteria to a workflow without data'),
        )
        return http.JsonResponse({'html_redirect': ''})

    # If the request has the 'action_content', update the action
    action_content = request.POST.get('action_content')
    if action_content:
        action.set_text_content(action_content)

    # Form to read/process data
    form = forms.CriterionForm(
        request.POST or None,
        other_criterion=models.ActionColumnConditionTuple.objects.filter(
            action=action).first(),
        workflow=action.workflow)

    if request.method == 'POST' and form.is_valid():
        column = form.save(commit=False)
        try:
            services.add_column_to_workflow(
                request.user,
                workflow,
                column,
                form.initial_valid_value,
                models.Log.ACTION_RUBRIC_CRITERION_ADD,
                action)
            form.save_m2m()
        except OnTaskServiceException as exc:
            exc.message_to_error(request)
            exc.delete()

        return http.JsonResponse({'html_redirect': ''})

    return http.JsonResponse({
        'html_form': render_to_string(
            'workflow/includes/partial_criterion_addedit.html',
            {
                'form': form,
                'action_id': action.id,
                'add': True},
            request=request)})