一个简单的数据库驱动的工作流引擎

django-wf的Python项目详细描述


这个项目的目标是提供一个简单的数据库驱动的工作流引擎,您可以使用它来配置和 自动化复杂的操作。它基于在运行时通过管理员定义的可配置状态机 接口。

工作流定义

工作流由以下对象定义:

Workflow:main object, it must have a unique ^{tt1}$
State:objects which represent the nodes of the graph they are ^{tt2}$ specific
Transition:Transitions can be manual or automatic, automatic transitions are executed asynchrnously as soon as an object reaches their intial state. Of course only one automatic transition can be executed at the time and it will normally change the state so that other transitions which were defined for the intial state cannot be executed anymore. Which transition is executed depends on the ^{tt3}$ and on the related conditions
Condition:objects which limit the execution of transitions based on the properties of the object which is undergoing the process, of the user or on generic queries. Conditions are hierarchical and can be combined using special boolean conditions
Function:For every condition ^{tt4}$ of type function there must be a function which has value ^{tt4}$ for the condition. The function specifies the python function which will be called to check if the condition is fulfilled.
FunctionParameter:
Each function parameter is passed as kwarg to its ^{tt6}$
Callback:A callback defines a python function which should be called when a transition occurs, usually for required side effect. The callback will be called either within the same transaction and before the update of the object state, if ^{tt7}$ is ^{tt8}$ or after the after the status has been updated in an independent thread
CallbackParameter:
Each callback parameter is passed as kwarg to its ^{tt9}$

下面是一个简单的三态工作流示例,它混合了手动和自动转换功能

from django.contrib.auth.models import User
from django_workflow import workflow
from django_workflow.models import Workflow, State, Transition, Condition, Function, FunctionParameter, Callback, CallbackParameter

# create the main workflow object
wf = Workflow.objects.create(name="Test_Workflow", object_type="django.contrib.auth.models.User")
# create 3 states
s1 = State.objects.create(name="state 1", workflow=wf, active=True, initial=True)
s2 = State.objects.create(name="state 2", workflow=wf, active=True)
#the final state is defined as inactive so that its skipped when scanning for automatic transitions
s3 = State.objects.create(name="state 3", workflow=wf, active=False)
# create the transitions, we have 2 automatic transitions from state 1 to state 2,
# the first is going to be executed despite t4 having a better priority because
# t1 has a lower automatic_delay
t1 = Transition.objects.create(name="auto_fast", initial_state=s1, final_state=s2, automatic=True, automatic_delay=1.0/24.0/3600.0, priority=2)
t4 = Transition.objects.create(name="auto_slow", initial_state=s1, final_state=s3, automatic=True,
    automatic_delay=1.0 / 24.0, priority=1)
t2 = Transition.objects.create(initial_state=s1, final_state=s3, automatic=False)
t3 = Transition.objects.create(initial_state=s2, final_state=s3, automatic=False)
# we set t3 to be executed only by superusers this can be done with a object_attribute_value conditon
c1 = Condition.objects.create(condition_type="function", transition=t3)
f1 = Function.objects.create(
    function_name="object_attribute_value",
    function_module="django_workflow.conditions",
    condition=c1
)
p11 = FunctionParameter.objects.create(function=f1, name="attribute_name", value="is_superuser")
p12 = FunctionParameter.objects.create(function=f1, name="attribute_value", value="True")
# we want to print out if transition 1 was executed, this can be done with a callback
cb1 = Callback.objects.create(transition=t1, function_name="_print", function_module="django_workflow.tests", order=1)
cp11 = CallbackParameter.objects.create(callback=cb1, name="text", value="Transition 1 Executed")

状态和转换

定义工作流后,可以将对象添加到工作流中

obj = MyModelObject.objects.get(name="MyObjectName")
wf = workflow.get_workflow("Test_Workflow")
wf.add_object(obj.id)

方法add_object除了在状态机中启动对象的跟踪之外,它还触发 任何初始状态下可用的自动转换

要检查对象的状态,可以使用:

workflow.get_object_state("Test_Workflow", object_id)

要检查可用的转换,例如要知道可以在ui中显示哪些按钮,可以调用

workflow.get_available_transitions("Test_Workflow", user, object_id)

其中user应该是希望执行该操作的django用户。user被传递给 每个条件和回调,以便检查授权和执行特定任务, 例如通知

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java扫雷游戏的算法问题。游戏开始时隐藏地雷?   struts2的java迭代器标记   JavaFX如何创建精简菜单栏?   java从SQLite数据库获取字符串并检查是否等于变量不起作用   java如何检查移动数据连接   一旦我将Runnable传递给另一个线程中Swing的invokeAndWait,java就无法停止它   Java中每个键的多线程线程池   java没有可用的EjbContext   组织的java例外初始化错误。springframework。网状物util。UriComponentsBuilder。Android上的newInstance   java GWT CSS GUI通知用户浏览器问题   多线程一个Java编码程序,它读取一个巨大的CSV文件的行   java如何以编程方式打开特定应用程序的Android备份屏幕   java创建一个由10个整数组成的数组,并用1到6之间的随机数填充它   java如何将堆栈传递到方法中   java如何为注解@CurrentUser发送未经授权的响应   java访问switch语句中的对象   java如何正确扩展类   java DropboxAPI入门,未找到类