From 36a9f3c75dad07a4708370c2f44eaecbe93e91e1 Mon Sep 17 00:00:00 2001 From: yostashiro Date: Sat, 7 Mar 2026 10:32:44 +0000 Subject: [PATCH] [ADD] product_attribute_value_name_search --- .../README.rst | 90 ++++ .../__init__.py | 1 + .../__manifest__.py | 14 + .../models/__init__.py | 1 + .../models/product_attribute_value.py | 27 ++ .../pyproject.toml | 3 + .../readme/DESCRIPTION.md | 12 + .../static/description/index.html | 424 ++++++++++++++++++ .../tests/__init__.py | 1 + ...est_product_attribute_value_name_search.py | 97 ++++ 10 files changed, 670 insertions(+) create mode 100644 product_attribute_value_name_search/README.rst create mode 100644 product_attribute_value_name_search/__init__.py create mode 100644 product_attribute_value_name_search/__manifest__.py create mode 100644 product_attribute_value_name_search/models/__init__.py create mode 100644 product_attribute_value_name_search/models/product_attribute_value.py create mode 100644 product_attribute_value_name_search/pyproject.toml create mode 100644 product_attribute_value_name_search/readme/DESCRIPTION.md create mode 100644 product_attribute_value_name_search/static/description/index.html create mode 100644 product_attribute_value_name_search/tests/__init__.py create mode 100644 product_attribute_value_name_search/tests/test_product_attribute_value_name_search.py diff --git a/product_attribute_value_name_search/README.rst b/product_attribute_value_name_search/README.rst new file mode 100644 index 00000000000..1c6fee61675 --- /dev/null +++ b/product_attribute_value_name_search/README.rst @@ -0,0 +1,90 @@ +=================================== +Product Attribute Value Name Search +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:b0f80a0321559296df8bf766ddda6ab3aa8f884dad007e80da804fd6024fa40b + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github + :target: https://github.com/OCA/product-attribute/tree/18.0/product_attribute_value_name_search + :alt: OCA/product-attribute +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/product-attribute-18-0/product-attribute-18-0-product_attribute_value_name_search + :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/product-attribute&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module enhances the name search of product template attribute +values to support the composite "Attribute: Value" display name format. + +In standard Odoo, ``product.template.attribute.value`` records display +as "Attribute: Value" (e.g., "Color: Red"), but the search only looks at +the ``name`` field. This means attribute values with the same name under +different attributes (e.g., "Red" under both "Color" and "Size") cannot +be distinguished during import or name-based lookups. + +With this module installed, searching for "Color: Red" will match only +the "Red" value under the "Color" attribute, enabling unambiguous +matching by display name. + +**Table of contents** + +.. contents:: + :local: + +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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Quartile + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px + :target: https://github.com/yostashiro + :alt: yostashiro + +Current `maintainer `__: + +|maintainer-yostashiro| + +This module is part of the `OCA/product-attribute `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_attribute_value_name_search/__init__.py b/product_attribute_value_name_search/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/product_attribute_value_name_search/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_attribute_value_name_search/__manifest__.py b/product_attribute_value_name_search/__manifest__.py new file mode 100644 index 00000000000..f6b3bdf19d8 --- /dev/null +++ b/product_attribute_value_name_search/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Product Attribute Value Name Search", + "summary": "Search for product attribute values with display name", + "version": "18.0.1.0.0", + "category": "Product", + "license": "AGPL-3", + "website": "https://github.com/OCA/product-attribute", + "author": "Quartile, Odoo Community Association (OCA)", + "depends": ["product"], + "maintainers": ["yostashiro"], + "installable": True, +} diff --git a/product_attribute_value_name_search/models/__init__.py b/product_attribute_value_name_search/models/__init__.py new file mode 100644 index 00000000000..967ba8ab066 --- /dev/null +++ b/product_attribute_value_name_search/models/__init__.py @@ -0,0 +1 @@ +from . import product_attribute_value diff --git a/product_attribute_value_name_search/models/product_attribute_value.py b/product_attribute_value_name_search/models/product_attribute_value.py new file mode 100644 index 00000000000..0f1f650f1bb --- /dev/null +++ b/product_attribute_value_name_search/models/product_attribute_value.py @@ -0,0 +1,27 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models +from odoo.osv.expression import NEGATIVE_TERM_OPERATORS + +SEPARATOR = ": " + + +class ProductTemplateAttributeValue(models.Model): + _inherit = "product.template.attribute.value" + + @api.model + def _search_display_name(self, operator, value): + if value and SEPARATOR in str(value): + attr_name, val_name = str(value).split(SEPARATOR, 1) + if operator in NEGATIVE_TERM_OPERATORS: + return [ + "|", + ("attribute_id.name", operator, attr_name), + ("name", operator, val_name), + ] + return [ + ("attribute_id.name", operator, attr_name), + ("name", operator, val_name), + ] + return super()._search_display_name(operator, value) diff --git a/product_attribute_value_name_search/pyproject.toml b/product_attribute_value_name_search/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/product_attribute_value_name_search/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_attribute_value_name_search/readme/DESCRIPTION.md b/product_attribute_value_name_search/readme/DESCRIPTION.md new file mode 100644 index 00000000000..1dbfd0527a0 --- /dev/null +++ b/product_attribute_value_name_search/readme/DESCRIPTION.md @@ -0,0 +1,12 @@ +This module enhances the name search of product template attribute values to +support the composite "Attribute: Value" display name format. + +In standard Odoo, ``product.template.attribute.value`` records display as +"Attribute: Value" (e.g., "Color: Red"), but the search only looks at the +``name`` field. This means attribute values with the same name under different +attributes (e.g., "Red" under both "Color" and "Size") cannot be distinguished +during import or name-based lookups. + +With this module installed, searching for "Color: Red" will match only the +"Red" value under the "Color" attribute, enabling unambiguous matching by +display name. diff --git a/product_attribute_value_name_search/static/description/index.html b/product_attribute_value_name_search/static/description/index.html new file mode 100644 index 00000000000..bc701f17fd6 --- /dev/null +++ b/product_attribute_value_name_search/static/description/index.html @@ -0,0 +1,424 @@ + + + + + +Product Attribute Value Name Search + + + + + + diff --git a/product_attribute_value_name_search/tests/__init__.py b/product_attribute_value_name_search/tests/__init__.py new file mode 100644 index 00000000000..309a111bc01 --- /dev/null +++ b/product_attribute_value_name_search/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_attribute_value_name_search diff --git a/product_attribute_value_name_search/tests/test_product_attribute_value_name_search.py b/product_attribute_value_name_search/tests/test_product_attribute_value_name_search.py new file mode 100644 index 00000000000..8cd0b0ea360 --- /dev/null +++ b/product_attribute_value_name_search/tests/test_product_attribute_value_name_search.py @@ -0,0 +1,97 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.fields import Command + +from odoo.addons.base.tests.common import BaseCommon + + +class TestProductTemplateAttributeValueNameSearch(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + Attr = cls.env["product.attribute"] + Val = cls.env["product.attribute.value"] + cls.attr_color = Attr.create({"name": "Color"}) + cls.attr_size = Attr.create({"name": "Size"}) + cls.pav_color_red = Val.create( + {"name": "Red", "attribute_id": cls.attr_color.id} + ) + cls.pav_size_red = Val.create({"name": "Red", "attribute_id": cls.attr_size.id}) + cls.pav_size_large = Val.create( + {"name": "L: Large", "attribute_id": cls.attr_size.id} + ) + # Create a product template with attribute lines to generate PTAVs + cls.tmpl = cls.env["product.template"].create( + { + "name": "Test Product", + "attribute_line_ids": [ + Command.create( + { + "attribute_id": cls.attr_color.id, + "value_ids": [Command.set([cls.pav_color_red.id])], + } + ), + Command.create( + { + "attribute_id": cls.attr_size.id, + "value_ids": [ + Command.set( + [cls.pav_size_red.id, cls.pav_size_large.id] + ) + ], + } + ), + ], + } + ) + PTAV = cls.env["product.template.attribute.value"] + cls.ptav_color_red = PTAV.search( + [ + ("product_tmpl_id", "=", cls.tmpl.id), + ("product_attribute_value_id", "=", cls.pav_color_red.id), + ] + ) + cls.ptav_size_red = PTAV.search( + [ + ("product_tmpl_id", "=", cls.tmpl.id), + ("product_attribute_value_id", "=", cls.pav_size_red.id), + ] + ) + cls.ptav_size_large = PTAV.search( + [ + ("product_tmpl_id", "=", cls.tmpl.id), + ("product_attribute_value_id", "=", cls.pav_size_large.id), + ] + ) + + def test_exact_match(self): + """Search with 'Attribute: Value' format returns only the correct one.""" + PTAV = self.env["product.template.attribute.value"] + result = PTAV.name_search("Color: Red", operator="=") + result_ids = [r[0] for r in result] + self.assertIn(self.ptav_color_red.id, result_ids) + self.assertNotIn(self.ptav_size_red.id, result_ids) + + def test_ilike_match(self): + """Search with ilike operator on composite name works.""" + PTAV = self.env["product.template.attribute.value"] + result = PTAV.name_search("Color: Re", operator="ilike") + result_ids = [r[0] for r in result] + self.assertIn(self.ptav_color_red.id, result_ids) + self.assertNotIn(self.ptav_size_red.id, result_ids) + + def test_fallback_without_separator(self): + """Search without separator falls back to default behavior.""" + PTAV = self.env["product.template.attribute.value"] + result = PTAV.name_search("Red", operator="ilike") + result_ids = [r[0] for r in result] + self.assertIn(self.ptav_color_red.id, result_ids) + self.assertIn(self.ptav_size_red.id, result_ids) + + def test_colon_in_value_name(self): + """Value name containing ': ' is handled by splitting on first occurrence.""" + PTAV = self.env["product.template.attribute.value"] + result = PTAV.name_search("Size: L: Large", operator="=") + result_ids = [r[0] for r in result] + self.assertIn(self.ptav_size_large.id, result_ids)