-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.contrib import admin | ||
from viewflow.admin import ProcessAdmin | ||
from . import models, flows | ||
|
||
|
||
@admin.register(models.DynamicSplitProcess) | ||
class DynamicSplitAdmin(ProcessAdmin): | ||
list_display = ['pk', 'created', 'get_status_display', 'participants', | ||
'split_count', 'decisions_list'] | ||
|
||
list_display_links = ['pk', 'created'] | ||
|
||
def decisions_list(self, obj): | ||
return ', '.join(['No', 'Yes'][answer.decision] for answer in obj.decision_set.all()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from viewflow import flow | ||
from viewflow.base import this, Flow | ||
from viewflow.site import viewsite | ||
from viewflow.views import StartView | ||
|
||
from . import models, views | ||
from .nodes import DynamicSplit | ||
|
||
|
||
class DynamicSplitFlow(Flow): | ||
process_cls = models.DynamicSplitProcess | ||
|
||
start = flow.Start(StartView, fields=['split_count']) \ | ||
.Permission(auto_create=True) \ | ||
.Next(this.spit_on_decision) | ||
|
||
spit_on_decision = DynamicSplit(lambda p: p.split_count) \ | ||
.Next(this.make_decision) | ||
|
||
make_decision = flow.View(views.DecisionView) \ | ||
.Next(this.join_on_decision) | ||
|
||
join_on_decision = flow.Join() \ | ||
.Next(this.end) | ||
|
||
end = flow.End() | ||
|
||
|
||
viewsite.register(DynamicSplitFlow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.db import models | ||
from django.conf import settings | ||
from viewflow.models import Process | ||
|
||
|
||
class DynamicSplitProcess(Process): | ||
split_count = models.IntegerField(default=0) | ||
|
||
|
||
class Decision(models.Model): | ||
process = models.ForeignKey(DynamicSplitProcess) | ||
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True) | ||
decision = models.BooleanField(default=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from viewflow.activation import GateActivation | ||
from viewflow.flow.base import Gateway, Edge | ||
from viewflow.token import Token | ||
|
||
|
||
class DynamicSplitActivation(GateActivation): | ||
def execute(self): | ||
self._split_count = self.flow_task._task_count_callback(self.process) | ||
|
||
def activate_next(self): | ||
if self._split_count: | ||
token_source = Token.split_token_source(self.task.token, self.task.pk) | ||
for _ in range(self._split_count): | ||
self.flow_task._next_task.activate(prev_activation=self, token=next(token_source)) | ||
|
||
|
||
class DynamicSplit(Gateway): | ||
""" | ||
Activates several outgoing task instances depends on callback value | ||
Example:: | ||
spit_on_decision = flow.DynamicSplit(lambda p: 4) \\ | ||
.Next(this.make_decision) | ||
make_decision = flow.View(MyView) \\ | ||
.Next(this.join_on_decision) | ||
join_on_decision = flow.Join() \\ | ||
.Next(this.end) | ||
""" | ||
task_type = 'SPLIT' | ||
activation_cls = DynamicSplitActivation | ||
|
||
def __init__(self, callback): | ||
super(DynamicSplit, self).__init__() | ||
self._next_task, self._task_count_callback = None, callback | ||
|
||
def _outgoing(self): | ||
yield Edge(src=self, dst=self._next_task, edge_class='next') | ||
|
||
def Next(self, node): | ||
self._next_task = node | ||
return self | ||
|
||
|
||
# TODO: Some boilerplate to remove | ||
from viewflow.resolve import resolve_children_links | ||
|
||
|
||
@resolve_children_links.register(DynamicSplit) | ||
def _(flow_node, resolver): | ||
flow_node._next_task = resolver.get_implementation(flow_node._next_task) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.views import generic | ||
from viewflow import flow | ||
|
||
from . import models | ||
|
||
|
||
class DecisionView(flow.TaskFormViewMixin, generic.CreateView): | ||
model = models.Decision | ||
fields = ['decision'] | ||
|
||
def activation_done(self, form): | ||
self.object = form.save(commit=False) | ||
|
||
self.object.user = self.request.user | ||
self.object.process = self.activation.process | ||
self.object.save() | ||
|
||
self.activation.done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters