oodo 設(shè)計模式一 - 消息通知

問題

系統(tǒng)根據(jù)單據(jù)更新發(fā)送消息通知相關(guān)人員。

舉例:
生產(chǎn)訂單下發(fā),庫管人員就會得到需要發(fā)貨的消息。

相關(guān)的設(shè)計模式有:

  • 重寫動作函數(shù).
  • 自動化動作
  • 定制開發(fā),根據(jù)單據(jù)狀態(tài),發(fā)送消息

相關(guān)主題:odoo提醒

下面就,討論不同模式的實現(xiàn)方法,和利弊。

重寫動作函數(shù)

odoo v8 中 確認生產(chǎn)訂單 通過workflow完成,
workflow 調(diào)用的確認函數(shù)是:

def action_confirm(self, cr, uid, ids, context=None):
        """ Confirms production order.
        @return: Newly generated Shipment Id.
        """
        user_lang = self.pool.get('res.users').browse(cr, uid, [uid]).partner_id.lang
        context = dict(context, lang=user_lang)
        uncompute_ids = filter(lambda x: x, [not x.product_lines and x.id or False for x in self.browse(cr, uid, ids, context=context)])
        self.action_compute(cr, uid, uncompute_ids, context=context)
        for production in self.browse(cr, uid, ids, context=context):
            self._make_production_produce_line(cr, uid, production, context=context)

            stock_moves = []
            for line in production.product_lines:
                if line.product_id.type != 'service':
                    stock_move_id = self._make_production_consume_line(cr, uid, line, context=context)
                    stock_moves.append(stock_move_id)
                else:
                    self._make_service_procurement(cr, uid, line, context=context)
            if stock_moves:
                self.pool.get('stock.move').action_confirm(cr, uid, stock_moves, context=context)
            production.write({'state': 'confirmed'})
        return 0

重寫函數(shù), 增加以下代碼實現(xiàn)。

SUPER(mrp_prodduction, self).action_confirm(cr, uid, ids, context=None)
self.message_post(cr, uid, ids, body=_("Order %s confirmed. Please Send Material") % self._description, context=context)

修改結(jié)果


rewrite_function.png

優(yōu)點: 簡單直接
缺點: 需要找到代碼函數(shù),重寫函數(shù)。

定制,根據(jù)狀態(tài)變化

此方法需要修改代碼,在單據(jù)狀態(tài)變化的時候,自動推送消息。

依賴代碼部分
模塊集成

    _inherit = ['mail.thread', 'ir.needaction_mixin']

定義狀態(tài), 增加track_visibility屬性

'state': fields.selection(
            [('draft', 'New'), ('cancel', 'Cancelled'), ('confirmed', 'Awaiting Raw Materials'),
                ('ready', 'Ready to Produce'), ('in_production', 'Production Started'), ('done', 'Done')],
            string='Status', readonly=True,
            track_visibility='onchange', copy=False,

定義_trace 字段以及參數(shù)
ps. mrp.production 中未定義_track, 故狀態(tài)更新 不會推送消息通知。

# Automatic logging system if mail installed
    # _track = {
    #   'field': {
    #       'module.subtype_xml': lambda self, cr, uid, obj, context=None: obj[state] == done,
    #       'module.subtype_xml2': lambda self, cr, uid, obj, context=None: obj[state] != done,
    #   },
    #   'field2': {
    #       ...
    #   },
    # }
    # where
    #   :param string field: field name
    #   :param module.subtype_xml: xml_id of a mail.message.subtype (i.e. mail.mt_comment)
    #   :param obj: is a browse_record
    #   :param function lambda: returns whether the tracking should record using this subtype

其中 module.subtype_xml 需要在xml中定義消息類型。 例如 account_voucher 的跟蹤消息類型

 <!-- Voucher-related subtypes for messaging / Chatter -->
        <record id="mt_voucher_state_change" model="mail.message.subtype">
            <field name="name">Status Change</field>
            <field name="res_model">account.voucher</field>
            <field name="default" eval="False"/>
            <field name="description">Status changed</field>
        </record>

優(yōu)點:根據(jù)狀態(tài)或其他字段自動推送消息。
缺點:定義復(fù)雜。

自動化動作

創(chuàng)建自動話動作,定義對象和條件


Automatci_action.png

定義動作: 更改負責(zé)人 或增加關(guān)注者(本例中可以增加倉庫人員)


set_action1.png

或 更復(fù)雜動作,用服務(wù)器動作定義


create_server_action.png

優(yōu)點 : 用戶可配置
缺點: server action 需要寫python代碼

總結(jié)

以上三種方法,都是使用message_post方法發(fā)送消息給關(guān)注者,如需使用其他發(fā)送消息方法,需要在mail thread尋找新的方法。
方法三,可以自定義配置條件,也可以增加關(guān)注者,也可以增加復(fù)雜動作,靈活。
方法一,對開發(fā)者來說更直接。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 工廠模式類似于現(xiàn)實生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情,實現(xiàn)同樣的效果;這時候需要使用工廠模式。簡單...
    舟漁行舟閱讀 7,827評論 2 17
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,243評論 25 708
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,270評論 0 4
  • 昨天下午更新了下mac系統(tǒng)到版本10.11.3(15D21)后打開Android studio的時候出現(xiàn)了一個bu...
    DevWang閱讀 1,408評論 0 49
  • 宋承煥:妙趣橫生,人生之首,莫屬平昌 ”壓力好大,因為要制作出供世界七十五億人觀看的公演,但只要一投入到會議中,這...
    隔行如童話閱讀 204評論 7 0