From 79bbd7cdc45c3f496c339a0cdf272a5669641043 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Wed, 11 Jan 2023 10:19:31 +0000 Subject: [PATCH] [FIX] account_chart_update: false positive detecting diff on Html fields When executing the chart updater and selecting the "notes" field from fiscal positions, you were getting differences 100% of the time. This was because [fiscal position's notes field is Html][1], while [the template field is Text][2]. @moduon MT-1912 [1]: https://github.com/odoo/odoo/blob/5ef647d5d452a1b334ad6f987d9bf0722b4b8ae7/addons/account/models/partner.py#L32 [2]: https://github.com/odoo/odoo/blob/5ef647d5d452a1b334ad6f987d9bf0722b4b8ae7/addons/account/models/chart_template.py#L1470 --- account_chart_update/readme/CONTRIBUTORS.rst | 2 +- .../tests/test_account_chart_update.py | 7 +++---- .../wizard/wizard_chart_update.py | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/account_chart_update/readme/CONTRIBUTORS.rst b/account_chart_update/readme/CONTRIBUTORS.rst index c55a6e97..7308e634 100644 --- a/account_chart_update/readme/CONTRIBUTORS.rst +++ b/account_chart_update/readme/CONTRIBUTORS.rst @@ -1,7 +1,6 @@ * `Tecnativa `_: * Pedro M. Baeza - * Jairo Llopis * Ernesto Tejeda * Jacques-Etienne Baudoux @@ -9,3 +8,4 @@ * Nacho Muñoz * Alberto Martín - Guadaltech * Fernando La Chica - GreenIce +* Jairo Llopis (https://www.moduon.team/) diff --git a/account_chart_update/tests/test_account_chart_update.py b/account_chart_update/tests/test_account_chart_update.py index e5f55642..4c8a98c7 100644 --- a/account_chart_update/tests/test_account_chart_update.py +++ b/account_chart_update/tests/test_account_chart_update.py @@ -327,7 +327,7 @@ class TestAccountChartUpdate(common.HttpCase): self.account_template.tag_ids = [ (6, 0, [self.account_tag_1.id, self.account_tag_2.id]) ] - self.fp_template.note = "

Test note

" + self.fp_template.note = "Test note. \n \n Multiline. \n" self.fp_template.account_ids.account_dest_id = new_account_tmpl.id self.fp_template.tax_ids.tax_dest_id = self.tax_template.id wizard = self.wizard_obj.create(self.wizard_vals) @@ -341,7 +341,7 @@ class TestAccountChartUpdate(common.HttpCase): self.assertEqual(wizard.account_ids.account_id, self.account_template) self.assertEqual(wizard.account_ids.type, "updated") self.assertTrue(wizard.fiscal_position_ids) - self.assertTrue(wizard.fiscal_position_ids.type, "updated") + self.assertEqual(wizard.fiscal_position_ids.type, "updated") self.assertEqual( wizard.fiscal_position_ids.fiscal_position_id, self.fp_template ) @@ -359,7 +359,7 @@ class TestAccountChartUpdate(common.HttpCase): self.assertEqual(self.account.name, self.account_template.name) self.assertIn(self.account_tag_1, self.account.tag_ids) self.assertIn(self.account_tag_2, self.account.tag_ids) - self.assertEqual(self.fp.note, self.fp_template.note) + self.assertEqual(self.fp.note, f"

{self.fp_template.note}

") self.assertEqual(self.fp.account_ids.account_dest_id, new_account) self.assertEqual(self.fp.tax_ids.tax_dest_id, self.tax) wizard.unlink() @@ -384,7 +384,6 @@ class TestAccountChartUpdate(common.HttpCase): self.assertFalse(wizard.fiscal_position_ids) self.tax_template.description = "Test description" self.account_template.name = "Other name" - self.fp_template.note = "

Test note

" wizard.unlink() # Remove objects new_tax_tmpl.unlink() diff --git a/account_chart_update/wizard/wizard_chart_update.py b/account_chart_update/wizard/wizard_chart_update.py index 834a1501..714888e1 100644 --- a/account_chart_update/wizard/wizard_chart_update.py +++ b/account_chart_update/wizard/wizard_chart_update.py @@ -13,6 +13,8 @@ import logging from contextlib import closing from io import StringIO +from markupsafe import Markup + from odoo import _, api, exceptions, fields, models, tools from odoo.tools import config @@ -672,7 +674,7 @@ class WizardUpdateChartsAccounts(models.TransientModel): return set(models.MAGIC_COLUMNS) | specials @api.model - def diff_fields(self, template, real): + def diff_fields(self, template, real): # noqa: C901 """Get fields that are different in template and real records. :param odoo.models.Model template: @@ -721,10 +723,18 @@ class WizardUpdateChartsAccounts(models.TransientModel): ): result[key] = expected else: - template_value = template[key] + template_value, real_value = template[key], real[key] if template._name == "account.account.template" and key == "code": template_value = self.padded_code(template["code"]) - if template_value != real[key]: + # Normalize values when one field is Char and the other is Html + if ( + isinstance(template_value, str) and isinstance(real_value, Markup) + ) or ( + isinstance(template_value, Markup) and isinstance(real_value, str) + ): + template_value = Markup(template_value).striptags() + real_value = Markup(real_value).striptags() + if template_value != real_value: result[key] = template_value # Avoid to cache recordset references if key in result: