Skip to content

Commit

Permalink
[MIG] product_weighable_default_weight: Migration to 16.0 (from 12.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
legalsylvain committed Nov 5, 2024
1 parent 662b69c commit faefdbd
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 90 deletions.
10 changes: 5 additions & 5 deletions product_weighable_default_weight/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -52,7 +52,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-workflow/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 <https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20product_weighable_default_weight%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20product_weighable_default_weight%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Expand Down Expand Up @@ -90,6 +90,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-legalsylvain|

This module is part of the `OCA/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/12.0/product_weighable_default_weight>`_ project on GitHub.
This module is part of the `OCA/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/16.0/product_weighable_default_weight>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
5 changes: 2 additions & 3 deletions product_weighable_default_weight/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
# 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",
"author": "GRAP, Odoo Community Association (OCA)",
"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,
}
13 changes: 0 additions & 13 deletions product_weighable_default_weight/demo/res_groups.xml

This file was deleted.

109 changes: 52 additions & 57 deletions product_weighable_default_weight/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
6 changes: 3 additions & 3 deletions product_weighable_default_weight/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ <h1 class="title">Weighable Product - Default Weight</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:9309e682dc20a992ece75bf4d1fb418c23326477b99f8dbc03ff6133089b567f
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/stock-logistics-workflow/tree/12.0/product_weighable_default_weight"><img alt="OCA/stock-logistics-workflow" src="https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/stock-logistics-workflow-12-0/stock-logistics-workflow-12-0-product_weighable_default_weight"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&amp;target_branch=12.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/stock-logistics-workflow/tree/16.0/product_weighable_default_weight"><img alt="OCA/stock-logistics-workflow" src="https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/stock-logistics-workflow-16-0/stock-logistics-workflow-16-0-product_weighable_default_weight"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module extends the functionality of product module to set
default value on weight field for weighable products.</p>
<p>Once installed, when a user create a new weighable product
Expand Down Expand Up @@ -398,7 +398,7 @@ <h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20product_weighable_default_weight%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20product_weighable_default_weight%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -426,7 +426,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/legalsylvain"><img alt="legalsylvain" src="https://github.com/legalsylvain.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/tree/12.0/product_weighable_default_weight">OCA/stock-logistics-workflow</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/stock-logistics-workflow/tree/16.0/product_weighable_default_weight">OCA/stock-logistics-workflow</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
15 changes: 9 additions & 6 deletions product_weighable_default_weight/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit faefdbd

Please sign in to comment.