Skip to content

Commit 534f336

Browse files
fvollmercodingjoe
andauthored
Add StartViewMixin for custom start view tasks (#132)
Co-authored-by: Johannes Maron <johannes@maron.family>
1 parent 94cd044 commit 534f336

File tree

7 files changed

+36
-3
lines changed

7 files changed

+36
-3
lines changed

joeflow/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .conf import settings
2020
from .typing import HUMAN, MACHINE
2121
from .utils import NoDashDiGraph
22+
from .views import StartViewMixin
2223

2324
logger = logging.getLogger(__name__)
2425

@@ -139,7 +140,7 @@ def urls(cls):
139140
urls = []
140141
for name, node in cls.get_nodes():
141142
if isinstance(node, View):
142-
if isinstance(node, BaseCreateView):
143+
if isinstance(node, (BaseCreateView, StartViewMixin)):
143144
route = f"{name}/"
144145
else:
145146
route = f"{name}/<int:pk>/"

joeflow/tasks/human.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
from django.views import generic
44

5-
from joeflow.views import TaskViewMixin
5+
from joeflow.views import StartViewMixin, TaskViewMixin
66

77
__all__ = (
88
"StartView",
99
"UpdateView",
1010
)
1111

1212

13-
class StartView(TaskViewMixin, generic.CreateView):
13+
class StartView(StartViewMixin, generic.CreateView):
1414
"""
1515
Start a new workflow by a human with a view.
1616

joeflow/views.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ def create_task(self, workflow, prev_task):
9797
)
9898

9999

100+
class StartViewMixin(TaskViewMixin):
101+
"""
102+
View-mixin to create a start workflow.
103+
104+
Example:
105+
class MyStartWorkflowView(StartViewMixin, View):
106+
def post(self, request, *args, **kwargs):
107+
try:
108+
data = json.loads(request.body)
109+
workflow_id = self.start_workflow(data)
110+
except Exception as e:
111+
return HttpResponseBadRequest("Failed to start workflow")
112+
113+
return JsonResponse({'message': 'Workflow started successfully.', 'id': workflow_id}, status=201)
114+
"""
115+
116+
model = None
117+
118+
100119
class WorkflowDetailView(WorkflowTemplateNameViewMixin, generic.DetailView):
101120
pass
102121

tests/fixtures/simpleworkflow.dot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ digraph {
55
"save the princess" [color=black fontcolor=black style="filled, rounded"]
66
"start method" [color=black fontcolor=black style=filled]
77
"start view" [color=black fontcolor=black style="filled, rounded"]
8+
"custom start view" [color=black fontcolor=black style="filled, rounded"]
89
"save the princess" -> end [color=black]
910
"start method" -> "save the princess" [color=black]
1011
"start view" -> "save the princess" [color=black]
12+
"custom start view" -> "save the princess" [color=black]
1113
}

tests/fixtures/simpleworkflow_instance.dot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ digraph {
55
"save the princess" [color=black fontcolor=black href="/simple/save_the_princess/2/" peripheries=1 style="filled, rounded, bold"]
66
"start method" [color=black fontcolor=black peripheries=1 style=filled]
77
"start view" [color="#888888" fontcolor="#888888" style="filled, rounded"]
8+
"custom start view" [color="#888888" fontcolor="#888888" style="filled, rounded"]
89
"save the princess" -> end [color="#888888"]
910
"start method" -> "save the princess" [color=black]
1011
"start view" -> "save the princess" [color="#888888"]
12+
"custom start view" -> "save the princess" [color="#888888"]
1113
}

tests/test_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def test_urls(self):
207207
assert namespace == "simpleworkflow"
208208
names = {pattern.name for pattern in patterns}
209209
assert names == {
210+
"custom_start_view",
210211
"save_the_princess",
211212
"start_view",
212213
"override",

tests/testapp/workflows.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from datetime import timedelta
22

33
from django.core.mail import send_mail
4+
from django.views import generic
45

56
from joeflow import tasks
7+
from joeflow.views import StartViewMixin
68

79
from . import models
810
from .views import UpdateWithPrevUserView
@@ -40,8 +42,13 @@ class Meta:
4042
proxy = True
4143

4244

45+
class CustomStartViewTaskView(StartViewMixin, generic.View):
46+
pass
47+
48+
4349
class SimpleWorkflow(models.SimpleWorkflowState):
4450
start_view = tasks.StartView(fields="__all__", path="custom/postfix/")
51+
custom_start_view = CustomStartViewTaskView()
4552
start_method = tasks.Start()
4653
save_the_princess = tasks.UpdateView(fields="__all__")
4754

@@ -50,6 +57,7 @@ def end(self):
5057

5158
edges = (
5259
(start_view, save_the_princess),
60+
(custom_start_view, save_the_princess),
5361
(start_method, save_the_princess),
5462
(save_the_princess, end),
5563
)

0 commit comments

Comments
 (0)