Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TP2000-1602 Implement workflow delete view #1342

Draft
wants to merge 3 commits into
base: TP2000-1555-impl-workflow-detail-view
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tasks/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from common.validators import SymbolValidator
from tasks.models import Task
from tasks.models import TaskTemplate
from tasks.models import TaskWorkflow
from tasks.models import TaskWorkflowTemplate
from workbaskets.models import WorkBasket

Expand Down Expand Up @@ -158,6 +159,9 @@ def __init__(self, *args, **kwargs):
)


TaskWorkflowDeleteForm = delete_form_for(TaskWorkflow)


class TaskWorkflowTemplateBaseForm(ModelForm):
class Meta:
model = TaskWorkflowTemplate
Expand Down
49 changes: 49 additions & 0 deletions tasks/jinja2/tasks/workflows/confirm_delete.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends "layouts/confirm.jinja" %}

{% from "components/breadcrumbs/macro.njk" import govukBreadcrumbs %}
{% from "components/panel/macro.njk" import govukPanel %}
{% from "components/button/macro.njk" import govukButton %}

{% set page_title = verbose_name|capitalize ~ " deleted" %}

{% if verbose_name == "workflow" %}
{% set list_url = "#TODO" %}
{% set create_url = url("workflow:task-workflow-ui-create") %}
{% elif verbose_name == "workflow template" %}
{% set list_url = "#TODO" %}
{% set create_url = url("workflow:task-workflow-template-ui-create") %}
{% endif %}

{% block breadcrumb %}
{{ breadcrumbs(
request,
[
{"text": "Find and view "~ verbose_name ~ "s", "href": list_url},
{"text": page_title}
],
False,
) }}
{% endblock %}

{% block panel %}
{{ govukPanel({
"titleText": verbose_name|capitalize ~ " ID: " ~ deleted_pk,
"text": verbose_name|capitalize ~ " has been deleted",
"classes": "govuk-!-margin-bottom-7"
}) }}
{% endblock %}

{% block button_group %}
{{ govukButton({
"text": "Find and view " ~ verbose_name ~ "s",
"href": list_url,
"classes": "govuk-button"
}) }}
{{ govukButton({
"text": "Create a " ~ verbose_name,
"href": create_url,
"classes": "govuk-button--secondary"
}) }}
{% endblock %}

{% block actions %}{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
{% from "components/warning-text/macro.njk" import govukWarningText %}
{% from "components/button/macro.njk" import govukButton %}

{% set page_title = "Delete workflow template: " ~ object.title %}
{% set object_name = object._meta.verbose_name %}
{% set page_title = "Delete " ~ object_name ~ ": " ~ object.title %}

{% block breadcrumb %}
{{ breadcrumbs(
request,
[
{"text": "Find and view workflow templates", "href": "#TODO"},
{"text": "Find and view "~ object_name ~ "s", "href": object.get_url("list")},
{
"text": "Workflow template: " ~ object.title,
"text": object_name|capitalize ~ ": " ~ object.title,
"href": object.get_url("detail"),
},
{"text": page_title}
Expand All @@ -23,7 +24,7 @@

{% block form %}
{{ govukWarningText({
"text": "Are you sure you want to delete this workflow template?"
"text": "Are you sure you want to delete this " ~ object_name ~ "?",
}) }}

{% call django_form(action=object.get_url("delete")) %}
Expand Down
42 changes: 0 additions & 42 deletions tasks/jinja2/tasks/workflows/template_confirm_delete.jinja

This file was deleted.

10 changes: 5 additions & 5 deletions tasks/models/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def get_url(self, action: str = "detail"):
# "workflow:task-workflow-ui-update",
# kwargs={"pk": self.pk},
# )
# elif action == "delete":
# return reverse(
# "workflow:task-workflow-ui-delete",
# kwargs={"pk": self.pk},
# )
elif action == "delete":
return reverse(
"workflow:task-workflow-ui-delete",
kwargs={"pk": self.pk},
)
elif action == "create":
return reverse(
"workflow:task-workflow-ui-create",
Expand Down
41 changes: 40 additions & 1 deletion tasks/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from common.tests.factories import TaskFactory
from tasks.forms import TaskWorkflowCreateForm
from tasks.models import ProgressState
from tasks.models import Task
from tasks.models import TaskItem
from tasks.models import TaskItemTemplate
from tasks.models import TaskLog
from tasks.models import TaskTemplate
Expand Down Expand Up @@ -354,7 +356,7 @@ def test_workflow_template_delete_view(
task_workflow_template_single_task_template_item,
):
"""Tests that a workflow template can be deleted (along with related
TaskItemPosition and TaskTemplate objects) and that the corresponding
TaskItemTemplate and TaskTemplate objects) and that the corresponding
confirmation view returns a HTTP 200 response."""

task_workflow_template_pk = task_workflow_template_single_task_template_item.pk
Expand Down Expand Up @@ -686,3 +688,40 @@ def test_workflow_create_view(

soup = BeautifulSoup(str(confirmation_response.content), "html.parser")
assert str(created_workflow) in soup.select("h1.govuk-panel__title")[0].text


def test_workflow_delete_view(
valid_user_client,
task_workflow_single_task_item,
):
"""Tests that a workflow can be deleted (along with related TaskItem and
Task objects) and that the corresponding confirmation view returns a HTTP
200 response."""

workflow_pk = task_workflow_single_task_item.pk
summary_task_pk = task_workflow_single_task_item.summary_task.pk
task_pk = task_workflow_single_task_item.get_tasks().get().pk

delete_url = task_workflow_single_task_item.get_url("delete")
delete_response = valid_user_client.post(delete_url)
assert delete_response.status_code == 302

assert not TaskWorkflow.objects.filter(
pk=workflow_pk,
).exists()
assert not TaskItem.objects.filter(
queue_id=workflow_pk,
).exists()
assert not Task.objects.filter(pk__in=[summary_task_pk, task_pk]).exists()

confirmation_url = reverse(
"workflow:task-workflow-ui-confirm-delete",
kwargs={"pk": workflow_pk},
)
assert delete_response.url == confirmation_url

confirmation_response = valid_user_client.get(confirmation_url)
assert confirmation_response.status_code == 200

soup = BeautifulSoup(str(confirmation_response.content), "html.parser")
assert f"Workflow ID: {workflow_pk}" in soup.select(".govuk-panel__title")[0].text
10 changes: 10 additions & 0 deletions tasks/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@
views.TaskWorkflowConfirmCreateView.as_view(),
name="task-workflow-ui-confirm-create",
),
path(
"<int:pk>/delete/",
views.TaskWorkflowDeleteView.as_view(),
name="task-workflow-ui-delete",
),
path(
"<int:pk>/confirm-delete/",
views.TaskWorkflowConfirmDeleteView.as_view(),
name="task-workflow-ui-confirm-delete",
),
]

workflow_template_ui_patterns = [
Expand Down
44 changes: 42 additions & 2 deletions tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from tasks.forms import TaskTemplateUpdateForm
from tasks.forms import TaskUpdateForm
from tasks.forms import TaskWorkflowCreateForm
from tasks.forms import TaskWorkflowDeleteForm
from tasks.forms import TaskWorkflowTemplateCreateForm
from tasks.forms import TaskWorkflowTemplateDeleteForm
from tasks.forms import TaskWorkflowTemplateUpdateForm
Expand Down Expand Up @@ -382,6 +383,44 @@ class TaskWorkflowConfirmCreateView(PermissionRequiredMixin, DetailView):
permission_required = "tasks.add_taskworkflow"


class TaskWorkflowDeleteView(PermissionRequiredMixin, DeleteView):
model = TaskWorkflow
template_name = "tasks/workflows/delete.jinja"
permission_required = "tasks.delete_taskworkflow"
form_class = TaskWorkflowDeleteForm

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["instance"] = self.object
return kwargs

@transaction.atomic
def form_valid(self, form):
summary_task = self.object.summary_task
self.object.get_tasks().delete()
result = super().form_valid(form)
summary_task.delete()
return result

def get_success_url(self):
return reverse(
"workflow:task-workflow-ui-confirm-delete",
kwargs={"pk": self.object.pk},
)


class TaskWorkflowConfirmDeleteView(PermissionRequiredMixin, TemplateView):
model = TaskWorkflow
template_name = "tasks/workflows/confirm_delete.jinja"
permission_required = "tasks.change_taskworkflow"

def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data["verbose_name"] = "workflow"
context_data["deleted_pk"] = self.kwargs["pk"]
return context_data


class TaskWorkflowTemplateDetailView(PermissionRequiredMixin, DetailView):
model = TaskWorkflowTemplate
template_name = "tasks/workflows/detail.jinja"
Expand Down Expand Up @@ -505,7 +544,7 @@ class TaskWorkflowTemplateConfirmUpdateView(PermissionRequiredMixin, DetailView)

class TaskWorkflowTemplateDeleteView(PermissionRequiredMixin, DeleteView):
model = TaskWorkflowTemplate
template_name = "tasks/workflows/template_delete.jinja"
template_name = "tasks/workflows/delete.jinja"
permission_required = "tasks.delete_taskworkflowtemplate"
form_class = TaskWorkflowTemplateDeleteForm

Expand All @@ -528,11 +567,12 @@ def get_success_url(self):

class TaskWorkflowTemplateConfirmDeleteView(PermissionRequiredMixin, TemplateView):
model = TaskWorkflowTemplate
template_name = "tasks/workflows/template_confirm_delete.jinja"
template_name = "tasks/workflows/confirm_delete.jinja"
permission_required = "tasks.change_taskworkflowtemplate"

def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data["verbose_name"] = "workflow template"
context_data["deleted_pk"] = self.kwargs["pk"]
return context_data

Expand Down