## 工作流 工作流是业务对象描述其动态的模型。工作流程也用来跟踪过程,随着时间的推移发展。 练习 几乎是工作流 添加一个 `state` 字段向*会话*模型。它将定义一个工作流ISH。 一个会话可以有三种可能的状态:草稿(默认),证实了。 在会话形式中,添加一个(只读)字段来可视化状态,并将其更改为。有效转换: * Draft -> Confirmed * Confirmed -> Draft * Confirmed -> Done * Done -> Draft 1. 添加一个新的 `state` 字段 2. 添加状态转换方法,这些可以从视图按钮来更改记录的状态 3. 并将相关按钮添加到会话的窗体视图中 *openacademy/models.py* ~~~ python attendees_count = fields.Integer( string="Attendees count", compute='_get_attendees_count', store=True) 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' @api.depends('seats', 'attendee_ids') def _taken_seats(self): for r in self: ~~~ *openacademy/views/openacademy.xml* ~~~ 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> <sheet> <group> <group string="General"> ~~~ 工作流可以与任何对象关联,Odoo,是完全可定制的。工作流是用于组织和管理业务对象和文件的生命周期,并定义转换,触发器等图形工具。工作流程,活动(节点或行动)和转换(条件)被声明为通常的XML。标记导航在工作流称为工作项。 Warning 创建模型的记录时,只有创建模型的工作流。因此,在工作流的定义之前,没有创建工作流实例关联的工作流实例 练习 工作流 用真实的工作流程替换临时 工作流程。转换 *Session* 表单视图,所以它的按钮调用工作流而不是模型的方法。 *openacademy/__openerp__.py* ~~~ python 'templates.xml', 'views/openacademy.xml', 'views/partner.xml', 'views/session_workflow.xml', ], # only loaded in demonstration mode 'demo': [ ~~~ *openacademy/models.py* ~~~ python ('draft', "Draft"), ('confirmed', "Confirmed"), ('done', "Done"), ]) @api.multi def action_draft(self): ~~~ *openacademy/views/openacademy.xml* ~~~ 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* ~~~ 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> ~~~ Tip 为了检查工作流程的实例是否正确地创建了与会话,转到设置技术工作流实例 练习 自动转换 会话自动过渡 *Draft* 到 *Confirmed* 当超过一半的会议的座位是保留。 *openacademy/views/session_workflow.xml* ~~~ 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> ~~~ 练习 服务器操作 取代Python方法的同步会话状态服务器的操作。 工作流和服务器操作都可以完全由用户界面创建。 *openacademy/views/session_workflow.xml* ~~~ 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"> ~~~