From 860976411ff487d1f694b2fada0e9aef91a1da34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ana=C3=AFs?= Date: Wed, 17 Apr 2024 09:26:43 +0200 Subject: [PATCH] [MIG] product_restricted_type: Migration to 17.0 --- product_restricted_type/README.rst | 13 +++- product_restricted_type/__manifest__.py | 6 +- product_restricted_type/models/__init__.py | 2 +- .../{product.py => product_category.py} | 2 +- .../models/product_template.py | 26 ++++---- product_restricted_type/readme/DESCRIPTION.md | 4 +- product_restricted_type/readme/USAGE.md | 4 ++ .../static/description/index.html | 13 +++- .../tests/test_product_restricted_type.py | 65 ++++++++++++++++++- ...t_views.xml => product_category_views.xml} | 2 +- .../views/product_template_views.xml | 18 +++++ 11 files changed, 126 insertions(+), 29 deletions(-) rename product_restricted_type/models/{product.py => product_category.py} (97%) rename product_restricted_type/views/{product_views.xml => product_category_views.xml} (84%) create mode 100644 product_restricted_type/views/product_template_views.xml diff --git a/product_restricted_type/README.rst b/product_restricted_type/README.rst index 08026175dd44..234a694069d1 100644 --- a/product_restricted_type/README.rst +++ b/product_restricted_type/README.rst @@ -29,10 +29,10 @@ Product Restricted Type |badge1| |badge2| |badge3| |badge4| |badge5| This module adds 'restricted_product_type' to product_category model. -With this optional selection field 'restricted_product_type' should have -the same values as in the product.template, 'type' field. +With this optional selection field, 'restricted_product_type' should +have the same values as in the product.template, 'type' field. -A constrain is also established in the product so that when the 'type' +A constraint is also established in the product so that when the 'type' defined in the product does not match with the 'restricted_product_type' defined in the product category it raises a Validation Error. @@ -49,7 +49,14 @@ value. Usage ===== +To use this module, you need to go to Inventory -> Configuration -> +Product Categories and set a Restricted Product Type. +When a Restricted Product Type is assigned to a Product Category, if we +then set a Product Type on a Product Template, the list of available +Product Categories in the dropdown will be filtered with only the ones +that have the same Restricted Product Type as the one set on the Product +Template or have no Restricted Product Type set. Bug Tracker =========== diff --git a/product_restricted_type/__manifest__.py b/product_restricted_type/__manifest__.py index 942536c50bc0..29984a61b12d 100644 --- a/product_restricted_type/__manifest__.py +++ b/product_restricted_type/__manifest__.py @@ -1,16 +1,16 @@ -# Copyright 2018 ForgeFlow S.L. +# Copyright 2018-2024 ForgeFlow S.L. # (http://www.forgeflow.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { "name": "Product Restricted Type", - "version": "15.0.1.0.0", + "version": "17.0.1.0.0", "author": "ForgeFlow," "Odoo Community Association (OCA)", "website": "https://github.com/OCA/product-attribute", "license": "AGPL-3", "category": "Product", "depends": ["product"], - "data": ["views/product_views.xml"], + "data": ["views/product_category_views.xml", "views/product_template_views.xml"], "auto_install": False, "installable": True, } diff --git a/product_restricted_type/models/__init__.py b/product_restricted_type/models/__init__.py index 8d6754c5d1f7..15c4d916dab1 100644 --- a/product_restricted_type/models/__init__.py +++ b/product_restricted_type/models/__init__.py @@ -1,4 +1,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import product_template -from . import product +from . import product_category diff --git a/product_restricted_type/models/product.py b/product_restricted_type/models/product_category.py similarity index 97% rename from product_restricted_type/models/product.py rename to product_restricted_type/models/product_category.py index f429b51977ba..033bea6a958f 100644 --- a/product_restricted_type/models/product.py +++ b/product_restricted_type/models/product_category.py @@ -1,4 +1,4 @@ -# Copyright 2018 ForgeFlow S.L. +# Copyright 2018-2024 ForgeFlow S.L. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import _, api, fields, models from odoo.exceptions import ValidationError diff --git a/product_restricted_type/models/product_template.py b/product_restricted_type/models/product_template.py index 6829138ea9e1..07006b2ca558 100644 --- a/product_restricted_type/models/product_template.py +++ b/product_restricted_type/models/product_template.py @@ -1,27 +1,29 @@ -# Copyright 2018 ForgeFlow S.L. +# Copyright 2018-2024 ForgeFlow S.L. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import _, api, models +from odoo import _, api, fields, models from odoo.exceptions import ValidationError class ProductTemplate(models.Model): _inherit = "product.template" + allowed_categ_ids = fields.Many2many( + "product.category", + compute="_compute_allowed_categ_ids", + string="Allowed Categories", + ) + @api.onchange("categ_id") def _onchange_categ_id(self): if self.categ_id and self.categ_id.restricted_product_type: self.type = self.categ_id.restricted_product_type - @api.onchange("type") - def _onchange_type(self): - if self.type: - return { - "domain": { - "categ_id": [("restricted_product_type", "in", [self.type, False])] - } - } - else: - return {"domain": {"categ_id": []}} + @api.depends("type") + def _compute_allowed_categ_ids(self): + for record in self: + record.allowed_categ_ids = self.env["product.category"].search( + [("restricted_product_type", "in", [record.type, False])] + ) @api.constrains("type") def _check_product_type(self): diff --git a/product_restricted_type/readme/DESCRIPTION.md b/product_restricted_type/readme/DESCRIPTION.md index ae1181efd519..363b8e588514 100644 --- a/product_restricted_type/readme/DESCRIPTION.md +++ b/product_restricted_type/readme/DESCRIPTION.md @@ -1,8 +1,8 @@ This module adds 'restricted_product_type' to product_category model. -With this optional selection field 'restricted_product_type' should have +With this optional selection field, 'restricted_product_type' should have the same values as in the product.template, 'type' field. -A constrain is also established in the product so that when the 'type' +A constraint is also established in the product so that when the 'type' defined in the product does not match with the 'restricted_product_type' defined in the product category it raises a Validation Error. diff --git a/product_restricted_type/readme/USAGE.md b/product_restricted_type/readme/USAGE.md index 8b137891791f..64e0d7b991cd 100644 --- a/product_restricted_type/readme/USAGE.md +++ b/product_restricted_type/readme/USAGE.md @@ -1 +1,5 @@ +To use this module, you need to go to Inventory -> Configuration -> Product Categories and set a Restricted Product Type. +When a Restricted Product Type is assigned to a Product Category, if we then set a Product Type on a Product Template, +the list of available Product Categories in the dropdown will be filtered with only the ones that have the same +Restricted Product Type as the one set on the Product Template or have no Restricted Product Type set. diff --git a/product_restricted_type/static/description/index.html b/product_restricted_type/static/description/index.html index 6d2696b20824..4e9a45581bb3 100644 --- a/product_restricted_type/static/description/index.html +++ b/product_restricted_type/static/description/index.html @@ -371,9 +371,9 @@

Product Restricted Type

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: AGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runboat

This module adds ‘restricted_product_type’ to product_category model. -With this optional selection field ‘restricted_product_type’ should have -the same values as in the product.template, ‘type’ field.

-

A constrain is also established in the product so that when the ‘type’ +With this optional selection field, ‘restricted_product_type’ should +have the same values as in the product.template, ‘type’ field.

+

A constraint is also established in the product so that when the ‘type’ defined in the product does not match with the ‘restricted_product_type’ defined in the product category it raises a Validation Error.

Also, in the product category one would not be able to change the field @@ -395,6 +395,13 @@

Product Restricted Type

Usage

+

To use this module, you need to go to Inventory -> Configuration -> +Product Categories and set a Restricted Product Type.

+

When a Restricted Product Type is assigned to a Product Category, if we +then set a Product Type on a Product Template, the list of available +Product Categories in the dropdown will be filtered with only the ones +that have the same Restricted Product Type as the one set on the Product +Template or have no Restricted Product Type set.

Bug Tracker

diff --git a/product_restricted_type/tests/test_product_restricted_type.py b/product_restricted_type/tests/test_product_restricted_type.py index 447d1f4561b9..583bb5b7e42d 100644 --- a/product_restricted_type/tests/test_product_restricted_type.py +++ b/product_restricted_type/tests/test_product_restricted_type.py @@ -7,7 +7,7 @@ class TestProductRestrictedType(TransactionCase): def setUp(self): - super(TestProductRestrictedType, self).setUp() + super().setUp() self.product_product = self.env["product.product"] self.product_category = self.env["product.category"] @@ -35,7 +35,7 @@ def setUp(self): } ) - def test_product_different_type(self): + def test_01_product_different_type(self): """User tries to change product type to a different type than company restricted product type""" with self.assertRaises(ValidationError): @@ -49,8 +49,67 @@ def test_product_different_type(self): } ) - def test_category_different_type(self): + def test_02_category_different_type(self): """User tries to change category restricted type but there are products with already defined (different) type""" with self.assertRaises(ValidationError): self.categ_test.write({"restricted_product_type": self.product_types[1][0]}) + + def test_03_type_equals_restricted_type(self): + """Ensure the product template's type is updated to match the category's restricted product type.""" + product_template_test = self.env["product.template"].create( + { + "name": "Test Product Template", + "type": self.product_types[0][0], + "categ_id": self.categ_test.id, + } + ) + + self.assertEqual( + product_template_test.type, + self.categ_test.restricted_product_type, + "The product template's type should have been updated to the restricted product type of the category.", + ) + + def test_04_domain_changes_with_type(self): + """Check that only allowed categories can be selected based on the product type.""" + + categ_consummable = self.product_category.create( + { + "name": "Test Category 1", + "restricted_product_type": self.product_types[0][0], + } + ) + categ_service = self.product_category.create( + { + "name": "Test Category 1", + "restricted_product_type": self.product_types[1][0], + } + ) + categ_no_restriction = self.product_category.create( + {"name": "General Category", "restricted_product_type": False} + ) + + product_template_test2 = self.env["product.template"].create( + { + "name": "Test Product Template 1", + "type": self.product_types[1][0], + "categ_id": categ_service.id, + } + ) + + self.assertIn( + categ_service, + product_template_test2.allowed_categ_ids, + "Service category should be included for service type", + ) + self.assertNotIn( + categ_consummable, + product_template_test2.allowed_categ_ids, + "Consummable category should not be included for service type", + ) + self.assertIn( + categ_no_restriction, + product_template_test2.allowed_categ_ids, + "General category should always be included", + ) diff --git a/product_restricted_type/views/product_views.xml b/product_restricted_type/views/product_category_views.xml similarity index 84% rename from product_restricted_type/views/product_views.xml rename to product_restricted_type/views/product_category_views.xml index cfe60ca375a5..7275ce4b192c 100644 --- a/product_restricted_type/views/product_views.xml +++ b/product_restricted_type/views/product_category_views.xml @@ -1,7 +1,7 @@ - product.category.form + product.category.form - product_restricted_type product.category diff --git a/product_restricted_type/views/product_template_views.xml b/product_restricted_type/views/product_template_views.xml new file mode 100644 index 000000000000..a74cd998cb97 --- /dev/null +++ b/product_restricted_type/views/product_template_views.xml @@ -0,0 +1,18 @@ + + + + product.template.product.form - product_restricted_type + product.template + + + + + + + [('id', 'in', allowed_categ_ids)] + + + +