From faefdbdcb5b65503218cea589c68e80bf7e21452 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Sun, 20 Oct 2024 10:48:59 +0200 Subject: [PATCH] [MIG] product_weighable_default_weight: Migration to 16.0 (from 12.0) --- product_weighable_default_weight/README.rst | 10 +- .../__manifest__.py | 5 +- .../demo/res_groups.xml | 13 --- product_weighable_default_weight/hooks.py | 109 +++++++++--------- .../models/product_product.py | 6 +- .../static/description/index.html | 6 +- .../tests/test_module.py | 15 ++- 7 files changed, 74 insertions(+), 90 deletions(-) delete mode 100644 product_weighable_default_weight/demo/res_groups.xml diff --git a/product_weighable_default_weight/README.rst b/product_weighable_default_weight/README.rst index fb707fe3a655..80c912da9760 100644 --- a/product_weighable_default_weight/README.rst +++ b/product_weighable_default_weight/README.rst @@ -17,13 +17,13 @@ Weighable Product - Default Weight :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github - :target: https://github.com/OCA/stock-logistics-workflow/tree/12.0/product_weighable_default_weight + :target: https://github.com/OCA/stock-logistics-workflow/tree/16.0/product_weighable_default_weight :alt: OCA/stock-logistics-workflow .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-12-0/stock-logistics-workflow-12-0-product_weighable_default_weight + :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-16-0/stock-logistics-workflow-16-0-product_weighable_default_weight :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png - :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=12.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=16.0 :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -52,7 +52,7 @@ 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 -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -90,6 +90,6 @@ Current `maintainer `__: |maintainer-legalsylvain| -This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. +This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_weighable_default_weight/__manifest__.py b/product_weighable_default_weight/__manifest__.py index 946e1728d51b..594830d10e54 100644 --- a/product_weighable_default_weight/__manifest__.py +++ b/product_weighable_default_weight/__manifest__.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { "name": "Weighable Product - Default Weight", - "version": "12.0.1.0.0", + "version": "16.0.1.0.0", "category": "Point Of Sale", "summary": "Set default weight on weighable product," " the weight is guessed from the ratio of the unit of mesure", @@ -11,8 +11,7 @@ "maintainers": ["legalsylvain"], "website": "https://github.com/OCA/stock-logistics-workflow", "license": "AGPL-3", - "depends": ["product"], - "demo": ["demo/res_groups.xml"], + "depends": ["stock", "product_uom_measure_type"], "post_init_hook": "post_init_hook", "installable": True, } diff --git a/product_weighable_default_weight/demo/res_groups.xml b/product_weighable_default_weight/demo/res_groups.xml deleted file mode 100644 index 71a1bd646d79..000000000000 --- a/product_weighable_default_weight/demo/res_groups.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - diff --git a/product_weighable_default_weight/hooks.py b/product_weighable_default_weight/hooks.py index f62ab2425fa8..1e4af6b41dc0 100644 --- a/product_weighable_default_weight/hooks.py +++ b/product_weighable_default_weight/hooks.py @@ -7,66 +7,61 @@ def post_init_hook(cr, registry): - with api.Environment.manage(): - env = api.Environment(cr, SUPERUSER_ID, {}) - _logger.info( - "[default UoM] Fast initializing weight fields for products" ", if not set" + env = api.Environment(cr, SUPERUSER_ID, {}) + _logger.info( + "[default UoM] Fast initializing weight fields for products" ", if not set" + ) + env.cr.execute( + """ + SELECT value + FROM ir_config_parameter + WHERE key='product.weight_in_lbs';""" + ) + res = env.cr.fetchone() + if res and res[0] == 1: + # LBs is the global default UoM + default_uom = env.ref("uom.product_uom_lb") + else: + # Kg is the global default UoM + default_uom = env.ref("uom.product_uom_kgm") + if default_uom: + env.cr.execute( + """ + UPDATE product_template + SET weight = 1.0 + WHERE uom_id = %s + AND (weight = 0.0 or weight is null);""", + (default_uom.id,), ) env.cr.execute( """ - SELECT value - FROM ir_config_parameter - WHERE key='product.weight_in_lbs';""" + UPDATE product_product pp + SET weight = 1.0 + FROM product_template pt + WHERE pt.id = pp.product_tmpl_id + AND pt.uom_id = %s + AND ( + pp.weight = 0.0 or pp.weight is null);""", + (default_uom.id,), ) - res = env.cr.fetchone() - if res and res[0] == 1: - # LBs is the global default UoM - default_uom = env.ref("uom.product_uom_lb") - else: - # Kg is the global default UoM - default_uom = env.ref("uom.product_uom_kgm") - if default_uom: - env.cr.execute( - """ - UPDATE product_template - SET weight = 1.0 - WHERE uom_id = %s - AND (weight = 0.0 or weight is null);""", - (default_uom.id,), - ) - env.cr.execute( - """ - UPDATE product_product pp - SET weight = 1.0 - FROM product_template pt - WHERE pt.id = pp.product_tmpl_id - AND pt.uom_id = %s - AND ( - pp.weight = 0.0 or pp.weight is null);""", - (default_uom.id,), - ) - _logger.info( - "[Other UoM] Initializing weight fields for weighable products," - " if not set, via ORM" - ) - groups = env["product.product"].read_group( - [ - ("uom_id.measure_type", "=", "weight"), - "|", - ("weight", "=", 0.0), - ("weight", "=", False), - ], - fields=["weight", "uom_id"], - groupby="uom_id", - ) + _logger.info( + "[Other UoM] Initializing weight fields for weighable products," + " if not set, via ORM" + ) + groups = env["product.product"].read_group( + [ + ("uom_id.measure_type", "=", "weight"), + "|", + ("weight", "=", 0.0), + ("weight", "=", False), + ], + fields=["weight", "uom_id"], + groupby="uom_id", + ) - for group in groups: - products = env["product.product"].search(group["__domain"]) - new_weight = env["product.template"]._get_weight_from_uom_id( - group["uom_id"][0] - ) - _logger.info( - f"Writing new weight {new_weight} for {len(products)} products." - ) - products.write({"weight": new_weight}) + for group in groups: + products = env["product.product"].search(group["__domain"]) + new_weight = env["product.template"]._get_weight_from_uom_id(group["uom_id"][0]) + _logger.info(f"Writing new weight {new_weight} for {len(products)} products.") + products.write({"weight": new_weight}) diff --git a/product_weighable_default_weight/models/product_product.py b/product_weighable_default_weight/models/product_product.py index 29d594f523af..cedeb6d2ef20 100644 --- a/product_weighable_default_weight/models/product_product.py +++ b/product_weighable_default_weight/models/product_product.py @@ -8,9 +8,9 @@ class ProductProduct(models.Model): _inherit = "product.product" - @api.onchange("uom_id", "uom_po_id") - def _onchange_uom(self): - res = super()._onchange_uom() + @api.onchange("uom_id") + def _onchange_uom_id(self): + res = super()._onchange_uom_id() new_weight = self.product_tmpl_id._get_weight_from_uom(self.uom_id) if new_weight: self.weight = new_weight diff --git a/product_weighable_default_weight/static/description/index.html b/product_weighable_default_weight/static/description/index.html index 4e5541e46361..32f0d1b70b9a 100644 --- a/product_weighable_default_weight/static/description/index.html +++ b/product_weighable_default_weight/static/description/index.html @@ -369,7 +369,7 @@

Weighable Product - Default Weight

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! source digest: sha256:9309e682dc20a992ece75bf4d1fb418c23326477b99f8dbc03ff6133089b567f !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

Beta License: AGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

+

Beta License: AGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

This module extends the functionality of product module to set default value on weight field for weighable products.

Once installed, when a user create a new weighable product @@ -398,7 +398,7 @@

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 -feedback.

+feedback.

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

@@ -426,7 +426,7 @@

Maintainers

promote its widespread use.

Current maintainer:

legalsylvain

-

This module is part of the OCA/stock-logistics-workflow project on GitHub.

+

This module is part of the OCA/stock-logistics-workflow project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

diff --git a/product_weighable_default_weight/tests/test_module.py b/product_weighable_default_weight/tests/test_module.py index 37118afe7916..29021b8afde8 100644 --- a/product_weighable_default_weight/tests/test_module.py +++ b/product_weighable_default_weight/tests/test_module.py @@ -6,12 +6,15 @@ class TestModule(TransactionCase): - def setUp(self): - super().setUp() - self.ProductProduct = self.env["product.product"] - self.ProductTemplate = self.env["product.template"] - self.uom_kg = self.browse_ref("uom.product_uom_kgm") - self.uom_ton = self.browse_ref("uom.product_uom_ton") + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.ProductProduct = cls.env["product.product"] + cls.ProductTemplate = cls.env["product.template"] + cls.uom_kg = cls.env.ref("uom.product_uom_kgm") + cls.uom_ton = cls.env.ref("uom.product_uom_ton") + cls.env.user.groups_id += cls.env.ref("uom.group_uom") + def _test_create_write(self, model): item = model.create(