Skip to content

Commit

Permalink
[IMP] product_weighable_default_weight: pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
legalsylvain committed Oct 20, 2024
1 parent a5f0f86 commit 662b69c
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion product_weighable_default_weight/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import models
from .hooks import post_init_hook
from .hooks import post_init_hook
4 changes: 2 additions & 2 deletions product_weighable_default_weight/demo/res_groups.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2023 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
Expand All @@ -7,7 +7,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<odoo>

<record id="uom.group_uom" model="res.groups">
<field name="users" eval="[(4, ref('base.user_admin'))]"/>
<field name="users" eval="[(4, ref('base.user_admin'))]" />
</record>

</odoo>
33 changes: 22 additions & 11 deletions product_weighable_default_weight/hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from odoo import api, SUPERUSER_ID

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)

Expand All @@ -9,13 +10,14 @@ 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"
"[default UoM] Fast initializing weight fields for products" ", if not set"
)
env.cr.execute("""
env.cr.execute(
"""
SELECT value
FROM ir_config_parameter
WHERE key='product.weight_in_lbs';""")
WHERE key='product.weight_in_lbs';"""
)
res = env.cr.fetchone()
if res and res[0] == 1:
# LBs is the global default UoM
Expand All @@ -24,19 +26,25 @@ def post_init_hook(cr, registry):
# Kg is the global default UoM
default_uom = env.ref("uom.product_uom_kgm")
if default_uom:
env.cr.execute("""
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("""
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,))
pp.weight = 0.0 or pp.weight is null);""",
(default_uom.id,),
)

_logger.info(
"[Other UoM] Initializing weight fields for weighable products,"
Expand All @@ -45,10 +53,13 @@ def post_init_hook(cr, registry):
groups = env["product.product"].read_group(
[
("uom_id.measure_type", "=", "weight"),
'|', ("weight", "=", 0.0), ('weight', '=', False)
"|",
("weight", "=", 0.0),
("weight", "=", False),
],
fields=["weight", "uom_id"],
groupby="uom_id")
groupby="uom_id",
)

for group in groups:
products = env["product.product"].search(group["__domain"])
Expand Down
2 changes: 1 addition & 1 deletion product_weighable_default_weight/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models, api
from odoo import api, models


class ProductProduct(models.Model):
Expand Down
7 changes: 3 additions & 4 deletions product_weighable_default_weight/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models, api
from odoo import api, models


class ProductTemplate(models.Model):
_inherit = "product.template"

@api.onchange('uom_id')
@api.onchange("uom_id")
def _onchange_uom_id(self):
res = super()._onchange_uom_id()
new_weight = self._get_weight_from_uom(self.uom_id)
Expand Down Expand Up @@ -41,6 +41,5 @@ def _get_weight_from_uom(self, uom):
if not uom.measure_type == "weight":
return
return uom._compute_quantity(
1,
self._get_weight_uom_id_from_ir_config_parameter()
1, self._get_weight_uom_id_from_ir_config_parameter()
)
2 changes: 1 addition & 1 deletion product_weighable_default_weight/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import test_module
from . import test_module
16 changes: 8 additions & 8 deletions product_weighable_default_weight/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tests.common import TransactionCase
from odoo.tests.common import Form
from odoo.tests.common import Form, TransactionCase


class TestModule(TransactionCase):

def setUp(self):
super().setUp()
self.ProductProduct = self.env["product.product"]
Expand All @@ -16,11 +14,13 @@ def setUp(self):
self.uom_ton = self.browse_ref("uom.product_uom_ton")

def _test_create_write(self, model):
item = model.create({
"name": "Demo Product",
"uom_id": self.uom_kg.id,
"uom_po_id": self.uom_kg.id,
})
item = model.create(
{
"name": "Demo Product",
"uom_id": self.uom_kg.id,
"uom_po_id": self.uom_kg.id,
}
)
self.assertEqual(item.weight, 1.0)
item.uom_id = self.uom_ton
self.assertEqual(item.weight, 1000.0)
Expand Down
6 changes: 6 additions & 0 deletions setup/product_weighable_default_weight/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 662b69c

Please sign in to comment.