Odoo10模組開發(1) 工作流
阿新 • • 發佈:2019-01-01
首先建立一個demo
檔案結構如下:
其中models/models.py:
class workflow_demo(models.Model):
_name = 'workflow_demo.workflow_demo'
WORKFLOW_STATE_SELECTION = [
('init', '初始'),
('start', '開始'),
('confirm', '確認'),
('complete', '完成'),
]
name = fields.Char(string="姓名" )
state = fields.Selection(WORKFLOW_STATE_SELECTION, default='init', string="狀態", readonly=True)
# 注意使用新版本定義方法是,需要新增裝飾器@api.one/@api.multi
# 將state 置於開始狀態
@api.one
def do_start(self):
print "------------self.state = 'start'"
self.state = 'start'
return True
# 將state 置於確認狀態
@api.one
def do_confirm(self):
print "------------self.state = 'confirm'"
self.state = 'confirm'
return True
# 將state 置於完成狀態
@api.one
def do_complete(self):
print "------------self.state = 'complete'"
self.state = 'complete'
return True
views建立 (workflow_view.xml)
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<!-- form 檢視-->
<record id="workflow_demo_form_view" model="ir.ui.view">
<field name="name">Workflow</field>
<field name="type">form</field>
<field name="model">workflow_demo.workflow_demo</field>
<field name="arch" type="xml">
<form string="Workflow單">
<!-- workflow 宣告-->
<!-- 新增header標籤用來顯示workflow中的按鈕-->
<header>
<!-- 首先指明button的type為workflow型別 -->
<!-- name 對應於workflow.transition中的signal-->
<!-- states 屬性用來控制button顯示隱藏,例如states="init",當state屬性為init時,這個button顯示,否則不顯示 -->
<button name="workflow_start" states="init" string="開始" type="workflow" class="oe_highlight"/>
<button name="workflow_confirm" states="start" string="確認" type="workflow" class="oe_highlight"/>
<button name="workflow_end" states="confirm" string="完成" type="workflow" class="oe_highlight"/>
<!-- 右上角 進度顯示。注意在此處已經使用的state屬性,所以在接下來的tree標籤裡面就不能再次宣告<field name="state"/>瞭如果再次宣告此處的進度顯示便會失去作用 -->
<field name="state" widget="statusbar" statusbar_visible="start,confirm,complete"/>
</header>
<sheet>
<group col="4">
<field name="name"/>
</group>
</sheet>
</form>
</field>
</record>
<!-- tree(list)檢視 -->
<record id="workflow_demo_tree_view" model="ir.ui.view">
<field name="name">workflow_demo</field>
<field name="type">tree</field>
<field name="model">workflow_demo.workflow_demo</field>
<field name="arch" type="xml">
<tree string="Workflow">
<field name="name"/>
</tree>
</field>
</record>
<record id="workflow_demo_action" model="ir.actions.act_window">
<field name="name">Workflow</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">workflow_demo.workflow_demo</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">建立Workflow單</p>
</field>
</record>
<!-- 選單 -->
<menuitem id="workflow_demo_main_menu" name="Workflow演示"/>
<menuitem id="workflow_demo_sub_menu" name="Workflow演示" parent="workflow_demo_main_menu"/>
<menuitem id="workflow_demo_item_menu" name="Workflow" parent="workflow_demo_sub_menu"
action="workflow_demo_action" sequence="1"/>
</data>
</odoo>
建立workflow_demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<!-- Workflow 工作流 -->
<!-- https://www.odoo.com/documentation/8.0/reference/workflows.html -->
<!-- 設定record的model為系統自帶的 “workflow” -->
<record model="workflow" id="workflow_demo">
<field name="name">workflow_demo</field>
<!-- 關聯的model -->
<field name="osv">workflow_demo.workflow_demo</field>
<!-- 是否主動被例項化 -->
<field name="on_create">True</field>
</record>
<!-- workflow.activity 用來指定要呼叫的model裡的方法,一般用來更改state狀態-->
<record model="workflow.activity" id="activity_one">
<!-- wkf_id 這個activity對應於哪個workflow,這裡對應於上面宣告的workflow-->
<field name="wkf_id" ref="workflow_demo"/>
<field name="name">workflow.demo.activity.one</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_two">
<field name="wkf_id" ref="workflow_demo"/>
<field name="name">workflow.demo.activity.two</field>
<!-- function 表示要執行model中的方法-->
<field name="kind">function</field>
<!-- 對應model中的方法-->
<field name="action">do_start()</field>
</record>
<record model="workflow.activity" id="activity_three">
<field name="wkf_id" ref="workflow_demo"/>
<field name="name">workflow.demo.activity.three</field>
<field name="kind">function</field>
<field name="action">do_confirm()</field>
</record>
<record model="workflow.activity" id="activity_four">
<field name="wkf_id" ref="workflow_demo"/>
<field name="name">workflow.demo.activity.four</field>
<field name="kind">function</field>
<field name="action">do_complete()</field>
</record>
<record model="workflow.activity" id="activity_five">
<field name="wkf_id" ref="workflow_demo"/>
<field name="name">workflow.temp.activity.five</field>
<field name="kind">dummy</field>
<field name="flow_end">True</field>
</record>
<!-- 相當於一個轉換器,用來接受button傳來的訊號,呼叫相關的activity-->
<record model="workflow.transition" id="transition_one">
<!-- 表示上一個執行的activity,與上面宣告的activity對應-->
<field name="act_from" ref="activity_one"/>
<!-- 表示上一個將要執行的activity-->
<field name="act_to" ref="activity_two"/>
<!-- 用來做一個嚴重,當滿足條件是才能執行,可省略-->
<field name="condition">state == 'init'</field>
<!-- 接收的訊號 -->
<field name="signal">workflow_start</field>
</record>
<record model="workflow.transition" id="transition_two">
<field name="act_from" ref="activity_two"/>
<field name="act_to" ref="activity_three"/>
<field name="condition">state == 'start'</field>
<field name="signal">workflow_confirm</field>
</record>
<record model="workflow.transition" id="transition_three">
<field name="act_from" ref="activity_three"/>
<field name="act_to" ref="activity_four"/>
<field name="condition">state == 'confirm'</field>
<field name="signal">workflow_end</field>
</record>
</data>
</odoo>
載入到_manifest_.py
# always loaded
'data': [
# 'security/ir.model.access.csv',
'views/views.xml',
'views/templates.xml',
'views/workflow_view.xml',
'views/workflow_demo.xml',
],
測試
安裝模組並進行測試
可以看到控制檯輸出
workflow 該功能的開發要注意,workflow、workflow_activity、workflow_transition三者的關係
Odoo 相比較於現在的workflow宣告方式,還有另外一種簡單的方式,這種方式要在後面的文章介紹。