Odoo自定义功能按钮触发后端业务逻辑多步校验功能如下图:


models: 自定义warning类型、定义method的装饰类
UserCustomWarning: 方便前端 CrashManager的自定义提示逻辑
class UserCustomWarning(except_orm):
def __init__(self, action):
super(UserCustomWarning, self).__init__('', action)
warning_on_user_error: 定义methond装饰类、作用于自定义的methond方法上,通过捕获自定义的Warning类型,包装提示Action信息给前端处理
def warning_on_user_error(check_func):
def check_or_skip(*args):
model_method_frame = inspect.currentframe().f_back
while model_method_frame and 'self' not in model_method_frame.f_locals and 'env' not in \
model_method_frame.f_locals['self']:
model_method_frame = model_method_frame.f_back
if not model_method_frame:
raise Exception(_t('warning_on_user_error不能独立使用'))
check_func_name = check_func.__name__
skip_check = '__%s_skip' % check_func_name
self = model_method_frame.f_locals['self']
args2 = model_method_frame.f_locals.get('args')
if not args2:
args2 = []
if self.env.context.get(skip_check):
_logger.debug(_t('%s不为空,跳过检查: %s'), skip_check, check_func_name)
return
_, _, model_method, _, _ = inspect.getframeinfo(model_method_frame)
result = None
try:
result = check_func(*args)
except UserCustomWarning as e:
message = e.value
ctx = dict(self._context)
ctx.update({skip_check: True})
action = {
'type': 'ir.actions.act_window.message',
'title': _t('提示'),
'message': message,
'is_html_message': True,
'button_close': True,
'buttons': [
{
'type': 'button',
'name': _t('继续'),
'model': self._name,
'method': model_method,
'args': [self.ids] + list(args2),
# dictionary of keyword arguments
'kwargs': {
'context': ctx
},
# button style
'classes': 'btn-primary',
}
]
}
raise UserCustomWarning(action)
return result
return check_or_skip
js:主要要重写 crashManage的自定义处理逻辑、重写ActionManager的_handleAction方法添加特殊
crashManage:前端crashManage重写处理后端自定义异常,通过display的doAction方法展示多步校验提示信息
odoo.define('web.UserWarning', function (require) {
let core = require('web.core');
let Widget = require('web.Widget');
let CrashManager = require('web.CrashManager')
let WarningDialog = CrashManager.WarningDialog
let _t = core._t
let UserCustomWarning = Widget.extend({
init: function (parent, error) {
this._super(parent)
this.error = error
},
display: function () {
var self = this
var error = this.error
// var message = error.data.arguments[0]
var action = error.data.arguments[1]
self.do_action(action)
},
})
core.crash_registry.add('odoo.addons.multi_step_validation.models.exceptions.UserCustomWarning', UserCustomWarning)
})
注意:crash_registry添加的 key值格式为odoo.addons.multi_step_validation.models.exceptions.UserCustomWarning ,不然前端自定义处理对应不上会直接提示Odoo错误提示框
ActionManager:前端ActionManager重写type为ir.actions.act_window.message的自定义action处理,弹出Odoo的dialog提示进行业务处理
_handleAction: function (action, options) {
if (action.type === 'ir.actions.act_window.message') {
return this._executeWindowMessageAction(action, options)
}
return this._super.apply(this, arguments)
},
# __manifest__.py
{
# ....
"depends": ['dcg_base']
# ....
}
并使用api.warning_on_user_error装饰该方法,需要 提醒时,直接raise UserCustomWarning(‘检验内容提示’),在界面上点击“继续”后,系统会跳过该验证并继续执行:
from odoo import api
class Model1(models.Model):
def click_save(self):
self.test_raise()
......
@api.warning_on_user_error
def test_raise(self):
raise UserCustomWarning('检验数据有误,是否继续?')
版权声明:该内容由神州数码云基地团队编撰,转载请注明出处!
微信公众号后台回复“Odoo”,可加入技术交流群!