您可以为此目的使用服务器操作。您可以通过访问设置 »» 技术 »» 操作 »» 服务器操作或在您的模块上创建 XML 在 OpenERP 中创建服务器操作。
我将在这里留下一个服务器操作示例,当对象在我正在开发的模块上达到特定状态时,我使用该操作向用户发送电子邮件:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="ir_actions_server_send_email_when_closed_nconf" model="ir.actions.server">
<field name="name">Auto-email when request is closed, not confirmed</field>
<field name="model_id" ref="model_generic_request"/>
<field name="state">email</field>
<field name="type">ir.actions.server</field>
<field name="condition">True</field>
<field name="email">object.requestor.email</field>
<field name="subject">Your request object.name has been closed (not confirmed)</field>
<field name="message"><![CDATA[
THIS IS AN AUTOMATED EMAIL. DO NOT REPLY.
Hello,
We are here to inform you that the request [[object.name]] you submitted on [[object.request_date]] with the following data:
| Request - Details
|=========================
| Number: [[object.id]]
|=========================
| Responsible Person: [[object.responsible_name.name]]
| Request description: [[object.request_description]]
| Stating reasons: [[object.stating_reasons]]
|=========================
| Notes: [[object.notes]]
Has not been confirmed and is closed.
If you have any question, do not hesitate to contact your supervisor.
Thank you!]]>
</field>
</record>
</data>
</openerp>
从工作流调用此操作。在您的情况下,您可以在保存表单时调用它(状态=草稿,也许?)。
因此,您必须在工作流活动定义中添加对服务器操作的调用:
<record model="workflow.activity" id="act_closed_nconf">
<field name="wkf_id" ref="wkf_request" />
<field name="name">request_closed_nconf</field>
<field name="action_id" ref="ir_actions_server_send_email_when_closed_nconf"/>
<field name="kind">function</field>
<field name="action">close_nconf_request()</field>
<field name="flow_stop">True</field>
</record>
希望这会有所帮助!
------ 对更扩展的答案稍作修改-----
好的,我会尝试做一个简短的几乎可以运行的例子。
如果尚未在您的 python 文件中,您必须添加一些状态才能使工作流正常工作。
class whatever(osv.osv):
_name='whatever'
_description='whatever'
_columns={
'name': fields.char('whatever', size=64, required=True),
'state': fields.selection([('draft','Draft'),
('sent','Sent'),
('closed','Closed'),
],
'Status', readonly=True, track_visibility='onchange',
),
(... some other fields in here...)
}
_defaults={
'state': 'draft',
}
#these 3 functions are called by the workflow
def draft(self, cr, uid, ids, context=None):
self.write(cr, uid, ids, {'state': 'draft'})
return True
def send(self, cr, uid, ids, context=None):
self.write(cr, uid, ids, {'state': 'sent'})
return True
def close(self, cr, uid, ids, context=None):
self.write(cr, uid, ids, {'state': 'closed'})
return True
whatever()
然后,您必须有一个适用于您的对象的工作流定义,这将是您的 xml 的内容:
<?xml version="1.0"?>
<openerp>
<data>
<record model="workflow" id="wkf_whatever">
<field name="name">whatever.wkf</field>
<field name="osv">whatever</field>
<field name="on_create">True</field>
</record>
<!-- activities -->
<record model="workflow.activity" id="act_draft">
<field name="wkf_id" ref="wkf_whatever" />
<field name="flow_start">True</field>
<field name="name">draft</field>
<field name="action_id" ref="send_automatic_email"/>
<field name="kind">function</field>
<field name="action">draft()</field>
</record>
<record model="workflow.activity" id="act_send">
<field name="wkf_id" ref="wkf_whatever" />
<field name="name">send</field>
<field name="kind">function</field>
<field name="action">send()</field>
</record>
<record model="workflow.activity" id="act_close">
<field name="wkf_id" ref="wkf_whatever" />
<field name="flow_stop">True</field>
<field name="name">close</field>
<field name="kind">function</field>
<field name="action">close()</field>
</record>
<!-- transitions -->
<record model="workflow.transition" id="whatever_t1">
<field name="act_from" ref="act_draft" />
<field name="act_to" ref="act_send" />
<field name="signal">draft</field>
</record>
<record model="workflow.transition" id="whatever_t2">
<field name="act_from" ref="act_send" />
<field name="act_to" ref="act_close" />
<field name="signal">close</field>
</record>
</data>
</openerp>
活动声明中的<field name="action_id" ref="send_automatic_email"/> 行调用ID为“send_automatic_email”的服务器操作
还有你的动作服务器:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="send_automatic_email" model="ir.actions.server">
<field name="name">Send automatic email</field>
<field name="model_id" ref="model_whatever"/>
<field name="state">email</field>
<field name="type">ir.actions.server</field>
<field name="condition">True</field>
<field name="email">object.requestor.email</field>
<field name="subject">Your whatever: object.name has been created</field>
<field name="message"><![CDATA[
THIS IS AN AUTOMATED EMAIL. DO NOT REPLY.
Hello,
bla bla bla bla
In here you will write whatever you want, and can access to data stored in your database with, for example [[object.name]] to access the field "name"
</field>
</record>
</data>
</openerp>
有了这 3 个文件(以及其中的一些更改!),您应该能够做您想做的事。
别忘了,你必须重启 OpenERP 服务器(为了重新编译你在 python 文件中的更改)并更新你的模块来加载 XML 文件!
祝你好运!
--
差点忘了!
在您的 xml 视图文件中,您必须在表单视图中添加这些按钮才能调用工作流操作:
<header>
<button name="send" class="oe_highlight" string="Send" type="workflow" states="draft"/>
<button name="close" class="oe_highlight" string="Close" type="workflow" states="sent"/>
<field name="state" widget="statusbar" statusbar_visible="draft,sent,closed" />
</header>