[英]Adding Odoo server action to custom module
我有一个为Odoo V8编写的自定义模块,该模块将各种字段等添加到不同的形式,并添加了一些控制器等。
我现在需要添加服务器操作,但是在这方面,文档似乎真的很差。
我在我的models.py中有一个继承类的函数。
class res_partner(osv.osv):
#update Jira customer list (called on update/save of res.partner)
def updatejira(self):
r = requests.post('http://10.10.15.39:4000',data='Worked!')
我希望能够调用此函数向Jira发出请求,以在创建或更新客户时更新其客户列表。
我已经将此添加到我的templates.xml
<record model="ir.actions.server" id="update_jira">
<field name="name">Res Partner Server Action</field>
<field name="model_id" ref="model_res_partner"/>
<field name="condition">True</field>
<field name="code">
self.updatejira()
</field>
</record>
我会以正确的方式解决这个问题吗? 我不想在模块中而不是在UI中执行此操作。 但是同样,该文档是稀疏的,并且在示例方面并没有包含太多内容。
谢谢
@miw
像这样?
import requests
from openerp import models, api
class res_partner(models.Model):
#_name = "ResPartnerChange"
_inherit = "res.partner"
def update_jira(self):
r = requests.post('http://10.10.15.39:4000',data='Worked!')
f = open("test.log","a")
f.write(r.text)
return r.text
def write(self, vals):
res = super(extendedProject, self).write(vals)
for p in self:
self.update_jira()
return res
res_partner()
这似乎也不起作用。
编辑--------
感谢您一直以来的帮助。 这是我的最新尝试。 如果我一直使用@ api.onchange装饰它,我可以让服务器调用update_jira函数。 因此,我试图覆盖写函数来调用update_jira。 但这似乎没有被调用。 我究竟做错了什么?
__author__ = 'karl'
import requests
import json
from openerp import models, api
class ResPartnerChange(models.Model):
_inherit = "res.partner"
def update_jira(self):
if self.jira_id == 0: #If the ID is 0 then the customer does not exist so create it
H = {'Content-Type':'application/json'}
customer = {
"optionvalue": str(self.name),
"id": 0,
"sequence": 0,
"disabled": 'false',
"childOptions": [
{}
]
}
req = requests.post('https://jira.example.com/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoption/customfield_10128', auth=('user','pass'), headers=H, data=json.dumps(customer))
#store the json responce from Jira
jdata = json.loads(req.text)
#Change the Jira ID value to the received one
# res = {
# 'value': {
#
# 'jira_id': jdata['id']
# }
# }
vals = []
vals['jira_id'] = jdata['id']
else:
#update jira customer
data = self.name, self.jira_id, self.active
pass
return vals
def write(self):
vals = self.update_jira()
res = super(ResPartnerChange, self).write(vals)
return res
ResPartnerChange()
所以我想通了。 我没有重写write方法。 相反,我使用@ api.constrains方法装饰器,该装饰器仅在调用create或write方法时被激活。
如果有人感兴趣,这是我的代码。 在保存Odoo客户时,将调用我的方法,然后验证它是客户,而不是供应商,用户等(它们都使用res.partner)。 然后,它会进行快速逻辑测试,以查看客户是新客户(jira id 0)还是现有客户(非0)。 根据具体情况,它调用相关的Jira API来创建或更新存储我的客户详细信息的自定义字段。
感谢@miw向我指出了正确的方向。
__author__ = 'karl'
import requests
import json
from openerp import models, api
import logging
_logger = logging.getLogger(__name__)
class ResPartnerChange(models.Model):
_inherit = "res.partner"
@api.constrains('name','active')
def update_jira(self):
#Only apply to customers not suppliers users etc as they all use the res.partner model
if self.customer == True:
if self.jira_id == 0: #If the ID is 0 then the jira customer does not exist so create it
_logger.info('Not a Customer yet - Creating Jira Customer')
H = {'Content-Type':'application/json'}
customer = {
"optionvalue": str(self.name),
"id": 0,
"sequence": 0,
"disabled": 'false',
"childOptions": [
{}
]
}
req = requests.post('https://example.com/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoption/customfield_10128', auth=('apiuser','apipass'), headers=H, data=json.dumps(customer))
_logger.info(req.text)
#store the json responce from Jira
jdata = json.loads(req.text)
#create values dictionary
vals = {}
vals['jira_id'] = jdata['id']
self.write(vals)
else:
#update jira customer
_logger.info('Existing Customer - Updating Jira Customer')
vals = {}
vals['name'] = self.name
vals['active'] = self.active
if self.active:
disabled = "false"
else:
disabled = "true"
H = {'Content-Type':'application/json'}
customer = {
"optionvalue": str(self.name),
"id": str(self.jira_id),
"sequence": 0,
"disabled": disabled,
"childOptions": [
{}
]
}
req = requests.put('https://example.com/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoption/customfield_10128/'+str(self.jira_id), auth=('apiuser','apipass'), headers=H, data=json.dumps(customer))
_logger.info(req.text)
return True
else:
return True
ResPartnerChange()
我不确定您的方向正确。 任何python代码(嗯,大多数...)都是在Odoo服务器中执行的,而不是在浏览器/ UI中执行的。 因此,对于您来说,创建名称为“ method_name”的“ object”类型的按钮可能更有意义,然后您将通过技巧来调用Jira。
另一个选择是重写子类中的write()
方法以添加调用,如下所示:
def write(self, vals):
res = super(extendedProject, self).write(vals)
for p in self:
# do the call to JIRA here for each partner p, using p.field1, p.field2 etc.
return res
vals
是具有修改后的值以存储的字典; self
是一个记录集,其中包含所有要存储值的记录。 请注意,从您的write
调用返回后,Odoo中的事务可能仍会回滚。
最后,您可以尝试首先以交互方式在“设置”>“技术”>“操作”>“服务器操作”下创建服务器操作。 这将允许立即反馈/测试。 请注意,服务器操作是在没有特定对象上下文的情况下执行的,而是基于计时器的。
这是该表单的一小段文档:
# Available locals:
# - time, datetime, dateutil: Python libraries
# - env: Odoo Environement
# - model: Model of the record on which the action is triggered
# - object: Record on which the action is triggered if there is one, otherwise None
# - workflow: Workflow engine
# - Warning: Warning Exception to use with raise
这样,您将需要修改代码,使其首先找到要推送到Jira的res.partners,然后对其进行循环并为每个合作伙伴推送到Jira。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.