From 8d6d016ed43fdd22418b144e79f7763294f0c34f Mon Sep 17 00:00:00 2001 From: SebastianKapunkt Date: Thu, 22 Oct 2020 11:37:00 +0200 Subject: [PATCH] Move task creation into form_valid --- joeflow/views.py | 19 +++++++++++-------- tests/test_views.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/joeflow/views.py b/joeflow/views.py index 3056907..556fef6 100644 --- a/joeflow/views.py +++ b/joeflow/views.py @@ -56,19 +56,22 @@ def get_task(self): except KeyError: return models.Task(name=self.name, type=models.Task.HUMAN) - def get_object(self, queryset=None): - task = self.get_task() - return task.workflow - - @transaction.atomic - def post(self, request, *args, **kwargs): - response = super().post(request, *args, **kwargs) + def next_task(self): task = self.get_task() task.workflow = self.model._base_manager.get( pk=self.model._base_manager.get(pk=self.object.pk) ) - task.finish(request.user) + task.finish(self.request.user) task.start_next_tasks() + + def get_object(self, queryset=None): + task = self.get_task() + return task.workflow + + @transaction.atomic + def form_valid(self, form): + response = super().form_valid(form) + self.next_task() return response diff --git a/tests/test_views.py b/tests/test_views.py index e69de29..d49e6c6 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -0,0 +1,17 @@ +from django.urls import reverse + +from .testapp import models + + +class TestTaskViewMixin: + def test_create_task__form_errors(self, db, admin_client, admin_user): + url = reverse("shippingworkflow:checkout") + response = admin_client.post( + url, data={"shipping_address": 123, "email": "not a email"} + ) + assert response.status_code == 200 + assert response.context_data["form"].errors == { + "email": ["Enter a valid email address."] + } + + assert models.Shipment.objects.count() == 0