From 2ec48c4feaa43ceac251b59855ae49707df442a5 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Tue, 24 Mar 2020 18:08:56 +0100 Subject: [PATCH] [IMP] account_chart_update: Search for real XML-IDs only Previously, only a check on an existing XML-ID which is not coming from exportation was done, but now we check that the XML-ID is the exact one. With this, we avoid that the same record changed their XML-ID and thus, there's no more link between them. --- account_chart_update/__manifest__.py | 2 +- .../wizard/wizard_chart_update.py | 36 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/account_chart_update/__manifest__.py b/account_chart_update/__manifest__.py index 8dce57b0..e518420b 100644 --- a/account_chart_update/__manifest__.py +++ b/account_chart_update/__manifest__.py @@ -7,7 +7,7 @@ { "name": "Detect changes and update the Account Chart from a template", "summary": "Wizard to update a company's account chart from a template", - "version": "13.0.1.0.3", + "version": "13.0.1.0.4", "author": "Tecnativa, BCIM, Okia, Odoo Community Association (OCA)", "website": "http://github.com/OCA/account-financial-tools", "depends": ["account"], diff --git a/account_chart_update/wizard/wizard_chart_update.py b/account_chart_update/wizard/wizard_chart_update.py index c1aa8e91..1b1923b3 100644 --- a/account_chart_update/wizard/wizard_chart_update.py +++ b/account_chart_update/wizard/wizard_chart_update.py @@ -728,12 +728,19 @@ class WizardUpdateChartsAccounts(models.TransientModel): result.append(_("Tax is disabled.")) return "\n".join(result) - def missing_xml_id(self, real_obj): - return not self.env["ir.model.data"].search( + @tools.ormcache("self", "template", "real_obj") + def missing_xml_id(self, template, real_obj): + ir_model_data = self.env["ir.model.data"] + template_xmlid = ir_model_data.search( + [("model", "=", template._name), ("res_id", "=", template.id)] + ) + new_xml_id = "%d_%s" % (self.company_id.id, template_xmlid.name) + return not ir_model_data.search( [ ("res_id", "=", real_obj.id), ("model", "=", real_obj._name), - ("module", "!=", "__export__"), + ("module", "=", template_xmlid.module), + ("name", "=", new_xml_id), ] ) @@ -763,7 +770,7 @@ class WizardUpdateChartsAccounts(models.TransientModel): tax = self.env["account.tax"].browse(tax_id) notes = self.diff_notes(template, tax) - if self.recreate_xml_ids and self.missing_xml_id(tax): + if self.recreate_xml_ids and self.missing_xml_id(template, tax): notes += (notes and "\n" or "") + _("Missing XML-ID.") if notes: @@ -817,7 +824,7 @@ class WizardUpdateChartsAccounts(models.TransientModel): account = self.env["account.account"].browse(account_id) notes = self.diff_notes(template, account) - if self.recreate_xml_ids and self.missing_xml_id(account): + if self.recreate_xml_ids and self.missing_xml_id(template, account): notes += (notes and "\n" or "") + _("Missing XML-ID.") if notes: @@ -859,7 +866,7 @@ class WizardUpdateChartsAccounts(models.TransientModel): fp = self.env["account.fiscal.position"].browse(fp_id) notes = self.diff_notes(template, fp) - if self.recreate_xml_ids and self.missing_xml_id(fp): + if self.recreate_xml_ids and self.missing_xml_id(template, fp): notes += (notes and "\n" or "") + _("Missing XML-ID.") if notes: @@ -880,12 +887,9 @@ class WizardUpdateChartsAccounts(models.TransientModel): [("model", "=", template._name), ("res_id", "=", template.id)] ) new_xml_id = "%d_%s" % (self.company_id.id, template_xmlid.name) - - real_xmlid = ir_model_data.search( - [("model", "=", real_obj._name), ("res_id", "=", real_obj.id)], limit=1 - ) - if real_xmlid: - real_xmlid.unlink() + ir_model_data.search( + [("model", "=", real_obj._name), ("res_id", "=", real_obj.id)] + ).unlink() template_xmlid.copy( { "model": real_obj._name, @@ -919,7 +923,7 @@ class WizardUpdateChartsAccounts(models.TransientModel): continue tax[key] = value _logger.info(_("Updated tax %s."), "'%s'" % template.name) - if self.recreate_xml_ids and self.missing_xml_id(tax): + if self.recreate_xml_ids and self.missing_xml_id(template, tax): self.recreate_xml_id(template, tax) _logger.info( _("Updated tax %s. (Recreated XML-IDs)"), "'%s'" % template.name @@ -972,7 +976,9 @@ class WizardUpdateChartsAccounts(models.TransientModel): _("Updated account %s."), "'{} - {}'".format(account.code, account.name), ) - if self.recreate_xml_ids and self.missing_xml_id(account): + if self.recreate_xml_ids and self.missing_xml_id( + template, account + ): self.recreate_xml_id(template, account) _logger.info( _("Updated account %s. (Recreated XML-ID)"), @@ -1067,7 +1073,7 @@ class WizardUpdateChartsAccounts(models.TransientModel): _("Updated fiscal position %s."), "'%s'" % template.name ) - if self.recreate_xml_ids and self.missing_xml_id(fp): + if self.recreate_xml_ids and self.missing_xml_id(template, fp): self.recreate_xml_id(template, fp) _logger.info( _("Updated fiscal position %s. (Recreated XML-ID)"),