Odoo10.0中的工作流

Odoo10.0中可以通过两种方式来实现工作流(workflow)

  一种的官网API中给出的方式,链接:https://www.odoo.com/documentation/10.0/reference/workflows.html

  还有一种是通过查看Odoo10.0的源码,发现自带模块中使用的方式;

先来说下第一种方式:

首先,创建文件目录:

接下来编写model及XML文件:

model文件:

# -*- coding: utf-8 -*-

from odoo import models, fields, api


class WorkflowDemo(models.Model):
    _name = 'workflow_demo.leave'
    _description = u'工作流请假测试'

    name = fields.Char(u'请假说明')
    start_date = fields.Datetime(u'开始时间')
    end_date = fields.Datetime(u'结束时间')

    state = fields.Selection(
        [
            ('init', u'初始'),
            ('start', u'开始'),
            ('confirm', u'确认'),
            ('complete', u'完成'),
        ],
        default='init',
        string=u'状态',
        copy=False,
        readonly=True,
    )

    @api.one
    def do_start(self):
        print '-' * 40
        print 'do_start'
        self.state = 'start'

    @api.one
    def do_confirm(self):
        print '-' * 40
        print 'do_confirm'
        self.state = 'confirm'

    @api.one
    def do_complete(self):
        print '-' * 40
        print 'do_complete'
        self.state = 'complete'

  

view.xml文件:

<odoo>
    <data>
        <record model="ir.ui.view" id="workflow_demo.list">
            <field name="name">workflow_demo list</field>
            <field name="model">workflow_demo.leave</field>
            <field name="arch" type="xml">
                <tree>
                    <field name="name"/>
                    <field name="start_date"/>
                    <field name="end_date"/>
                    <field name="state"/>
                </tree>
            </field>
        </record>

        <record model="ir.ui.view" id="workflow_demo.form">
            <field name="name">workflow_demo form</field>
            <field name="model">workflow_demo.leave</field>
            <field name="arch" type="xml">
                <form string="请假单">
                    <header>
                        <button name="leave_start" states="init" string="开始" groups="workflow_demo.workflow_leave_group_employee" type="workflow" class="oe_highlight"/>
                        <button name="leave_confirm" states="start" string="确认" groups="workflow_demo.workflow_leave_group_manager" type="workflow" class="oe_highlight"/>
                        <button name="leave_complete" states="confirm" string="完成" groups="workflow_demo.workflow_leave_group_leader" type="workflow" class="oe_highlight"/>

                        <field name="state" widget="statusbar" statusbar_visible="start,confirm,complete"/>
                    </header>
                    <group>
                        <field name="name"/>
                        <field name="start_date"/>
                        <field name="end_date"/>
                    </group>
                </form>
            </field>
        </record>

        <record model="ir.actions.act_window" id="workflow_demo.action_window">
            <field name="name">workflow_demo window</field>
            <field name="res_model">workflow_demo.leave</field>
            <field name="view_mode">tree,form</field>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">创建请假单</p>
            </field>
        </record>

        <!-- Top menu item -->
        <menuitem name="workflow_demo" id="workflow_demo.menu_root"/>

        <!-- menu categories -->
        <menuitem name="Menu 1" id="workflow_demo.menu_1" parent="workflow_demo.menu_root"/>

        <!-- actions -->
        <menuitem name="List" id="workflow_demo.menu_1_list" parent="workflow_demo.menu_1"
                  action="workflow_demo.action_window"/>

    </data>
</odoo>

  

leave_security.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <!--
    noupdate
        0,升级模块时更新数据
        1,只在安装时创建数据,开发时可设置为0,定版后改回1
    -->
    <data noupdate="0">
        <record id="model_workflow_leave_category" model="ir.module.category">
            <field name="name">请假管理</field>
        </record>

        <record id="workflow_leave_group_employee" model="res.groups">
            <field name="name">团队成员</field>
            <field name="category_id" ref="model_workflow_leave_category"/>
            <field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
            <field name="comment">团队成员</field>
        </record>
         <record id="workflow_leave_group_manager" model="res.groups">
            <field name="name">项目经理</field>
            <field name="category_id" ref="model_workflow_leave_category"/>
            <field name="implied_ids" eval="[(4, ref('workflow_demo.workflow_leave_group_employee'))]"/>
            <field name="comment">项目经理</field>
        </record>
        <record id="workflow_leave_group_leader" model="res.groups">
            <field name="name">领导</field>
            <field name="category_id" ref="model_workflow_leave_category"/>
            <field name="implied_ids" eval="[(4, ref('workflow_demo.workflow_leave_group_manager'))]"/>
            <field name="users" eval="[(4, ref('base.user_root'))]"/>
            <field name="comment"></field>
        </record>
    </data>
</odoo>

  

ir.model.access.csv文件:

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_workflow_demo_workflow_demo,workflow_demo.leave,model_workflow_demo_leave,workflow_leave_group_leader,1,1,1,0
access_workflow_demo_workflow_demo,workflow_demo.leave,model_workflow_demo_leave,workflow_leave_group_manager,1,1,1,1
access_workflow_demo_workflow_demo,workflow_demo.leave,model_workflow_demo_leave,workflow_leave_group_employee,1,1,1,1

  

leave_workflow.xml文件:

<odoo>
    <data>
        <!-- 设置record为系统自带的workflow -->
        <record model="workflow" id="workflow_leave">
            <field name="name">workflow_demo</field>
            <!-- 关联model -->
            <field name="osv">workflow_demo.leave</field>
            <!-- 是否主动被实例化 -->
            <field name="on_create">True</field>
        </record>

        <!-- workflow.activity 用来指定要调用的model里的方法, 一般用来更改state的状态 -->
        <!--<record model="workflow.activity" id="activity_start">-->
            <!--<!– wkf_id 指定上面生命的workflow –>-->
            <!--<field name="wkf_id" ref="workflow_leave" />-->
            <!--<field name="name">workflow_demo.leave start</field>-->
            <!--<!– dummy 类型用来指定这个activity是workflow的开始还是结束 –>-->
            <!--<field name="kind">dummy</field>-->
            <!--<!– flow_start:开始, flow_end:结束 –>-->
            <!--<field name="flow_start">True</field>-->
        <!--</record>-->

        <record model="workflow.activity" id="activity_do_start">
            <field name="wkf_id" ref="workflow_leave" />
            <field name="name">workflow_demo.leave do start</field>
            <field name="flow_start">True</field>
            <!-- function 表示要执行model中的方法 -->
            <field name="kind">function</field>
            <!-- 对应model中的方法 -->
            <field name="action">do_start()</field>
        </record>

        <record model="workflow.activity" id="activity_do_confirm">
            <field name="wkf_id" ref="workflow_leave" />
            <field name="name">workflow_demo.leave do confirm</field>
            <!-- function 表示要执行model中的方法 -->
            <field name="kind">function</field>
            <!-- 对应model中的方法 -->
            <field name="action">do_confirm()</field>
        </record>

        <record model="workflow.activity" id="activity_do_complete">
            <field name="wkf_id" ref="workflow_leave" />
            <field name="name">workflow_demo.leave do complete</field>
            <field name="flow_end">True</field>
            <!-- function 表示要执行model中的方法 -->
            <field name="kind">function</field>
            <!-- 对应model中的方法 -->
            <field name="action">do_complete()</field>
        </record>

        <!--<record model="workflow.activity" id="activity_end">-->
            <!--<field name="wkf_id" ref="workflow_leave" />-->
            <!--<field name="name">workflow_demo.leave end</field>-->
            <!--<field name="kind">dummy</field>-->
            <!--<field name="flow_end">True</field>-->
        <!--</record>-->

        <!-- Transitions control how the workflow progresses from activity to activity. -->
        <!-- Transitions 来控制activity的走向 -->
        <record model="workflow.transition" id="transition_one">
            <field name="act_from" ref="activity_start" />
            <field name="act_to" ref="activity_do_start" />
            <field name="condition">state == 'init'</field>
            <field name="signal">leave_start</field>
        </record>

        <record model="workflow.transition" id="transition_two">
            <field name="act_from" ref="activity_do_start" />
            <field name="act_to" ref="activity_do_confirm" />
            <field name="condition">state == 'start'</field>
            <field name="signal">leave_confirm</field>
        </record>

        <record model="workflow.transition" id="transition_three">
            <field name="act_from" ref="activity_do_confirm" />
            <field name="act_to" ref="activity_do_complete" />
            <field name="condition">state == 'confirm'</field>
            <field name="signal">leave_complete</field>
        </record>

    </data>
</odoo>

  

__mainfest__.py文件:

# -*- coding: utf-8 -*-
{
    'name': "工作流Demo",
    'summary': """
        工作流测试
    """,
    'description': """
        Long description of module's purpose
    """,
    'author': "ed",
    'website': "http://www.yourcompany.com",
    'category': 'Uncategorized',
    'version': '0.1',
    'application': True,
    # any module necessary for this one to work correctly
    'depends': ['base'],
    'sequence': 10,
    # always loaded
    'data': [
        'security/leave_security.xml',
        'security/ir.model.access.csv',
        'views/leave_workflow.xml',
        'views/views.xml',
    ],
    # only loaded in demonstration mode
    'demo': [
    ],
}

  

使用这种方式定义workflow,FromView中有两处需要注意:

  1.button中的name属性对应workflow.transition中的signal;

  2.button的type属性必须设置为workflow,否则会报错;

 

接下来第二种方式定义workflow,这种方式相比较第一种方式,要简单一些

原理是直接在button中调用model中定义的方法来修改state的状态,但是此处button的type属性需要定义为object

具体如下:

__mainfest__.py文件中注释掉该行

# 'views/leave_workflow.xml',

  

view.xml中需要将header中的button坐下调整:

<header>
  <button name="do_start" states="init" string="开始" groups="workflow_demo.workflow_leave_group_employee" type="object" class="oe_highlight"/>
  <button name="do_confirm" states="start" string="确认" groups="workflow_demo.workflow_leave_group_manager" type="object" class="oe_highlight"/>
  <button name="do_complete" states="confirm" string="完成" groups="workflow_demo.workflow_leave_group_leader" type="object" class="oe_highlight"/>

  <field name="state" widget="statusbar" statusbar_visible="start,confirm,complete"/>
</header>

  

其中name属性为model中的方法,type属性修改为object,其他不用修改。

 

以上两种方式我自己都本地验证过,是没有问题的,有疑问的伙伴可以留言

 

posted @ 2023-04-26 21:15  CrossPython  阅读(93)  评论(0编辑  收藏  举报