关注各种黑科技
B站UP主,编程教学,游戏玩家,挂B,金牛座

Odoo工作流

在odoo中,workflow是关联到某个model的对象,用来动态的管理记录的生命周期.   练习 创建一个模拟的workflow-ish(http://language.chinadaily.com.cn/2007-03/16/content_829692.htm) 给课时加入状态字段state, 有三种状态 草稿Draft (default), 确认 Confirmed and 完成Done。 在课时session的form里加入state状态(只读)显示和改变状态的按钮   状态的改变流程如下
Draft -> Confirmed
Confirmed -> Draft
Confirmed -> Done
Done -> Draft
  添加代码的步骤 1 添加state字段 2 添加state状态转移方法,并绑定方法到form view上的按钮 3 添加相关按钮到form view   openacademy/models.py
    state = fields.Selection([
        ('draft', "Draft"),
        ('confirmed', "Confirmed"),
        ('done', "Done"),
    ], default='draft')
    @api.multi
    def action_draft(self):
        self.state = 'draft'
    @api.multi
    def action_confirm(self):
        self.state = 'confirmed'
    @api.multi
    def action_done(self):
        self.state = 'done'
openacademy/views/openacademy.xml
<field name="model">openacademy.session</field>
            <field name="arch" type="xml">
                <form string="Session Form">
                    <header>
                        <button name="action_draft" type="object"
                                string="Reset to draft"
                                states="confirmed,done"/>
                        <button name="action_confirm" type="object"
                                string="Confirm" states="draft"
                                class="oe_highlight"/>
                        <button name="action_done" type="object"
                                string="Mark as done" states="confirmed"
                                class="oe_highlight"/>
                        <field name="state" widget="statusbar"/>
                    </header>
 

使用odoo workflow管理状态

  以上没有使用odoo workflow对象。odoo中的workflow使用xml定义,在workflows中有activities (nodes or actions) ,transitions (conditions) 和workitems。   Activities定义了一些由odoo服务器所要完成的任务,如改变记录状态,发送邮件 Transitions定义了如何从一个Activity到另外Activity   更多内容参考 https://www.odoo.com/documentation/8.0/reference/workflows.html     注意 workflow是在关联的model对象创建时一起创建并运行的。   练习 替换刚才临时的(ad-hoc)session workflow到一个真正的workflow对象. 并且改变button的绑定方法到workflow的transition方法 去除state的default值,交由workflow管理。   openacademy/__openerp__.py
        'templates.xml',
        'views/openacademy.xml',
        'views/partner.xml',
        'views/session_workflow.xml',
    ],
    # only loaded in demonstration mode
    'demo': [
  openacademy/models.py
        ('draft', "Draft"),
        ('confirmed', "Confirmed"),
        ('done', "Done"),
    ])
    @api.multi
    def action_draft(self):
  openacademy/views/openacademy.xml
            <field name="arch" type="xml">
                <form string="Session Form">
                    <header>
                        <button name="draft" type="workflow"
                                string="Reset to draft"
                                states="confirmed,done"/>
                        <button name="confirm" type="workflow"
                                string="Confirm" states="draft"
                                class="oe_highlight"/>
                        <button name="done" type="workflow"
                                string="Mark as done" states="confirmed"
                                class="oe_highlight"/>
                        <field name="state" widget="statusbar"/>
  openacademy/views/session_workflow.xml
<openerp>
    <data>
        <record model="workflow" id="wkf_session">
            <field name="name">OpenAcademy sessions workflow</field>
            <field name="osv">openacademy.session</field>
            <field name="on_create">True</field>
        </record>
        <record model="workflow.activity" id="draft">
            <field name="name">Draft</field>
            <field name="wkf_id" ref="wkf_session"/>
            <field name="flow_start" eval="True"/>
            <field name="kind">function</field>
            <field name="action">action_draft()</field>
        </record>
        <record model="workflow.activity" id="confirmed">
            <field name="name">Confirmed</field>
            <field name="wkf_id" ref="wkf_session"/>
            <field name="kind">function</field>
            <field name="action">action_confirm()</field>
        </record>
        <record model="workflow.activity" id="done">
            <field name="name">Done</field>
            <field name="wkf_id" ref="wkf_session"/>
            <field name="kind">function</field>
            <field name="action">action_done()</field>
        </record>
        <record model="workflow.transition" id="session_draft_to_confirmed">
            <field name="act_from" ref="draft"/>
            <field name="act_to" ref="confirmed"/>
            <field name="signal">confirm</field>
        </record>
        <record model="workflow.transition" id="session_confirmed_to_draft">
            <field name="act_from" ref="confirmed"/>
            <field name="act_to" ref="draft"/>
            <field name="signal">draft</field>
        </record>
        <record model="workflow.transition" id="session_done_to_draft">
            <field name="act_from" ref="done"/>
            <field name="act_to" ref="draft"/>
            <field name="signal">draft</field>
        </record>
        <record model="workflow.transition" id="session_confirmed_to_done">
            <field name="act_from" ref="confirmed"/>
            <field name="act_to" ref="done"/>
            <field name="signal">done</field>
        </record>
    </data>
</openerp>
创建的workflow可以在Settings ‣ Technical ‣ Workflows ‣ Instances里查看   button name="transition signal 名称"   练习 根据条件自动执行transition,当课时的上课人数大于50%的时候 自动从草稿到确认状态。 openacademy/views/session_workflow.xml
            <field name="act_to" ref="done"/>
            <field name="signal">done</field>
        </record>
        <record model="workflow.transition" id="session_auto_confirm_half_filled">
            <field name="act_from" ref="draft"/>
            <field name="act_to" ref="confirmed"/>
            <field name="condition">taken_seats &gt; 50</field>
        </record>
    </data>
</openerp>
练习 服务器动作 server action 可以在xml里定义的python方法 用来替换model的方法 openacademy/views/session_workflow.xml
           <field name="on_create">True</field>
        </record>
        <record model="ir.actions.server" id="set_session_to_draft">
            <field name="name">Set session to Draft</field>
            <field name="model_id" ref="model_openacademy_session"/>
            <field name="code">
model.search([('id', 'in', context['active_ids'])]).action_draft()
            </field>
        </record>
        <record model="workflow.activity" id="draft">
            <field name="name">Draft</field>
            <field name="wkf_id" ref="wkf_session"/>
            <field name="flow_start" eval="True"/>
            <field name="kind">dummy</field>
            <field name="action"></field>
            <field name="action_id" ref="set_session_to_draft"/>
        </record>

        <record model="ir.actions.server" id="set_session_to_confirmed">
            <field name="name">Set session to Confirmed</field>
            <field name="model_id" ref="model_openacademy_session"/>
            <field name="code">
model.search([('id', 'in', context['active_ids'])]).action_confirm()
            </field>
        </record>
        <record model="workflow.activity" id="confirmed">
            <field name="name">Confirmed</field>
            <field name="wkf_id" ref="wkf_session"/>
            <field name="kind">dummy</field>
            <field name="action"></field>
            <field name="action_id" ref="set_session_to_confirmed"/>
        </record>

        <record model="ir.actions.server" id="set_session_to_done">
            <field name="name">Set session to Done</field>
            <field name="model_id" ref="model_openacademy_session"/>
            <field name="code">
model.search([('id', 'in', context['active_ids'])]).action_done()
            </field>
        </record>
        <record model="workflow.activity" id="done">
            <field name="name">Done</field>
            <field name="wkf_id" ref="wkf_session"/>
            <field name="kind">dummy</field>
            <field name="action"></field>
            <field name="action_id" ref="set_session_to_done"/>
        </record>
        <record model="workflow.transition" id="session_draft_to_confirmed"
   
转载请注明出处
分享到: 更多 (0)

评论 0