Contributors mailing list archives

contributors@odoo-community.org

Browse archives

Avatar

Re: How to write hook addon properly.

by Richard deMeester <richard.demeester@willdooit.com> - 11/03/2019 23:48:36
Ooh, thankyou Holger,

Being all self taught by trial and error, I thought my solution was elegant enough, but I really do appreciate the more refined solution you have presented.

I do realise that the classes are not instantiated - that was more of a typo caused by lacking the correct way of explaining things.

Also "actually, the framework won't control anything for non-installed modules" - kind-of what I meant - that it will call and control what it needs to, and will ignore and not call what is not installed.

Am I oversimplifying by suggesting that the true difference between between our 2 suggestions is that:
  • my solution will always cause a reassignment of the base method to the new method;
  • the _register_hook solution will cause the reassignment only if the module is installed.
Is there any other difference or benefit?

 # do the patching, and detect if it happend before
I guess this is just a proper fail-safe, because it if is a simple assignment, then it shouldn't matter if it has happened before.  Or does it?

Richard



<img class="EmojiInsert" style="margin:0px; width:148.5pt; height:93.74pt" data-outlook-trace="F:0|T:1" src="cid:image002.jpg@01D4BDF2.E63F2420">

Richard deMeester

Senior Development Analyst

WilldooIT Pty Ltd

E: richard.demeester@willdooit.com

M: +61 403 76 76 76

P: +61 3 9135 1900

A: 10/435 Williamstown Road, Port Melbourne, Vic 3207

 

 

Our vision is to provide end to end IT solutions to help our clients achieve their desired objectives and provide growth opportunities for everyone working in the company to reach their full professional potential.

 

cid:image005.png@01D33CFB.CF033C70

 

DISCLAIMER | This electronic message together with any attachments is confidential. If you are not the recipient, do not copy, disclose, or use the contents in any way. Please also advise us by e-mail that you have received this message in error and then please destroy this email and any of its attachments. WilldooIT Pty. Ltd. is not responsible for any changes made to this message and/or any attachments after sending by WilldooIT Pty. Ltd. WilldooIT Pty. Ltd. use virus scanning software but exclude all liability for virus or anything similar in this email or attachment.



From: Holger Brunn <hbrunn@therp.nl>
Sent: Friday, 8 March 2019 5:31 PM
To: Contributors
Subject: Re: How to write hook addon properly.
 
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

Reference