odoo13之给模块添加自定义配置项

本文示例代码可查看github仓库:odoo13_custom_config_item

配置项效果图

odoo中给系统添加配置项有两种方式,

一种是使用odoo自带的设置,在设置中添加配置项;效果如下图,

 

第二种是在模块中自定义一个配置项管理菜单,在菜单form视图下添加配置项;效果如下图,

 

使用odoo自带配置项

添加model

models/res_config_settings.py

from odoo import models, fields, api


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    dingtalk_corp_id = fields.Char(string='钉钉corpId')
    dingtalk_app_key = fields.Char(string='钉钉AppKey')
    dingtalk_app_secret = fields.Char(string='钉钉AppSecret')
    dingtalk_agent_id = fields.Char(string='钉钉AgentId')
    system_ip = fields.Char(string="系统ip端口地址")

    def set_values(self):
        super(ResConfigSettings, self).set_values()
        params = self.env['ir.config_parameter'].sudo()
        params.set_param('dingtalk_corp_id', self[0].dingtalk_corp_id)
        params.set_param('dingtalk_app_key', self[0].dingtalk_app_key)
        params.set_param('dingtalk_app_secret', self[0].dingtalk_app_secret)
        params.set_param('dingtalk_agent_id', self[0].dingtalk_agent_id)
        params.set_param('system_ip', self[0].system_ip)

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        params = self.env['ir.config_parameter'].sudo()
        res.update(
            dingtalk_corp_id=params.get_param('dingtalk_corp_id'),
            dingtalk_app_key=params.get_param('dingtalk_app_key'),
            dingtalk_app_secret=params.get_param('dingtalk_app_secret'),
            dingtalk_agent_id=params.get_param('dingtalk_agent_id'),
            system_ip=params.get_param('system_ip'),
        )
        return res

    def sys_group(self):
        self.env["inherit_group"].sys_group()
res_config_settings.py

记得在__init__.py中进行引用。

继承odoo配置视图并添加配置项

views/res_config_settings_view.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- View -->
    <record id="res_config_settings_view_form" model="ir.ui.view">
        <field name="name">res.config.settings.view.form.inherit.dingtalk_connector</field>
        <field name="model">res.config.settings</field>
        <field name="inherit_id" ref="base.res_config_settings_view_form"/>
        <field name="arch" type="xml">
            <xpath expr="//div[hasclass('settings')]" position="inside">
                <div class="app_settings_block" data-string="钉钉" string="钉钉" data-key="dingtalk_connector"
                     groups="base.group_system">
                    <h2>基本参数</h2>
                    <div class="row mt16 o_settings_container">
                        <div class="col-12 col-lg-12 o_setting_box">
                            基本参数是用于钉钉接口的身份认证,请务必填写,否则模块无法使用。其中corpId在<a href="https://open-dev.dingtalk.com/"
                                                                        target="_blank">钉钉开放平台</a>获取,AppKey和AppSecret的获取方法请参考<a
                                href="https://open-doc.dingtalk.com/microapp/serverapi2/eev437" target="_blank">开发文档</a>
                        </div>
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="dingtalk_corp_id"/>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="dingtalk_corp_id"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="dingtalk_app_key"/>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="dingtalk_app_key"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="dingtalk_app_secret"/>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="dingtalk_app_secret"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="dingtalk_agent_id"/>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="dingtalk_agent_id"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="system_ip"/>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="system_ip" placeholder="http://127.0.0.1:8069/"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </xpath>
        </field>
    </record>
</odoo>
res_config_settings_view.xml

在__mainfest__.py中引用xml文件

'data': [
        'views/res_config_settings_view.xml',
    ],

至此,就可以在odoo自带设置中进行模块配置项管理了。

配置项数据获取

通过如下代码可获取配置项中的dingtalk_app_key参数,其他参数同理。

def get_dingtalk_config(self):
    contract_config = self.env["res.config.settings"].sudo().search([])
    dingtalk_app_key = contract_config[0]['dingtalk_app_key']

 

在模块中自定义配置项菜单

添加model

因为某些原因,这里采用另一个模块的配置项。

在models/models.py文件中添加model

class AssetsConfigParameter(models.Model):
    _name = "assets.config.parameter"
    _description = "xx系统配置项"

    zabbix_href = fields.Char(string="zabbix api链接")
    zabbix_username = fields.Char(string="zabbix用户名")
    zabbix_password = fields.Char(string="zabbix密码")

在models/transient_models.py中添加瞬态模型

from odoo import fields, models, api, exceptions


class AssetsConfigTransient(models.Model):
    _name = "assets.config.transient"
    _description = "xx系统配置项瞬态模型"

    zabbix_href = fields.Char(string="zabbix api链接")
    zabbix_username = fields.Char(string="zabbix用户名")
    zabbix_password = fields.Char(string="zabbix密码")

    @api.model
    def create(self, vals):
        configs = self.env["assets.config.parameter"].sudo().search([])
        if configs:
            configs[0].sudo().write(vals)
        else:
            self.env["assets.config.parameter"].sudo().create(vals)
        return super(AssetsConfigTransient, self).create(vals)

    @api.model
    def default_get(self, fields_list):
        transients = super().default_get(fields_list)
        configs = self.env["assets.config.parameter"].sudo().search([])
        if configs:
            transients['zabbix_href'] = configs[0]['zabbix_href']
            transients['zabbix_username'] = configs[0]['zabbix_username']
            transients['zabbix_password'] = configs[0]['zabbix_password']
        return transients

记得在__init__.py文件中引用

 

添加views

在views/views.xml中添加如下内容

<odoo>
    <data>
        <!-- xx配置项模块 开始 -->
            <record id="assets_config_transient_view_form" model="ir.ui.view">
                <field name="name">assets.config.transient.form</field>
                <field name="model">assets.config.transient</field>
                <field name="arch" type="xml">
                    <form string="xx配置项">
                        <sheet>
                            <group>
                                <field name="zabbix_href"/>
                                <field name="zabbix_username" />
                                <field name="zabbix_password" />
                            </group>
                        </sheet>

                    </form>
                </field>
            </record>
            <record id="assets_config_transient_action" model="ir.actions.act_window">
                <field name="name">xx配置项瞬态</field>
                <field name="res_model">assets.config.transient</field>
                <field name="view_mode">form</field>
                <field name="target">current</field>
            </record>
        <!-- xx配置项模块 结束 -->
    </data>
</odoo>

在__mainfest__.py文中中引用views.xm文件,不做描述。

当然,还有在menus.xml中添加菜单,这里同样不做描述。

至此,就可以在模块菜单中设置配置项了。

 

配置项数据获取

通过如下代码可获取配置项中的zabbix_href参数,其他参数同理。

def get_zabbix(self):
    configs = request.env["assets.config.parameter"].sudo().search([])
    zabbix_href = configs[0]['zabbix_href']

 

posted @ 2020-08-03 22:49  YifChan  阅读(383)  评论(0编辑  收藏