In the __opererp__.py you can use the special keys like:
- external_dependencies
- pre_init_hook
- post_init_hook
These keys can be very useful for doing lots of stuff like validating the module compatibility and it’s required dependency ,running some script at the time of installation .
External Dependencies
For Example :
As one of my stripe payment module required a python dependency of stripe library so we have use external_dependencies as:
'external_dependencies': { 'python' : ['stripe'], },
At the time of installation if system does not have stripe library installed , it will raise an error and not allow to install the module.
pre init hook and POST INIT HOOK
The ‘pre_init_hook‘ is also a useful key available in __openerp __.py .
For running the logic before registered the module in the ir.module.module you can use ‘pre_init_hook’.
For Example :
As many times we also have validate that the module build for Odoo8 should not be installed in Odoo9 in that case we use pre_init_hook .
For using the pre_init_hook define this key in your __openerp__.py and assing it’s a method created in your __init_.py of module.
In __opnerp__.py:
'pre_init_hook':'pre_init_check',
In __init__.py:
def pre_init_check(cr): # from openerp.service import common # from openerp.exceptions import Warning # version_info = common.exp_version() # server_serie =version_info.get('server_serie') # if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie)) return True
We can define the ‘post_init_hook‘ in the same ways:
In __openerp__.py:
'post_init_hook': 'post_init_check',
In __init__.py:
def post_init_check(cr, registry): # from openerp.service import common # from openerp.exceptions import Warning # version_info = common.exp_version() # server_serie =version_info.get('server_serie') # if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie)) return True
SAMPLE Code
Finally __openerp__.py look like:
.... 'external_dependencies': { 'python' : ['stripe'], }, 'pre_init_hook':'pre_init_check', 'post_init_hook': 'post_init_check', ....
And __init__.py look like:
# -*- coding: utf-8 -*- import models import controllers def post_init_check(cr, registry): # from openerp.service import common # from openerp.exceptions import Warning # version_info = common.exp_version() # server_serie =version_info.get('server_serie') # if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie)) return True def pre_init_check(cr): from openerp.service import common from openerp.exceptions import Warning version_info = common.exp_version() server_serie =version_info.get('server_serie') if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie)) return True
For further code reference you can visit this links over GitHub .
Be the first to comment.