prevent deletion of last workflow and update configs on deletion

This commit is contained in:
FinnStutzenstein 2019-05-20 12:53:02 +02:00
parent 0f03eb1446
commit 6f798357fa
2 changed files with 23 additions and 0 deletions

View File

@ -1598,15 +1598,29 @@ class WorkflowViewSet(ModelViewSet, ProtectedErrorMessageMixin):
result = False result = False
return result return result
@transaction.atomic
def destroy(self, *args, **kwargs): def destroy(self, *args, **kwargs):
""" """
Customized view endpoint to delete a workflow. 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: try:
result = super().destroy(*args, **kwargs) result = super().destroy(*args, **kwargs)
except ProtectedError as err: except ProtectedError as err:
msg = self.getProtectedErrorMessage("workflow", err) msg = self.getProtectedErrorMessage("workflow", err)
raise ValidationError({"detail": msg}) 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 return result

View File

@ -1972,6 +1972,15 @@ class DeleteWorkflow(TestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Workflow.objects.count(), 2) 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): class DeleteCategory(TestCase):
""" """