Skip to content

Commit 84933a4

Browse files
committed
[ADD] sixteen_in_fourteen
1 parent 2fb9ea3 commit 84933a4

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

sixteen_in_fourteen/__init__.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import sys
2+
import importlib
3+
4+
MOVED_MODULES = {
5+
"odoo.addons.sale.models.sale_order_line": "odoo.addons.sale.models.sale",
6+
"odoo.addons.sale.models.sale_order": "odoo.addons.sale.models.sale",
7+
}
8+
EXTENDED_MODULES = ["odoo.tools.float_utils"]
9+
10+
11+
class SixteenInFourteenMovedHook(object):
12+
def find_module(self, name, path=None):
13+
if name in MOVED_MODULES:
14+
return self
15+
16+
def load_module(self, name):
17+
assert name not in sys.modules
18+
odoo_module = sys.modules.get(name)
19+
if not odoo_module:
20+
odoo_module = importlib.import_module(MOVED_MODULES[name])
21+
sys.modules[name] = odoo_module
22+
return odoo_module
23+
24+
25+
class SixteenInFourteenExtendedHook(object):
26+
def find_module(self, name, path=None):
27+
if name in EXTENDED_MODULES:
28+
return self
29+
30+
def load_module(self, name):
31+
assert name not in sys.modules
32+
odoo_module = sys.modules.get(name)
33+
if not odoo_module:
34+
odoo_module = importlib.import_module(name)
35+
extended_module = importlib.import_module(
36+
f"odoo.addons.sixteen_in_fourteen.{name}"
37+
)
38+
odoo_module.__dict__.update(extended_module.__dict__)
39+
sys.modules[name] = odoo_module
40+
return odoo_module
41+
42+
43+
sys.meta_path.insert(0, SixteenInFourteenMovedHook())
44+
sys.meta_path.insert(0, SixteenInFourteenExtendedHook())
45+
46+
# Also patch already imported modules
47+
for mod in EXTENDED_MODULES:
48+
if mod in sys.modules:
49+
extended_module = importlib.import_module(
50+
f"odoo.addons.sixteen_in_fourteen.{mod}"
51+
)
52+
sys.modules[mod].__dict__.update(extended_module.__dict__)

sixteen_in_fourteen/__manifest__.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <florian.mounier@akretion.com>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
{
6+
"name": "Sixteen in Fourteen",
7+
"summary": "Layer of compat to run 16.0 modules in 14.0",
8+
"version": "14.0.1.0.0",
9+
"category": "Technical",
10+
"website": "https://github.com/akretion/ak-odoo-incubator",
11+
"author": " Akretion",
12+
"license": "AGPL-3",
13+
"application": False,
14+
"installable": True,
15+
"depends": [
16+
"base",
17+
],
18+
"data": [],
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def json_float_round(value, precision_digits, rounding_method="HALF-UP"):
2+
"""Not suitable for float calculations! Similar to float_repr except that it
3+
returns a float suitable for json dump
4+
5+
This may be necessary to produce "exact" representations of rounded float
6+
values during serialization, such as what is done by `json.dumps()`.
7+
Unfortunately `json.dumps` does not allow any form of custom float representation,
8+
nor any custom types, everything is serialized from the basic JSON types.
9+
10+
:param int precision_digits: number of fractional digits to round to.
11+
:param rounding_method: the rounding method used: 'HALF-UP', 'UP' or 'DOWN',
12+
the first one rounding up to the closest number with the rule that
13+
number>=0.5 is rounded up to 1, the second always rounding up and the
14+
latest one always rounding down.
15+
:return: a rounded float value that must not be used for calculations, but
16+
is ready to be serialized in JSON with minimal chances of
17+
representation errors.
18+
"""
19+
rounded_value = float_round(
20+
value, precision_digits=precision_digits, rounding_method=rounding_method
21+
)
22+
rounded_repr = float_repr(rounded_value, precision_digits=precision_digits)
23+
# As of Python 3.1, rounded_repr should be the shortest representation for our
24+
# rounded float, so we create a new float whose repr is expected
25+
# to be the same value, or a value that is semantically identical
26+
# and will be used in the json serialization.
27+
# e.g. if rounded_repr is '3.1750', the new float repr could be 3.175
28+
# but not 3.174999999999322452.
29+
# Cfr. bpo-1580: https://bugs.python.org/issue1580
30+
return float(rounded_repr)

0 commit comments

Comments
 (0)