Enable Dark Mode!
pre-post-hooks-odoo.png
By: Aswani

External Dependencies ,Pre Init Hook & Post Init Hook In Odoo

Technical

When we creating a module in odoo important thing is __manifest__.py( for odoo 10.0 and later versions) or __openerp__.py(for odoo 9.0 and earlier versions).


In manifest file, we can define


  1. name (str, required):- Human readable name of the module(other than the technical name)


  2. version(str) :- Module version(should follow version rules).


  3. summary(str):- Short description of the module.


  4. description (str):-Extended description for the module.


  5. author (str):- Module author’s name.


  6. website (str):- Module author’s website URL.


    license (str, defaults: AGPL-3):- Distribution license for the module.


  7. category (str, default: Uncategorized) :- Classification category within Odoo.


  8. depends (list(str)):- Depended on Odoo modules which must be loaded before the module.

  9. data (list(str)):- Path of all data files from the root directory which must always be installed or updated with the module.

  10. demo (list(str)):- Path of all demo data files which are only installed or updated in demonstration mode.

  11. auto_install (bool, default: False):- If True, the module will automatically be installed if all dependent modules are installed.


Except for those keys in odoo __manifest__.py we have some special keys for module compatibility check, adding required dependencies and for running scripts during module installation. Those keys are


-pre_init_hook

-External_dependencies

-post _init_hook



External dependency


In __manifest__.py file extenal_dependency key is used for declaring all external libraries (python packages or any binaries) that have to be installed to make the module work. External dependencies are defined in __manifest__.py file under external dependency key like below


"external_dependencies": {"python": [], "bin": []},

Check if some Python library exists:


"external_dependencies": {"python" : ["openid"]}

Check if some system application exists:


"external_dependencies": {"bin" : ["libreoffice"]}

If someone created a module in Odoo, which uses Python modules for its working. So at the time of module installation if the system does not find Python packages or any binaries defined in __maifest__.py installed in the system, it will raise an error and not allow to install the module.


For example:


In Odoo's  ‘Weighing Scale Hardware Driver’ module’s __manifest__.py file there is a line


‘external_dependencies’: {‘python’:[‘serial’]}


For defining python dependency file ‘scale’ for the module ‘Weighing Scale Hardware Driver’, and this module allows the point of sale to connect to a scale using a USB HSM serial scale interface such as Mettler Toledo Ariva. The module requires Python dependency of ‘scale’ library, so in the module they use external_dependencies’.So at the time of module installation, if the system does not find ‘scale’ library installed, it will raise an error and not allow to install the module.


Pre Init Hook:

The ‘pre_init_hook‘ is also a special key available in  __manifest__.py. During installation, when the user clicks to install the custom module, it will call a Pre-installation hook and do some initialization, copying of files or other and then after that once it finished, we can capture the return installation process of Odoo and run a Post-installation hook.


The pre_init_hook is called with a database cursor and may perform modifications in the database to prepare for the module installation.

You can use pre_init_hook’ before registering the module's logic in the ir.module.module. It is commonly used for modules compatibility check.


For using the pre_init_hook define this key in your __manifest__.py and assigning it’s a method created in your __init_.py of the module(Value of the key is the name of a Python function which must be defined in __init__.py).

 

For Example :


As many times we also have validated that the module build for Odoo 8 should not be installed in Odoo 9 in that case we use pre_init_hook .


In  __opnerp__.py:
‘pre_init_hook’:’version_check’
In  __init__.py:
def version_check(cr):
   # version check
    return True

Post Init Hook:


The post init hook is called with a database cursor and registry object and may perform modifications in the database to finalize the module installation. We can define the ‘post_init_hook‘ in the same ways as pre_init_hook.


For Example :


In odoo Invoicing module __manifest__.py they using post init hook


For auto installing l10n module


In  __openerp__.py:
‘Post_init_hook’:’_auto_install_l10n’,

In __init__.py:
def _auto_install_l10n(cr, registry):
   #check the country of the main company (only) and eventually load some module needed in   that country
   #auto install localization module(s) if available


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message