如何为Odoo 10实现自定义控制器以覆盖默认控制器 [英] How to implement custom controller for Odoo 10 that overides default controller

查看:237
本文介绍了如何为Odoo 10实现自定义控制器以覆盖默认控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问Odoo 10中的/web/session/authenticate方法(来自Vue应用程序),但是我需要进行少量自定义. 基于另一篇文章我看到应该可以超越Odoo的内置控制器,所以我可以在自定义控制器的自定义模块中使用它:

from odoo import http
from odoo.http import request
from odoo.addons.web.controllers.main import Session

class Session(Session):

    @http.route('/web/session/authenticate', type='json', auth="none", cors="*")
    def authenticate(self, db, login, password, base_location=None):
        print("custom authentication method called")

        request.session.authenticate(db, login, password)
        return request.env['ir.http'].session_info()

,可以看到我正在做的就是添加CORS支持(该路由在Vue应用中需要我的axios POST)

我没有收到错误,但是由于某种原因,Odoo从未调用过我的自定义代码.也许这与我的模块的加载顺序有关?

解决方案

默认情况下,如果HTTP请求在会话中没有指定DB,则Odoo将不知道需要使用带有自定义控制器的自定义模块. /p>

要解决此问题,可以将定制模块称为服务器范围的模块".这意味着它在服务器的整个上下文中使用,而不仅仅是在数据库的上下文中使用(它是会话信息的一部分).可以通过添加--load=web,your_module_here或将其添加到Odoo配置中来完成:server_wide_modules = web,your_module_here.

请注意,如果您没有web作为服务器级模块之一,则所有内容最终都将返回404响应. web是Odoo中默认的服务器级模块

I need to access the /web/session/authenticate method (from Vue app) in Odoo 10 but I need to make a tiny customization. Based on another post I can see that it should be possible to over-ride Odoo's built in controllers so I have this in a custom controller, in a custom module:

from odoo import http
from odoo.http import request
from odoo.addons.web.controllers.main import Session

class Session(Session):

    @http.route('/web/session/authenticate', type='json', auth="none", cors="*")
    def authenticate(self, db, login, password, base_location=None):
        print("custom authentication method called")

        request.session.authenticate(db, login, password)
        return request.env['ir.http'].session_info()

When compared with the existing source code, one can see that all I am doing is adding CORS support (needed for my axios POST in the Vue app to that route)

I'm not getting errors, but for some reason my custom code is never called by Odoo. Perhaps this has something to do with the order in which my modules are loading?

解决方案

By default Odoo will not know that the custom module with the custom controller needs to be used if the HTTP request does not have a DB specified in the session.

To work around this, one makes the custom module, a "server-wide module". Which means that its used in the entire context of the server, and not just in context of a database (which is part of the session info). This is done with either adding --load=web,your_module_here or adding this to the Odoo config: server_wide_modules = web,your_module_here.

Note, if you don't have web as one of the server-wide modules, everything will end up with 404 responses. web is the default server-wide module in Odoo

这篇关于如何为Odoo 10实现自定义控制器以覆盖默认控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆