• 多步验证Odoo功能模块



    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)
    },
    

    二、多步校验功能使用方式

    1、首先需要安装和依赖模块multi_step_validation

    # __manifest__.py
    {
      # ....
      "depends": ['dcg_base']
      # ....
    }
    

    2、在需要验证的方法上,定义(多个)验证的子方法(function)。

    并使用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”,可加入技术交流群!

  • 相关阅读:
    Appium+python+unittest搭建UI自动化框架!
    矩阵论理论知识(1)
    【在线编程-Python篇】Python入门 04 列表(中)
    基于HTML旅游网站项目的设计与实现——联途旅游网服务平台网站HTML模板HTML+CSS+JavaScript
    從turtle海龜動畫 學習 Python - 高中彈性課程系列 6.1 內嵌正多邊形 類似禪繞圖
    河流概化技巧方法介绍
    FreeSWITCH rtp 统计
    编译原理实验二、 编译器认知实验报告
    cesium 鹰眼图2
    超越 Transformer开启高效开放语言模型的新篇章
  • 原文地址:https://blog.csdn.net/CBGCampus/article/details/127047775