From 6f798357fa5f054cd83b32540ecf0c19b1779db3 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Mon, 20 May 2019 12:53:02 +0200 Subject: [PATCH] prevent deletion of last workflow and update configs on deletion --- openslides/motions/views.py | 14 ++++++++++++++ tests/integration/motions/test_viewset.py | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/openslides/motions/views.py b/openslides/motions/views.py index 361f0904e..aeb166f32 100644 --- a/openslides/motions/views.py +++ b/openslides/motions/views.py @@ -1598,15 +1598,29 @@ class WorkflowViewSet(ModelViewSet, ProtectedErrorMessageMixin): result = False return result + @transaction.atomic def destroy(self, *args, **kwargs): """ Customized view endpoint to delete a workflow. """ + workflow_pk = self.get_object().pk + if not Workflow.objects.exclude(pk=workflow_pk).exists(): + raise ValidationError({"detail": "You cannot delete the last workflow."}) + try: result = super().destroy(*args, **kwargs) except ProtectedError as err: msg = self.getProtectedErrorMessage("workflow", err) raise ValidationError({"detail": msg}) + + # Change motion default workflows in the config + if int(config["motions_workflow"]) == workflow_pk: + config["motions_workflow"] = str(Workflow.objects.first().pk) + if int(config["motions_statute_amendments_workflow"]) == workflow_pk: + config["motions_statute_amendments_workflow"] = str( + Workflow.objects.first().pk + ) + return result diff --git a/tests/integration/motions/test_viewset.py b/tests/integration/motions/test_viewset.py index 7a3062ec6..01d691908 100644 --- a/tests/integration/motions/test_viewset.py +++ b/tests/integration/motions/test_viewset.py @@ -1972,6 +1972,15 @@ class DeleteWorkflow(TestCase): self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(Workflow.objects.count(), 2) + def test_delete_last_workflow(self): + self.workflow.delete() + other_workflow_pk = Workflow.objects.get().pk + response = self.client.delete( + reverse("workflow-detail", args=[other_workflow_pk]) + ) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(Workflow.objects.count(), 1) # Just the other default one + class DeleteCategory(TestCase): """