diff --git a/tasks/forms.py b/tasks/forms.py index 2dc71dceb..7c60325b8 100644 --- a/tasks/forms.py +++ b/tasks/forms.py @@ -112,3 +112,6 @@ def __init__(self, *args, **kwargs): class TaskTemplateUpdateForm(TaskTemplateFormBase): def __init__(self, *args, **kwargs): super().__init__(*args, submit_title="Update", **kwargs) + + +TaskTemplateDeleteForm = delete_form_for(TaskTemplate) diff --git a/tasks/jinja2/tasks/workflows/task_template_confirm_delete.jinja b/tasks/jinja2/tasks/workflows/task_template_confirm_delete.jinja new file mode 100644 index 000000000..d185378af --- /dev/null +++ b/tasks/jinja2/tasks/workflows/task_template_confirm_delete.jinja @@ -0,0 +1,58 @@ +{% 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 = "Task template deleted" %} + + +{% block breadcrumb %} + {{ breadcrumbs( + request, + [ + {"text": "Find and view workflow templates", "href": "#TODO"}, + { + "text": "Workflow template: " ~ task_workflow_template.title, + "href": url( + "workflow:task-workflow-template-ui-detail", + kwargs={"pk": task_workflow_template.pk}, + ), + }, + {"text": page_title} + ], + False, + ) }} +{% endblock %} + +{% block panel %} + {{ govukPanel({ + "titleText": "Task template ID: " ~ deleted_pk, + "text": "Task template has been deleted", + "classes": "govuk-!-margin-bottom-7" + }) }} +{% endblock %} + +{% block button_group %} + {{ govukButton({ + "text": "View workflow template", + "href": url( + "workflow:task-workflow-template-ui-detail", + kwargs={"pk": task_workflow_template.pk}, + ), + "classes": "govuk-button" + }) }} + {{ govukButton({ + "text": "Return to homepage", + "href": url("home"), + "classes": "govuk-button--secondary" + }) }} +{% endblock %} + +{% block actions %} +
  • + Create a task template +
  • +{% endblock %} diff --git a/tasks/jinja2/tasks/workflows/task_template_delete.jinja b/tasks/jinja2/tasks/workflows/task_template_delete.jinja new file mode 100644 index 000000000..206700b56 --- /dev/null +++ b/tasks/jinja2/tasks/workflows/task_template_delete.jinja @@ -0,0 +1,40 @@ +{% extends "common/delete.jinja" %} + +{% from "components/breadcrumbs.jinja" import breadcrumbs %} +{% from "components/warning-text/macro.njk" import govukWarningText %} +{% from "components/button/macro.njk" import govukButton %} + +{% set page_title = "Delete task template:" ~ object.title %} + + +{% block breadcrumb %} + {{ breadcrumbs( + request, + [ + {"text": "Find and view workflow templates", "href": "#TODO"}, + { + "text": "Workflow template: " ~ task_workflow_template.title, + "href": url("workflow:task-workflow-template-ui-detail", kwargs={"pk": task_workflow_template.pk}), + }, + {"text": page_title} + ], + False, + ) }} +{% endblock %} + +{% block form %} + {{ govukWarningText({ + "text": "Are you sure you want to delete this task template?" + }) }} + + {% call django_form( + action=url( + "workflow:task-template-ui-delete", + kwargs={ + "workflow_template_pk": task_workflow_template.pk, + "pk": object.pk, + }), + ) %} + {{ crispy(form) }} + {% endcall %} +{% endblock %} diff --git a/tasks/jinja2/tasks/workflows/task_template_detail.jinja b/tasks/jinja2/tasks/workflows/task_template_detail.jinja index d8c672761..c2ce54206 100644 --- a/tasks/jinja2/tasks/workflows/task_template_detail.jinja +++ b/tasks/jinja2/tasks/workflows/task_template_detail.jinja @@ -56,7 +56,14 @@
    {{ govukButton({ "text": "Delete task template", - "href": "#TODO", + "href": url( + "workflow:task-template-ui-delete", + kwargs={ + "workflow_template_pk": task_workflow_template.pk, + "pk": object.pk, + }, + ), + "classes": "govuk-button--warning" }) }}
    diff --git a/tasks/tests/test_views.py b/tasks/tests/test_views.py index f2815fb21..67a541f2d 100644 --- a/tasks/tests/test_views.py +++ b/tasks/tests/test_views.py @@ -7,7 +7,10 @@ from common.tests.factories import SubTaskFactory from common.tests.factories import TaskFactory from tasks.models import ProgressState +from tasks.models import TaskItemTemplate from tasks.models import TaskLog +from tasks.models import TaskTemplate +from tasks.models import TaskWorkflowTemplate from tasks.tests.factories import TaskItemTemplateFactory pytestmark = pytest.mark.django_db @@ -307,3 +310,60 @@ def test_update_task_template_view( assert confirmation_response.status_code == 200 assert updated_task_template.title in soup.select("h1.govuk-panel__title")[0].text + + +def test_delete_task_template_view( + valid_user_client, + task_workflow_template_single_task_template_item, +): + """Test the view for deleting TaskTemplates and the confirmation view that a + successful deletion redirects to.""" + + assert ( + task_workflow_template_single_task_template_item.get_task_templates().count() + == 1 + ) + assert task_workflow_template_single_task_template_item.get_items().count() == 1 + + task_template_pk = ( + task_workflow_template_single_task_template_item.get_task_templates().get().pk + ) + task_item_template_pk = ( + task_workflow_template_single_task_template_item.get_items().get().pk + ) + delete_url = reverse( + "workflow:task-template-ui-delete", + kwargs={ + "workflow_template_pk": task_workflow_template_single_task_template_item.pk, + "pk": task_template_pk, + }, + ) + + delete_response = valid_user_client.post(delete_url) + task_workflow_template_after = TaskWorkflowTemplate.objects.get( + pk=task_workflow_template_single_task_template_item.pk, + ) + + confirmation_url = reverse( + "workflow:task-template-ui-confirm-delete", + kwargs={ + "workflow_template_pk": task_workflow_template_single_task_template_item.pk, + "pk": task_template_pk, + }, + ) + + assert delete_response.status_code == 302 + assert delete_response.url == confirmation_url + assert task_workflow_template_after.get_task_templates().count() == 0 + assert not TaskTemplate.objects.filter(pk=task_template_pk) + assert not TaskItemTemplate.objects.filter(pk=task_item_template_pk) + + confirmation_response = valid_user_client.get(confirmation_url) + + soup = BeautifulSoup(str(confirmation_response.content), "html.parser") + + assert confirmation_response.status_code == 200 + assert ( + f"Task template ID: {task_template_pk}" + in soup.select(".govuk-panel__title")[0].text + ) diff --git a/tasks/urls.py b/tasks/urls.py index c9b8f45a7..6620250ef 100644 --- a/tasks/urls.py +++ b/tasks/urls.py @@ -73,6 +73,16 @@ views.TaskTemplateConfirmUpdateView.as_view(), name="task-template-ui-confirm-update", ), + path( + "/task-templates//delete/", + views.TaskTemplateDeleteView.as_view(), + name="task-template-ui-delete", + ), + path( + "/task-templates//confirm-delete/", + views.TaskTemplateConfirmDeleteView.as_view(), + name="task-template-ui-confirm-delete", + ), ] urlpatterns = [ diff --git a/tasks/views.py b/tasks/views.py index 2df5b8c74..4d92f157f 100644 --- a/tasks/views.py +++ b/tasks/views.py @@ -18,6 +18,7 @@ from tasks.forms import TaskCreateForm from tasks.forms import TaskDeleteForm from tasks.forms import TaskTemplateCreateForm +from tasks.forms import TaskTemplateDeleteForm from tasks.forms import TaskTemplateUpdateForm from tasks.forms import TaskUpdateForm from tasks.models import Task @@ -340,3 +341,46 @@ def get_context_data(self, **kwargs) -> dict: context["task_workflow_template"] = self.get_object().taskitemtemplate.queue return context + + +class TaskTemplateDeleteView(PermissionRequiredMixin, DeleteView): + model = TaskTemplate + template_name = "tasks/workflows/task_template_delete.jinja" + permission_required = "tasks.delete_tasktemplate" + form_class = TaskTemplateDeleteForm + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs["instance"] = self.object + return kwargs + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + context["task_workflow_template"] = self.get_object().taskitemtemplate.queue + + return context + + def get_success_url(self): + return reverse( + "workflow:task-template-ui-confirm-delete", + kwargs={ + "workflow_template_pk": self.kwargs["workflow_template_pk"], + "pk": self.object.pk, + }, + ) + + +class TaskTemplateConfirmDeleteView(PermissionRequiredMixin, TemplateView): + template_name = "tasks/workflows/task_template_confirm_delete.jinja" + permission_required = "tasks.delete_tasktemplate" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + context["deleted_pk"] = self.kwargs["pk"] + context["task_workflow_template"] = TaskWorkflowTemplate.objects.get( + pk=self.kwargs["workflow_template_pk"], + ) + + return context