Contributors mailing list archives

contributors@odoo-community.org

Re: How to write hook addon properly.

by
Ecosoft, Kitti Upariphutthiphong
- 08/03/2019 08:37:15
Thank you (very deep though :)

By the way, a less technical question. Says we have already the hook addon, i.e., account_invoice_do_something_hook.  And this addon is being used for l10n-xxxxx addon.

Should this addon be in l10n-xxxxx repo, or it should find its most related, i.e., account-invoicing, even no addon there using it?

On Fri, Mar 8, 2019 at 1:31 PM Holger Brunn <hbrunn@therp.nl> wrote:
some nitpickings



> All of the python code is "loaded and available" and the classes are


> instantiated

Odoo loads (as in `import ...`) all code that's in the addon path. It does not 
instantiate the classes defined in non installed modules as in `my_class()`



> whether the module is installed or not - Odoo will control,


> by knowledge of the modules installed, which methods are to be called and


> in which order.

actually, the framework won't control anything for non-installed modules. And 
it can't and it shouldn't. If a module needs monkey patching I'd suggest to do 
this in _register_hook of some model, then you know your module is actually 
installed.

For models, Odoo will create python classes on the fly mimicking the 
inheritance chain of the modules installed, so if you have modules A and B 
both having a model inheriting from res.partner, and A depends on B, Odoo will 
carve classes that in python inheritance look somewhat like

base#res.partner
B#res.partner (inherits from base#res.partner)
A#res.partner (inherits from B#res.partner)

If you need to do fancy stuff with inheritance, inspect self.__mro__ - this is 
more or less where python stores the chain of functions to call when you say 
super(...). You can also manipulate this programmatically, or call just the 
version of the function you need.

So what the example code should be doing is

class MrpProductProduce(models.TransientModel):
    _inherit = "mrp.product.produce"

    def _register_hook(self):
        # do the patching, and detect if it happend before
        # (models are registered with every registry reload, that can happen
        # on run time too)
        # cf https://github.com/OCA/OCB/blob/9.0/openerp/models.py#L5329
        return super()._register_hook() # 

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe