Search code examples
pythonodooodoo-11

How to override http.py methods in Odoo 11?


I want to override setup_db method for my add-on because in current situation you can't take argument from url with db name, and if the user has more than 1 database I can`t run my login link from incognito.

I don't want the user to go to /web/database/selector first.

I was thinking about the user going to /web/login?db=example_db_name and then somehow redirect to my login link.

("somehow" because if you type it, it redirects you to /web/login, so i cant add redirect from login page).

I'm doing that assuming that in odoo.conf user has

db_name = False, dbfilter = .

Solution

  • If you faced the same problem, here is my solution. It`s overriding the default method which is kinda bad usually, but in our situation there isnt much we can do.

    from odoo import http
    
    class Rooting(http.Root):
        def setup_db(self, httprequest):
            db = httprequest.session.db
            # Check if session.db is legit
            if db:
                if db not in http.db_filter([db], httprequest=httprequest):
                    httprequest.session.logout()
                    db = None
            if not db:
                if 'db' in httprequest.args:
                    db = httprequest.args['db']
                    httprequest.session.db = db
            if not db:
                httprequest.session.db = http.db_monodb(httprequest)
    
    
    http.Root.setup_db = Rooting.setup_db