【问题标题】:How to connect two flows in django-viewflow如何在 django-viewflow 中连接两个流
【发布时间】:2019-02-08 14:19:47
【问题描述】:

我正在使用 django-viewflow 来跟踪复杂的业务流程。为了避免有长的 Flow 类和 flow.py 文件,我希望将一个流馈送到另一个流。这可能吗?

我尝试了以下代码,但 Python 抛出了 NotImplemented 异常。

class SecondFlow(Flow):
    process_class = SecondProcess
    start = (...)

class FirstFlow(Flow):
    process_class = FirstProcess
    start = (
        flow.Start(
            CreateProcessView,
            fields=['foo']
        ).Next(SecondFlow.start)
    )

如果 FirstFlow 路由到 SecondFlow 的开头就好了。

编辑:我尝试使用提供的建议和文档,但出现以下错误:'StartFunction' object has no attribute 'prepare'

下面是我的新代码。

from viewflow import flow, frontend
from viewflow.base import this, Flow
from viewflow.flow.views import CreateProcessView, UpdateProcessView

from .models import FirstProcess, SecondProcess

@frontend.register
class SecondFlow(Flow):
    process_class = SecondProcess
    start = flow.StartFunction(this.create_flow
            ).Next(this.enter_text)

    def create_flow(self, activation, **kwargs):
        activation.prepare()
        activation.done()

    enter_text = (
        flow.View(
            UpdateProcessView,
            fields=['text']
        ).Next(this.end)
    )

    end = flow.End()


@frontend.register
class FirstFlow(Flow):
    process_class = FirstProcess

    start = (
        flow.Start(
            CreateProcessView,
            fields=['text']
        ).Next(this.initiate_second_flow)
    )

    initiate_second_flow = (
        flow.Handler(this.start_second_flow
        ).Next(this.end)
    )

    def start_second_flow(self, activation):
        SecondFlow.start.run()

    end = flow.End()

第二次编辑:在我将装饰器添加到 SecondFlow 的 create_flow 方法后,它可以工作。

from django.utils.decorators import method_decorator
...

@frontend.register
class SecondFlow(Flow):
    process_class = SecondProcess
    start = flow.StartFunction(this.create_flow
            ).Next(this.enter_text)

    @method_decorator(flow.flow_start_func)
    def create_flow(self, activation, **kwargs):
        activation.prepare()
        activation.done()

...

【问题讨论】:

    标签: django-viewflow


    【解决方案1】:

    flow.Start 是用户调用并创建流程的开始视图的任务。 View 可能有一些逻辑,通常,该逻辑依赖于request 数据。因此,您不能在其他地方调用 flow.StartView 和 flow.View,除非通过浏览器访问 URL。

    要以编程方式激活某些进程,请使用 flow.StartFunction - http://docs.viewflow.io/viewflow_core_node.html#viewflow.nodes.StartFunction

    要从另一个流执行它,可以使用 flow.Handler - http://docs.viewflow.io/viewflow_core_node.html#viewflow.nodes.Handler

    【讨论】:

    • 谢谢。第二个进程能否拥有用户可以与之交互的 View?
    • 是的。视图流的目标之一是使视图独立于实际流。视图可以在一个流程内的不同任务之间或不同流程之间重用。
    • 嗨@kmmbvnr,你能看看我的问题的编辑吗?
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2019-08-16
    • 2013-01-11
    • 2017-04-26
    相关资源
    最近更新 更多