在一个文档的state变化时,需要将变化情况告知关注用户,通过研究account.invoice的代码,发现是经过如下过程实现此功能的:
1、添加一个消息阶段:
<record id="mt_invoice_paid" model="mail.message.subtype">
<field name="name">paid</field>
<field name="res_model">account.invoice</field>
</record>
2、定义state变更时的触发函数:
def confirm_paid(self, cr, uid, ids, context=None):
if context is None:
context = {}
self.write(cr, uid, ids, {'state':'paid'}, context=context)
self.confirm_paid_send_note(cr, uid, ids, context=context)
return True
3、send_note函数,注意subtype的定义,与第一步的定义相关联。
def confirm_paid_send_note(self, cr, uid, ids, context=None):
for obj in self.browse(cr, uid, ids, context=context):
self.message_post(cr, uid, [obj.id], body=_("%s <b>paid</b>.") % (self._get_document_type(obj.type)),
subtype="account.mt_invoice_paid", context=context)
这样在state变化的时候,消息会通知给关注此文档的用户,同时不同的用户可以设置关注不同的消息变更。
测试方法:
post_vars = {'subject': "这是一个自动通知",
'body': "这个单据已经改变!",
'partner_ids': [(4, 3)],} # Where "4" adds the ID to the list
# of followers and "3" is the partner ID
thread_pool = self.pool.get('mail.thread')
thread_pool.message_post(
cr, uid, False,
type="notification",
subtype="mt_comment",
context=None,
**post_vars)