From 19867cc330cdbffc113eacc78253d75990f02475 Mon Sep 17 00:00:00 2001 From: william-andre Date: Tue, 30 Jan 2024 13:45:32 +0000 Subject: [PATCH] [FIX] account: make import of discounts thread safe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The patch method is not thread safe, which is very annoying for SH users for instance. One obvious issue is that during one thread patching the method, other threads will also be impacted and have 100 decimal places for the discount. But it is even worse: * thread A start: original = real_original; new = patchedA * thread B start: original = patchedA; new = patchedB * thread A end: reset original to real_original * thread B end: reset original to patchedA Now at the end of the transaction, the original method simply doesn't exist anymore, and we only have one of the patches, which forces a restart of the server to fix it. opw-3552839 closes odoo/odoo#152168 X-original-commit: 9239ee3a95b474ce2eea92747b64f17f58c5c4cc Signed-off-by: Quentin De Paoli Signed-off-by: William André (wan) --- addons/account/models/__init__.py | 1 + addons/account/models/account_move.py | 9 +-------- addons/account/models/decimal_precision.py | 9 +++++++++ 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 addons/account/models/decimal_precision.py diff --git a/addons/account/models/__init__.py b/addons/account/models/__init__.py index 76f6bb8cb6e..2fd3f7504cf 100644 --- a/addons/account/models/__init__.py +++ b/addons/account/models/__init__.py @@ -30,6 +30,7 @@ from . import company from . import res_config_settings from . import account_cash_rounding from . import account_incoterms +from . import decimal_precision from . import digest from . import res_users from . import ir_attachment diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 1eb10ee1508..4ea878dd935 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -11,10 +11,8 @@ from psycopg2 import OperationalError import math import re from textwrap import shorten -from unittest.mock import patch from odoo import api, fields, models, _, Command -from odoo.addons.base.models.decimal_precision import DecimalPrecision from odoo.addons.account.tools import format_structured_reference_iso from odoo.exceptions import UserError, ValidationError, AccessError, RedirectWarning from odoo.tools import ( @@ -3065,12 +3063,7 @@ class AccountMove(models.Model): The reasonning is that if the document that we are importing has a discount, it shouldn't be rounded to the local settings. """ - original_precision_get = DecimalPrecision.precision_get - def precision_get(self, application): - if application == 'Discount': - return 100 - return original_precision_get(self, application) - with patch('odoo.addons.base.models.decimal_precision.DecimalPrecision.precision_get', new=precision_get): + with self._disable_recursion({'records': self}, 'ignore_discount_precision'): yield def _get_edi_decoder(self, file_data, new=False): diff --git a/addons/account/models/decimal_precision.py b/addons/account/models/decimal_precision.py new file mode 100644 index 00000000000..22c8176b257 --- /dev/null +++ b/addons/account/models/decimal_precision.py @@ -0,0 +1,9 @@ +from odoo import models + +class DecimalPrecision(models.Model): + _inherit = 'decimal.precision' + + def precision_get(self, application): + if application == 'Discount' and self.env.context.get('ignore_discount_precision'): + return 100 + return super().precision_get(application)