-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
edi_exchange_template: declare tmpl to use explicitly
Relying on the code by convention was fragile for many reasons. Most important: 1. users have to remember about this convention 2. is not clear which template is going to be used by a specific type With this change we make it more clear and deprecate the old behavior.
- Loading branch information
Showing
10 changed files
with
256 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
edi_exchange_template_oca/migrations/14.0.1.6.0/post-migrate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2025 Camptocamp SA (http://www.camptocamp.com) | ||
# @author Simone Orsi <simahawk@gmail.com> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def migrate(cr, version): | ||
if not version: | ||
return | ||
|
||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
|
||
# Look for templates w/ a type set and set them as allowed on the type | ||
# plus link the type to the template | ||
templates = env["edi.exchange.template.output"].search([("type_id", "!=", False)]) | ||
for tmpl in templates: | ||
allowed_type = tmpl.type_id | ||
tmpl.type_id.output_template_id = tmpl | ||
tmpl.allowed_type_ids += allowed_type | ||
tmpl.type_id = None | ||
_logger.info( | ||
"Set output template %s on exchange type %s", | ||
tmpl.name, | ||
allowed_type.name, | ||
) | ||
|
||
# Look for types w/o a template | ||
# and find the template by code to set as output template | ||
types = env["edi.exchange.type"].search([("output_template_id", "=", False)]) | ||
for t in types: | ||
settings = t.get_settings() | ||
generate_usage = settings.get("components", {}).get("generate", {}).get("usage") | ||
if generate_usage: | ||
templates = env["edi.exchange.template.output"].search( | ||
[ | ||
("code", "=", generate_usage), | ||
("backend_type_id", "=", t.backend_type_id.id), | ||
] | ||
) | ||
if len(templates) == 1: | ||
tmpl = templates[0] | ||
tmpl.allowed_type_ids += t | ||
t.output_template_id = tmpl | ||
_logger.info( | ||
"Set output template %s on exchange type %s", | ||
t.output_template_id.name, | ||
t.name, | ||
) | ||
continue | ||
|
||
_logger.warning( | ||
"Cannot set a template for exchange type %s. " | ||
"Either no template found or multiple found.", | ||
t.name, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import edi_backend | ||
from . import edi_exchange_template_mixin | ||
from . import edi_exchange_template_output | ||
from . import edi_exchange_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2025 Camptocamp SA | ||
# @author: Simone Orsi <simone.orsi@camptocamp.com> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
|
||
from odoo import _, api, exceptions, fields, models | ||
|
||
|
||
class EDIExchangeType(models.Model): | ||
_inherit = "edi.exchange.type" | ||
|
||
output_template_id = fields.Many2one( | ||
comodel_name="edi.exchange.template.output", | ||
string="Exchange Template", | ||
ondelete="restrict", | ||
required=False, | ||
help="Template used to generate or process this type.", | ||
) | ||
output_template_allowed_ids = fields.Many2many( | ||
comodel_name="edi.exchange.template.output", | ||
# Inverse relation is defined in `edi.exchange.template.mixin` | ||
relation="edi_exchange_template_type_rel", | ||
column1="type_id", | ||
column2="template_id", | ||
string="Allowed Templates", | ||
help="Templates allowed to be used with this type.", | ||
) | ||
|
||
@api.constrains("output_template_id") | ||
def _check_output_template_id(self): | ||
for rec in self: | ||
tmpl = rec.output_template_id | ||
if tmpl.type_id: | ||
if tmpl.type_id != rec: | ||
raise exceptions.ValidationError( | ||
_("Template type must match exchange type.") | ||
) | ||
if ( | ||
tmpl | ||
and tmpl.allowed_type_ids | ||
and tmpl not in rec.output_template_allowed_ids | ||
): | ||
raise exceptions.ValidationError( | ||
_("Template not allowed for this type.") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.