如何以编程方式创建Django ViewFlow流程 [英] How to create a django ViewFlow process programmatically

查看:260
本文介绍了如何以编程方式创建Django ViewFlow流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

我正在开发一个Web应用程序来学习Django(python 3.4和Django 1.6.10)。该Web应用程序具有复杂且经常更新的工作流程。我决定集成Django-Viewflow库( https://github.com/viewflow/viewflow/ ),因为这似乎是处理工作流并且不将工作流逻辑与应用程序模型合并的一种非常方便的方法。

I'm developing a web application to learn Django (python 3.4 & Django 1.6.10). The web app has complex and often updated workflows. I decided to integrate the Django-Viewflow library (https://github.com/viewflow/viewflow/) as it seems to be a very convenient way to handle workflows and not incorporate the workflow logic with the application models.

在这种情况下,我创建了一个工作流来收集使用Django-Viewflow库的作者信息和版权。每次将作者添加到书中时,都应启动工作流程。

In this case, I have created a workflow to collect authorship information and copyrights using the Django-Viewflow library. The workflow should be initiated each time an author is added to a book.

我的问题

文档提供了逐步整合最终目标的指南到端的工作流解决方案(前端和后端)。我的问题是我很难以编程方式(特别是从Book模型)以编程方式控制工作流程。

The documentation offers step by step guidelines to integrate an end-to-end worklfow solution (frontend and backend). My problem is that I'm having hard time to control the workflow programmatically (specifically from the Book model).

应用程序说明

我有一个Book模型(核心模型),它与作者之间有很多关系。

I have a Book model (core model) with a many to many relationship to Authors.

myApp / models.py

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

工作流程组件为:

myFlow / models.py

from viewflow.models import Process

class AuthorInvitation(process)     
    consent_confirmed = models.BooleanField(default=False)
    signature = models.CharField(max_length=150) 

myFlow / flows.py

from viewflow import flow
from viewflow.base import this, Flow
from viewflow.contrib import celery
from viewflow.views import StartProcessView, ProcessView
from . import models, tasks

class AuthorInvitationFlow(Flow):
    process_cls = models.AuthorInvitation

    start = flow.Start(StartProcessView) \
        .Permission(auto_create=True) \
        .Next(this.notify)

    notify = celery.Job(tasks.send_authorship_request) \
        .Next(this.approve)

    approve = flow.View(ProcessView, fields=["confirmed","signature"]) \
        .Permission(auto_create=True) \
        .Next(this.check_approve)

    check_approve = flow.If(cond=lambda p: p.confirmed) \
        .OnTrue(this.send) \
        .OnFalse(this.end)

    send = celery.Job(tasks.send_authorship) \
        .Next(this.end)

    end = flow.End()

问题

如何以编程方式控制工作流程过程(激活,确认步骤,重做步骤,取消过程...。)?我试图深入研究该库的代码。似乎 class activate 类包含正确的方法,但不确定如何对整体进行编排。

How can you control the workflow process programmatically (activate, confirm steps, redo steps, cancel the process....)? I have tried to dig into the code of the library. It seems that the class activate contains the right method but not sure how the whole should be orchestrated.

感谢

推荐答案

还有另外两个适用于Flow的Start内置任务

There are two additional Start build-in Tasks available for Flows

StartFunction-当函数在某处调用时开始流:

StartFunction - starts flow when the function called somewhere:

@flow_start_func
def create_flow(activation, **kwargs):
    activation.prepare()
    activation.done()
    return activation

class FunctionFlow(Flow):
    start = flow.StartFunction(create_flow) \
        .Next(this.end)

# somewhere in the code
FunctionFlow.start.run(**some_kwargs)

StartSignal-在django信号接收时开始流:

StartSignal - starts flow on django signal receive:

class SignalFlow(Flow):
    start = flow.StartSignal(some_signal, create_flow) \      
        .Next(this.end)

您可以ck的用法,以及此视图测试的其余任务套件

You can check the usage for them, and rest of build-in task in this viewflow test suite.

要手动处理任务状态,首先应从数据库中获取任务,将其激活,然后调用任何激活方法。

For manually process the task state, first you should get the task from the database, activate it, and call any activation method.

task  = MyFlow.task_cls.objects.get(...)
activation = task.activate()
if  activation.undo.can_proceed():
    activation.undo()

任何激活转换具有 .can_proceed()方法,可以帮助您检查是否处于允许过渡的状态。

Any activation transition have .can_proceed() method, helps you to check, is the task in the state that allows the transition.

这篇关于如何以编程方式创建Django ViewFlow流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆