[FIX] account: make import of discounts thread safe

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 <qdp@odoo.com>
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
william-andre
2024-02-05 18:24:40 +00:00
parent ea27398c4b
commit 19867cc330
3 changed files with 11 additions and 8 deletions
+1
View File
@@ -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
+1 -8
View File
@@ -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):
@@ -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)