What are the various types of hooks available in Odoo 17?

Hooks are functionalities that execute before, after, or instead of existing code. These functions, represented as strings, are typically stored in the https://init.py/ file of an Odoo module.

The hooks defined in the module's https://manifest.py/ file are identified using specific keywords, such as

1. Pre_init_hook 

2. Post_init_hook

3. Uninstall_hook

1. Post-initialization hook

Post-init hook functions execute after the module is installed. These functions are specified by their names within the https://init.py/ file and are usually tasked with generating specific data. The post-init hook can receive arguments, such as the environment (env).

For example,

The below code generates the data for the company.employee model.

 from odoo import fields, models
class Company(models.Model):
  _name = "company.employee"
  _description = "Company"
  name = fields.Char(string="Name", required=True)
  phone = fields.Char(string="Phone Number")
  email = fields.Char(string="Email", required=True)

The `post_init_hook` key is used in the https://manifest.py/ file of the module to define the hook. After the module is installed, the method specified in the `post_init_hook` is called.

In the manifest file, you can define a post_init_function as shown below.

'post_init_hook': 'create_employees',

In the https://init.py/ file, import the post_init_hook function and declare it as shown below.

def create_employees(env):
    env['company.employee'].create({
      'name': ABC,
      'phone': '+7865 6675 2341',
      'email': abc123@gmail.com'
  })

In the https://init.py/ file of the module, include the create_employee method. This function, executed after installation, facilitates the creation of employee records as an example. The post_init_hook takes the environment as an argument, demonstrating an instance of the create_employee function that runs after module installation. Within the https://init.py/ file,

To finalize the module installation, the post_init_function can be utilized to carry out specific database operations.

2. Pre-initialization Hook

Pre-init hooks are actions executed prior to module installation. When a user triggers the installation process by clicking the module's install button, the pre_init_hook function defined within the module is invoked. This function is utilized to perform specific operations before the module installation proceeds. An example featuring the pre_init_hook function with the previously created model company.employee is illustrated.

You can specify a pre_init_hook function in your manifest file, as shown below.

'pre_init_hook': '_pre_init_employee',

Inside the https://init.py/ file of the module, the _pre_init_employee function is defined. This function, invoked by the pre_init_hook, is enhanced by including the environment as a parameter.

def _pre_init_employee(env):
   env.cr.execute("""
       ALTER TABLE "company_employee"
       ADD COLUMN "place" VARCHAR,
       ADD COLUMN "job" VARCHAR,   
   """)

To configure the database prior to module installation, pre_init_hook can be utilized. This function, currently integrated into the init file, executes before the module is installed. Specifically for the company.employee model, this function introduces several additional fields.

3. Uninstall Hook

This function is triggered immediately after the module is uninstalled, with the uninstall hook accepting the database environment as an argument.

You can define an Uninstall_hook function in your manifest file, as demonstrated below.

'uninstall_hook': 'uninstall_hook'

The uninstall hook function is specified in the https://hooks.py/ file as shown below:

def uninstall_hook(env):
      installed_providers = env['payment.provider'].search([('module_id.state', '=', 'installed')])
   env['account.payment.method'].search([
       ('code', 'in', installed_providers.mapped('code')),
       ('payment_type', '=', 'inbound'),
   ]).unlink()

In the provided function, it removes `https://account.payment.method%60/ records generated for the installed payment providers.

How to Extend and Modify an Existing Mail Template in Odoo 17