From fb8b0ffd168bd8c3578ce4b6ca693ca5a1aab4c9 Mon Sep 17 00:00:00 2001 From: Souheil Bejaoui Date: Wed, 25 Mar 2026 10:45:03 +0100 Subject: [PATCH] [IMP] uom_unece: cache UNECE code mapping add an ormcache based helper to map UNECE codes to UoM --- uom_unece/README.rst | 7 +++- uom_unece/models/uom_uom.py | 31 ++++++++++++++++- uom_unece/readme/CONTRIBUTORS.rst | 1 + uom_unece/static/description/index.html | 38 +++++++++++++-------- uom_unece/tests/__init__.py | 1 + uom_unece/tests/test_uom_unece.py | 44 +++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 uom_unece/tests/__init__.py create mode 100644 uom_unece/tests/test_uom_unece.py diff --git a/uom_unece/README.rst b/uom_unece/README.rst index d1858f80f..391cf6ec1 100644 --- a/uom_unece/README.rst +++ b/uom_unece/README.rst @@ -1,3 +1,7 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + ================= Product UoM UNECE ================= @@ -13,7 +17,7 @@ Product UoM UNECE .. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png :target: https://odoo-community.org/page/development-status :alt: Production/Stable -.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcommunity--data--files-lightgray.png?logo=github @@ -78,6 +82,7 @@ Contributors * Andrea Stirpe * Levent Karakaş * Pedro M. Baeza +* Souheil Bejaoui Maintainers ~~~~~~~~~~~ diff --git a/uom_unece/models/uom_uom.py b/uom_unece/models/uom_uom.py index ab0e0419c..2d1265d85 100644 --- a/uom_unece/models/uom_uom.py +++ b/uom_unece/models/uom_uom.py @@ -2,7 +2,8 @@ # @author: Alexis de Lattre # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import api, fields, models +from odoo.tools import ormcache class UomUom(models.Model): @@ -13,3 +14,31 @@ class UomUom(models.Model): help="Standard nomenclature of the United Nations Economic " "Commission for Europe (UNECE).", ) + + @api.model + @ormcache() + def _get_uom_id_by_unece_code(self): + uoms = self.search([("unece_code", "!=", False)]) + return {u.unece_code: u.id for u in uoms} + + def get_uom_id_by_unece_code(self, unece_code): + uom_ids_by_code = self._get_uom_id_by_unece_code() + return uom_ids_by_code.get(unece_code) + + @api.model_create_multi + def create(self, vals_list): + records = super().create(vals_list) + if records.filtered("unece_code"): + self._get_uom_id_by_unece_code.clear_cache(self) + return records + + def write(self, vals): + res = super().write(vals) + if "unece_code" in vals: + self._get_uom_id_by_unece_code.clear_cache(self) + return res + + def unlink(self): + res = super().unlink() + self._get_uom_id_by_unece_code.clear_cache(self) + return res diff --git a/uom_unece/readme/CONTRIBUTORS.rst b/uom_unece/readme/CONTRIBUTORS.rst index e0299be45..68618c123 100644 --- a/uom_unece/readme/CONTRIBUTORS.rst +++ b/uom_unece/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * Andrea Stirpe * Levent Karakaş * Pedro M. Baeza +* Souheil Bejaoui diff --git a/uom_unece/static/description/index.html b/uom_unece/static/description/index.html index ce263609a..dde3e5201 100644 --- a/uom_unece/static/description/index.html +++ b/uom_unece/static/description/index.html @@ -3,15 +3,16 @@ -Product UoM UNECE +README.rst -
-

Product UoM UNECE

+
+ + +Odoo Community Association + +
+

Product UoM UNECE

-

Production/Stable License: AGPL-3 OCA/community-data-files Translate me on Weblate Try me on Runboat

+

Production/Stable License: AGPL-3 OCA/community-data-files Translate me on Weblate Try me on Runboat

This module adds a field UNECE Code on units of measure to allow the use of a standard written by the United Nations Economic Commission for Europe (which @@ -397,11 +403,11 @@

Product UoM UNECE

-

Configuration

+

Configuration

This module automatically adds the UNECE Code on the existing units of measure.

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -409,26 +415,29 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • Akretion
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

-Odoo Community Association + +Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

@@ -439,5 +448,6 @@

Maintainers

+
diff --git a/uom_unece/tests/__init__.py b/uom_unece/tests/__init__.py new file mode 100644 index 000000000..fe1997c6f --- /dev/null +++ b/uom_unece/tests/__init__.py @@ -0,0 +1 @@ +from . import test_uom_unece diff --git a/uom_unece/tests/test_uom_unece.py b/uom_unece/tests/test_uom_unece.py new file mode 100644 index 000000000..e8146b459 --- /dev/null +++ b/uom_unece/tests/test_uom_unece.py @@ -0,0 +1,44 @@ +# Copyright 2026 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestUomUnece(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.uom_model = cls.env["uom.uom"] + cls.uom_unit = cls.env.ref("uom.product_uom_unit") + cls.uom_dozen = cls.env.ref("uom.product_uom_dozen") + cls.uom_hour = cls.env.ref("uom.product_uom_hour") + + def test_get_uom_id_by_unece_code(self): + self.assertEqual( + self.uom_model.get_uom_id_by_unece_code("C62"), self.uom_unit.id + ) + self.assertEqual( + self.uom_model.get_uom_id_by_unece_code("DPC"), self.uom_dozen.id + ) + self.assertEqual( + self.uom_model.get_uom_id_by_unece_code("HUR"), self.uom_hour.id + ) + + def test_get_uom_id_by_unece_code_unknown(self): + self.assertFalse(self.uom_model.get_uom_id_by_unece_code("UNKNOWN")) + + def test_get_uom_id_by_unece_code_cache_invalidation_on_write(self): + self.assertEqual( + self.uom_model.get_uom_id_by_unece_code("C62"), self.uom_unit.id + ) + self.uom_unit.write({"unece_code": "C62_NEW"}) + self.assertFalse(self.uom_model.get_uom_id_by_unece_code("C62")) + self.assertEqual( + self.uom_model.get_uom_id_by_unece_code("C62_NEW"), self.uom_unit.id + ) + + def test_get_uom_id_by_unece_code_cache_invalidation_on_unlink(self): + uom = self.uom_hour.copy({"name": "Test Hour Copy", "unece_code": "ZZZ"}) + self.assertEqual(self.uom_model.get_uom_id_by_unece_code("ZZZ"), uom.id) + uom.unlink() + self.assertFalse(self.uom_model.get_uom_id_by_unece_code("ZZZ"))