Odoo Build - Develop a new module¶
In this guide I'll walk you through the development of a module. From specification to the final commit.
Specification¶
Let's assume have received the following specification.
Develop CRM Lead Template
Module: crm_lead_template
Version: 19.0
Repo: addons/sale_workflow
Depends: crm
Summary: Create lead notes from template.Add model crm.lead.template. Add menu to configuration.
Model has field description and tag_ids.
Add field crm.lead:template_id -> cr.lead.template. When field is selected, the content of description and tag_ids is copied. Copy field content if is empty.
There are some meta information (Module, Version, Repo, Depends, Summary) and broad description what the module should do.
As an experienced Odoo developer you realize that this functionality also exists for sale order and that you can lookup the implementation there.
Enviroment¶
It is assumed that the Odoo Build enviroment is running.
We have access to the Odoo source code.
On the terminal we have use zsh git aliases and the helix editor.
Bootstrap¶
First checkout the correct Odoo version.
Then create the module.
This command will prompt you for values.
Prompt for module manifest values...
Summary: Create lead notes from template.
License (AGPL-3/OPL-1): AGPL-3
Depends (comma-separated): crm
Demo? (y/n): n
Let's add a new model.
[main][~/Odoo-Build]$ task generate-module-model addons/sale_workflow/crm_lead_template crm.lea
d.template
And views for this model.
[main][~/Odoo-Build]$ task generate-module-view addons/sale_workflow/crm_lead_template crm.lead.template
We will extend the crm.lead model as well.
We need to update the crm.crm_lead_view_form form.
[main][~/Odoo-Build]$ task generate-module-inherit-view addons/sale_workflow/crm_lead_template crm.crm_lead_view_form crm.lead
Don't forget about the security rules.
[main][~/Odoo-Build]$ task generate-module-security addons/sale_workflow/crm_lead_template crm.
lead.template
Metadata¶
Let's edit the metadata of the module
I performed the following actions:
- Renamed name from
CrmtoCRM - Added
"views/crm_lead_views.xml",and"views/crm_lead_template_views.xml",to the data list
Model¶
According to the spec we need to add two fields the model. Let's lookup the definition.
[main][~/Odoo-Build]$ rg --no-line-number "description = fields" odoo/addons/crm/**/*.py
odoo/addons/crm/models/crm_lead.py
description = fields.Html('Notes')
[main][~/Odoo-Build]$ rg --no-line-number "tag_ids = fields" odoo/addons/crm/**/*.py -A2
odoo/addons/crm/report/crm_activity_report.py
tag_ids = fields.Many2many(related="lead_id.tag_ids", readonly=True)
won_status = fields.Selection([
('won', 'Won'),
odoo/addons/crm/models/crm_lead.py
tag_ids = fields.Many2many(
'crm.tag', 'crm_tag_rel', 'lead_id', 'tag_id', string='Tags',
help="Classify and analyze your lead/opportunity categories like: Training, Service")
Now we can copy these definition to the model.
- Rename class from
CrmLeadTemplatetoCRMLeadTemplate - Rename name from
CrmtoCRM - Remove
valuefield - Add
descriptionandtag_ids
description = fields.Html("Notes")
tag_ids = fields.Many2many(
"crm.tag",
"crm_template_tag_rel",
"lead_template_id",
"tag_id",
string="Tags",
help="Classify and analyze your lead/opportunity categories like: Training, Service",
)
Do not worry about formatting.
- Remove content inside of
header - Add
tags_idsbeforenotebook.
- Replace
valuewithdescription.
- Remove
valuefrom list and search view. - Replace menu item definition.
<menuitem
id="crm_lead_template.menu_crm_lead_template"
name="Lead Templates"
parent="crm.crm_menu_config"
action="crm_lead_template.action_crm_lead_template_view"
sequence="7"
/>
Inherit¶
Let's have a look at the the sale_management module.
[main][~/Odoo-Build]$ rg --no-line-number "sale_order_template_id = fields" odoo/addons/sale_management/**/*.py -A5
odoo/addons/sale_management/models/sale_order.py
sale_order_template_id = fields.Many2one(
comodel_name='sale.order.template',
string="Quotation Template",
compute='_compute_sale_order_template_id',
store=True, readonly=False, check_company=True, precompute=True,
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
Let's add the template_id field and the business logic.
- Rename class from
CrmLeadtoCRMLead - Add the
temlate_idfield
template_id = fields.Many2one(
comodel_name="crm.lead.template",
string="Lead Template",
inverse="_inverse_template_id",
store=True,
readonly=False,
)
- Add the
_compute_template_idmethod
def _inverse_template_id(self):
"""
Update templated fiels if they are empty and template is updated.
"""
for lead in self:
if not lead.description:
lead.description = lead.template_id.description
if not lead.tag_ids:
lead.tag_ids = lead.template_id.tag_ids
Then update the inherited form.
- Add
template_idafteruser_id.
Lint¶
In the repo folder update the docs and lint the modules.
[19.0][~/Odoo-Build/addons/sale_workflow]$ task docs
[19.0][~/Odoo-Build/addons/sale_workflow]$ task lint
Test¶
Reset the database.
And install the module.
If everything went well, start the Odoo server.
Create a template and and add it on a lead.
Commit¶
Add changes and push them.
[19.0][~/Odoo-Build/addons/sale_workflow]$ gaa
[19.0][~/Odoo-Build/addons/sale_workflow]$ gcmsg "feat(crm_lead_template): init module"
[19.0][~/Odoo-Build/addons/sale_workflow]$ gp
Backlinks