2
0

[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.
This commit is contained in:
Pedro M. Baeza 2020-03-24 18:08:56 +01:00 committed by Luis J. Salvatierra
parent 9da7d54ff8
commit 2ec48c4fea
2 changed files with 22 additions and 16 deletions

View File

@ -7,7 +7,7 @@
{ {
"name": "Detect changes and update the Account Chart from a template", "name": "Detect changes and update the Account Chart from a template",
"summary": "Wizard to update a company's 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)", "author": "Tecnativa, BCIM, Okia, Odoo Community Association (OCA)",
"website": "http://github.com/OCA/account-financial-tools", "website": "http://github.com/OCA/account-financial-tools",
"depends": ["account"], "depends": ["account"],

View File

@ -728,12 +728,19 @@ class WizardUpdateChartsAccounts(models.TransientModel):
result.append(_("Tax is disabled.")) result.append(_("Tax is disabled."))
return "\n".join(result) return "\n".join(result)
def missing_xml_id(self, real_obj): @tools.ormcache("self", "template", "real_obj")
return not self.env["ir.model.data"].search( 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), ("res_id", "=", real_obj.id),
("model", "=", real_obj._name), ("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) tax = self.env["account.tax"].browse(tax_id)
notes = self.diff_notes(template, tax) 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.") notes += (notes and "\n" or "") + _("Missing XML-ID.")
if notes: if notes:
@ -817,7 +824,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
account = self.env["account.account"].browse(account_id) account = self.env["account.account"].browse(account_id)
notes = self.diff_notes(template, account) 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.") notes += (notes and "\n" or "") + _("Missing XML-ID.")
if notes: if notes:
@ -859,7 +866,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
fp = self.env["account.fiscal.position"].browse(fp_id) fp = self.env["account.fiscal.position"].browse(fp_id)
notes = self.diff_notes(template, fp) 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.") notes += (notes and "\n" or "") + _("Missing XML-ID.")
if notes: if notes:
@ -880,12 +887,9 @@ class WizardUpdateChartsAccounts(models.TransientModel):
[("model", "=", template._name), ("res_id", "=", template.id)] [("model", "=", template._name), ("res_id", "=", template.id)]
) )
new_xml_id = "%d_%s" % (self.company_id.id, template_xmlid.name) new_xml_id = "%d_%s" % (self.company_id.id, template_xmlid.name)
ir_model_data.search(
real_xmlid = ir_model_data.search( [("model", "=", real_obj._name), ("res_id", "=", real_obj.id)]
[("model", "=", real_obj._name), ("res_id", "=", real_obj.id)], limit=1 ).unlink()
)
if real_xmlid:
real_xmlid.unlink()
template_xmlid.copy( template_xmlid.copy(
{ {
"model": real_obj._name, "model": real_obj._name,
@ -919,7 +923,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
continue continue
tax[key] = value tax[key] = value
_logger.info(_("Updated tax %s."), "'%s'" % template.name) _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) self.recreate_xml_id(template, tax)
_logger.info( _logger.info(
_("Updated tax %s. (Recreated XML-IDs)"), "'%s'" % template.name _("Updated tax %s. (Recreated XML-IDs)"), "'%s'" % template.name
@ -972,7 +976,9 @@ class WizardUpdateChartsAccounts(models.TransientModel):
_("Updated account %s."), _("Updated account %s."),
"'{} - {}'".format(account.code, account.name), "'{} - {}'".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) self.recreate_xml_id(template, account)
_logger.info( _logger.info(
_("Updated account %s. (Recreated XML-ID)"), _("Updated account %s. (Recreated XML-ID)"),
@ -1067,7 +1073,7 @@ class WizardUpdateChartsAccounts(models.TransientModel):
_("Updated fiscal position %s."), "'%s'" % template.name _("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) self.recreate_xml_id(template, fp)
_logger.info( _logger.info(
_("Updated fiscal position %s. (Recreated XML-ID)"), _("Updated fiscal position %s. (Recreated XML-ID)"),