Skip to content

Commit

Permalink
[MIG] product_restricted_type: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaisLForgeFlow committed Apr 22, 2024
1 parent 21ef635 commit 8609764
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 29 deletions.
13 changes: 10 additions & 3 deletions product_restricted_type/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
===========
Expand Down
6 changes: 3 additions & 3 deletions product_restricted_type/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 1 addition & 1 deletion product_restricted_type/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 14 additions & 12 deletions product_restricted_type/models/product_template.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
4 changes: 2 additions & 2 deletions product_restricted_type/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
4 changes: 4 additions & 0 deletions product_restricted_type/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 10 additions & 3 deletions product_restricted_type/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ <h1 class="title">Product Restricted Type</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/product-attribute/tree/17.0/product_restricted_type"><img alt="OCA/product-attribute" src="https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/product-attribute-17-0/product-attribute-17-0-product_restricted_type"><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/product-attribute&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>Also, in the product category one would not be able to change the field
Expand All @@ -395,6 +395,13 @@ <h1 class="title">Product Restricted Type</h1>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>To use this module, you need to go to Inventory -&gt; Configuration -&gt;
Product Categories and set a Restricted Product Type.</p>
<p>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.</p>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
Expand Down
65 changes: 62 additions & 3 deletions product_restricted_type/tests/test_product_restricted_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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):
Expand All @@ -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",
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="product_category_form_view" model="ir.ui.view">
<field name="name">product.category.form</field>
<field name="name">product.category.form - product_restricted_type</field>
<field name="model">product.category</field>
<field name="inherit_id" ref="product.product_category_form_view" />
<field name="arch" type="xml">
Expand Down
18 changes: 18 additions & 0 deletions product_restricted_type/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="product_template_only_form_view" model="ir.ui.view">
<field
name="name"
>product.template.product.form - product_restricted_type</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view" />
<field name="arch" type="xml">
<field name='categ_id' position="before">
<field name="allowed_categ_ids" invisible="1" />
</field>
<field name='categ_id' position="attributes">
<attribute name="domain">[('id', 'in', allowed_categ_ids)]</attribute>
</field>
</field>
</record>
</odoo>

0 comments on commit 8609764

Please sign in to comment.