odoo rpc用法

发布时间 2023-12-20 13:51:46作者: CrossPython

 

js

 

/** @odoo-module **/
import { registry } from "@web/core/registry";
import { Layout } from "@web/search/layout";
import { getDefaultConfig } from "@web/views/view";
import { Component, onWillStart, useSubEnv } from "@odoo/owl";
import { useBus, useService } from "@web/core/utils/hooks";

class MyClientAction extends Component {
    "use strict";
    setup() {
        useSubEnv({
            config: {
                ...getDefaultConfig(),
                ...this.env.config,
            },
        });
        this.rpc = useService("rpc");

        onWillStart(async () => {
                await this.rpc('/testjs/', ['']).then((val)=>{
                    console.log(val);
                });
            });

        this.display = {
            controlPanel: { "top-right": false, "bottom-right": false },
        };
    }
}

MyClientAction.template = "my_module.clientaction";
registry.category("actions").add("my_module.MyClientAction", MyClientAction);

  

controller

from odoo import http
from odoo.http import request, serialize_exception
# from odoo import api, SUPERUSER_ID
import os


class SapbotUpload(http.Controller):
    @http.route('/sapbot/', auth='user', type='http', csrf=False)
    def sapbot_upload(self, *args, **kwargs):
        print('hello.')
        for each in args:
            print(each)
        for each in kwargs:
            print(each, '=>', kwargs[each])
            if each == 'file':
                filepath = os.path.join(os.getcwd(), 'data', 'attachments', kwargs[each].filename)
                kwargs[each].save(filepath)
        return '234234'

    @http.route('/testjs', auth='user', type='http', csrf=False)
    def testjs(self):
        print('testjs, sdff===============================================================')
        return {'abc': 123}

  

 

xml

<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
    <t t-name="my_module.clientaction">
        Hello world, second.
    </t>
</templates>