diff --git a/product_similarity/README.rst b/product_similarity/README.rst new file mode 100644 index 000000000..ce5a54450 --- /dev/null +++ b/product_similarity/README.rst @@ -0,0 +1,210 @@ +=================== +Products Similarity +=================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:bcc3f3f508cbab83783e2860f6935c8c03c9aa0642714517587584244bea9bea + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fodoo--pim-lightgray.png?logo=github + :target: https://github.com/OCA/odoo-pim/tree/16.0/product_similarity + :alt: OCA/odoo-pim +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/odoo-pim-16-0/odoo-pim-16-0-product_similarity + :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/odoo-pim&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +[ This file must be max 2-3 paragraphs, and is required. + +The goal of this document is to explain quickly the features of this +module: “what” this module does and “what” it is for. ] + +Example: + +This module extends the functionality of ... to support ... and to allow +users to ... + +**Table of contents** + +.. contents:: + :local: + +Use Cases / Context +=================== + +[ This file is optional but strongly suggested to allow end-users to +evaluate the module's usefulness in their context. ] + +BUSINESS NEED: It should explain the “why” of the module: + +- what is the business requirement that generated the need to develop + this module +- in which context or use cases this module can be useful (practical + examples are welcome!). + +APPROACH: It could also explain the approach to address the mentioned +need. + +USEFUL INFORMATION: It can also inform on related modules: + +- modules it depends on and their features +- other modules that can work well together with this one +- suggested setups where the module is useful (eg: multicompany, + multi-website) + +Installation +============ + +[ This file must only be present if there are very specific installation +instructions, such as installing non-python dependencies. The audience +is systems administrators. ] + +To install this module, you need to: + +1. Do this ... + +Configuration +============= + +[ This file is not always required; it should explain **how to configure +the module before using it**; it is aimed at users with administration +privileges. + +Please be detailed on the path to configuration (eg: do you need to +activate developer mode?), describe step by step configurations and the +use of screenshots is strongly recommended.] + +To configure this module, you need to: + +- Go to *App* > Menu > Menu item +- Activate boolean… > save +- … + +Usage +===== + +[ This file is required and contains the instructions on **“how”** to +use the module for end-users. + +If the module does not have a visible impact on the user interface, just +add the following sentence: + + This module does not impact the user interface. + +If that’s not the case, please make sure that every usage step is +covered and remember that images speak more than words!] + +To use this module, you need to: + +- Go to *App* > Menu > Menu item + + *insert screenshot!* + +- In “Contact” form, add a value to field *xyz* > save + + *insert screenshot!* + +- The value of *xyz* is now displayed in the list view. + + *insert screenshot!* + +Known issues / Roadmap +====================== + +[ Enumerate known caveats and future potential improvements. It is +mostly intended for end-users, and can also help potential new +contributors discovering new features to implement. ] + +- ... + +Changelog +========= + +[ The change log. The goal of this file is to help readers understand +changes between version. The primary audience is end users and +integrators. Purely technical changes such as code refactoring must not +be mentioned here. + +This file may contain ONE level of section titles, underlined with the ~ +(tilde) character. Other section markers are forbidden and will likely +break the structure of the README.rst or other documents where this +fragment is included. ] + +11.0.x.y.z (YYYY-MM-DD) +----------------------- + +- [BREAKING] Breaking changes come first. + (`#70 `__) +- [ADD] New feature. (`#74 `__) +- [FIX] Correct this. (`#71 `__) + +11.0.x.y.z (YYYY-MM-DD) +----------------------- + +- ... + +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 +------- + +* ACSONE SA/NV + +Contributors +------------ + +- Nicolas Delbovier nicolas.delbovier@acsone.eu +- Laurent Mignon laurent.mignon@acsone.eu + +Other credits +------------- + +[ This file is optional and contains additional credits, other than +authors, contributors, and maintainers. ] + +The development of this module has been financially supported by: + +- Company 1 name +- Company 2 name + +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. + +This module is part of the `OCA/odoo-pim `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_similarity/__init__.py b/product_similarity/__init__.py new file mode 100644 index 000000000..aee8895e7 --- /dev/null +++ b/product_similarity/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/product_similarity/__manifest__.py b/product_similarity/__manifest__.py new file mode 100644 index 000000000..a3e78598f --- /dev/null +++ b/product_similarity/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Products Similarity", + "summary": "Enables to compute a similarity score between " + "products using vectorial embeddings", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/odoo-pim", + "depends": ["product", "field_vector", "queue_job"], + "assets": { + "web.assets_backend": [ + "product_similarity/static/src/js/product_vector_characteristic_tree_extend.js", + "product_similarity/static/src/xml/product_vector_characteristic_button.xml", + ] + }, + "data": [ + "security/ir.model.access.csv", + "views/product_vector_characteristic.xml", + "wizards/product_field_vectorization_wizard.xml", + ], + "demo": [], +} diff --git a/product_similarity/models/__init__.py b/product_similarity/models/__init__.py new file mode 100644 index 000000000..16e91bf6a --- /dev/null +++ b/product_similarity/models/__init__.py @@ -0,0 +1 @@ +from . import product_vector_characteristic diff --git a/product_similarity/models/product_vector_characteristic.py b/product_similarity/models/product_vector_characteristic.py new file mode 100644 index 000000000..94c2c3cbb --- /dev/null +++ b/product_similarity/models/product_vector_characteristic.py @@ -0,0 +1,191 @@ +from odoo import _, api, fields, models +from odoo.exceptions import UserError + + +class ProductVectorCharacteristic(models.Model): + """ + Each record on this model represent one characteristic used on products + characteristics vector. + + The main idea of this model is to link each characteristic with a + dimension and a weight inside the characteristics vector. + """ + + _name = "product.vector.characteristic" + _description = "Product Vector Characteristic" + + field_id = fields.Many2one( + "ir.model.fields", + store=True, + ondelete="cascade", + string="Field", + required=True, + domain=[ + ("model", "=", "product.product"), + ( + "ttype", + "in", + ["many2one", "many2many", "selection", "boolean"], + ), + ], + help="Field inside the `product.product` model linked to the current characteristic.", + ) + model_id = fields.Many2one( + "ir.model", + compute="_compute_model_id", + help="Model name of the given characteristic", + ) + value_id = fields.Many2oneReference( + model_field="model_id", + help="The id of the specific value of the characteristic from the linked model.\n" + "For example, if the characteristic's model is \"Color(0: 'red', 1: 'green', 2: " + "'blue')\", then to represent the color 'green', you would choose `1`.", + ) + value_name = fields.Text(compute="_compute_value_name") + value_id_visible = fields.Boolean(compute="_compute_value_id_visible") + possible_values_string = fields.Text(compute="_compute_possible_values_string") + name = fields.Text(compute="_compute_name") + + weight = fields.Float( + required=True, + default=1, + help="Weight applied to the current characteristic's index when computing " + "vector distances.", + ) + + vector_index = fields.Integer( + readonly=True, + help="Index of current characteristic inside the characteristics vector", + ) + + _sql_constraints = [ + ( + "unique_field_value", + "UNIQUE(field_id, value_id)", + "The given pair (field_id, value_id) already exists.", + ), + ( + "unique_vector_index", + "UNIQUE(vector_index)", + "There cannot be two characteristics pointing to the same index in the vector.", + ), + ( + "check_vector_index_non_negative", + "CHECK(vector_index >= 0)", + "A vector index must be >= 0", + ), + ( + "check_weight_positive", + "CHECK(weight > 0)", + "weight must be > 0", + ), + ] + + @api.depends("field_id", "value_id") + def _compute_value_name(self): + for record in self: + possible_values = record.get_possible_values(record.field_id) + if not possible_values: + record.value_name = "" + else: + record.value_name = possible_values.get(record.value_id, "") + + @api.depends("field_id", "value_name") + def _compute_name(self): + for record in self: + if record.field_id.ttype == "boolean": + record.name = record.field_id.name + else: + record.name = f"{record.field_id.name} = '{record.value_name}'" + + @api.depends("field_id") + def _compute_value_id_visible(self): + for record in self: + if not record.field_id or record.field_id.ttype == "boolean": + record.value_id_visible = False + else: + record.value_id_visible = True + + @api.model + def get_possible_values(self, field): + if not field: + return {} + + elif field.ttype == "boolean": + return {} + elif field.ttype == "selection": + return {x[0]: x[1] for x in field.selection_ids.name_get()} + else: + model = self.get_related_model(field) + return {x[0]: x[1] for x in self.env[model.model].search([]).name_get()} + + @api.depends("field_id") + def _compute_possible_values_string(self): + for record in self: + possible_values = record.get_possible_values(record.field_id) + if possible_values: + record.possible_values_string = "\n".join( + f"{value_id: <4n} {value_name}" + for value_id, value_name in sorted(possible_values.items()) + ) + else: + record.possible_values_string = "" + + @api.model + def get_related_model(self, field): + if field.ttype in ["many2one", "one2many", "many2many"]: + return self.env["ir.model"].search( + [("model", "=", field.relation)], limit=1 + ) + elif field.ttype in ["boolean", "selection"]: + return self.env["ir.model"].search( + [("model", "=", "product.product")], limit=1 + ) + else: + return False + + @api.depends("field_id") + def _compute_model_id(self): + for record in self: + record.model_id = record.get_related_model(record.field_id) + + @api.constrains("value_id") + def _check_value_id(self): + for record in self: + possible_values = record.get_possible_values(record.field_id) + if not possible_values: + continue + possible_ids = list(possible_values) + if record.value_id not in possible_ids: + raise UserError( + _("The given value_id is not inside the possible value_id's") + ) + + @api.model + def _get_empty_index(self, already_assigned_indices=None): + stored_indices = [ + x["vector_index"] for x in self.search_read([], ["vector_index"]) + ] + indices = stored_indices + ( + already_assigned_indices if already_assigned_indices else [] + ) + if not indices: + return 0 + for i, index in enumerate(sorted(set(indices))): + if i != index: + return i + return len(indices) + + @api.model_create_multi + def create(self, vals_list): + # Be sure to check fo an empty index BEFORE creating + # the record + already_assigned_indices = [] + for vals in vals_list: + index = self.env["product.vector.characteristic"]._get_empty_index( + already_assigned_indices + ) + vals["vector_index"] = index + already_assigned_indices.append(index) + res = super().create(vals_list) + return res diff --git a/product_similarity/readme/CONFIGURE.md b/product_similarity/readme/CONFIGURE.md new file mode 100644 index 000000000..2fdb0e64a --- /dev/null +++ b/product_similarity/readme/CONFIGURE.md @@ -0,0 +1,10 @@ +[ This file is not always required; it should explain **how to configure the module before using it**; it is aimed at users with administration privileges. + +Please be detailed on the path to configuration (eg: do you need to activate developer mode?), describe step by step configurations and the use of screenshots is strongly recommended.] + + +To configure this module, you need to: + +- Go to *App* > Menu > Menu item +- Activate boolean… > save +- … diff --git a/product_similarity/readme/CONTEXT.md b/product_similarity/readme/CONTEXT.md new file mode 100644 index 000000000..096235a2f --- /dev/null +++ b/product_similarity/readme/CONTEXT.md @@ -0,0 +1,16 @@ +[ This file is optional but strongly suggested to allow end-users to evaluate the +module's usefulness in their context. ] + +BUSINESS NEED: +It should explain the “why” of the module: +- what is the business requirement that generated the need to develop this module +- in which context or use cases this module can be useful (practical examples are welcome!). + +APPROACH: +It could also explain the approach to address the mentioned need. + +USEFUL INFORMATION: +It can also inform on related modules: +- modules it depends on and their features +- other modules that can work well together with this one +- suggested setups where the module is useful (eg: multicompany, multi-website) diff --git a/product_similarity/readme/CONTRIBUTORS.md b/product_similarity/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..8c5ed5a12 --- /dev/null +++ b/product_similarity/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Nicolas Delbovier +- Laurent Mignon diff --git a/product_similarity/readme/CREDITS.md b/product_similarity/readme/CREDITS.md new file mode 100644 index 000000000..9c2b025b5 --- /dev/null +++ b/product_similarity/readme/CREDITS.md @@ -0,0 +1,7 @@ +[ This file is optional and contains additional credits, other than + authors, contributors, and maintainers. ] + +The development of this module has been financially supported by: + +- Company 1 name +- Company 2 name diff --git a/product_similarity/readme/DESCRIPTION.md b/product_similarity/readme/DESCRIPTION.md new file mode 100644 index 000000000..2371a1464 --- /dev/null +++ b/product_similarity/readme/DESCRIPTION.md @@ -0,0 +1,7 @@ +[ This file must be max 2-3 paragraphs, and is required. + +The goal of this document is to explain quickly the features of this module: “what” this module does and “what” it is for. ] + +Example: + +This module extends the functionality of ... to support ... and to allow users to ... diff --git a/product_similarity/readme/HISTORY.md b/product_similarity/readme/HISTORY.md new file mode 100644 index 000000000..a6daf58cc --- /dev/null +++ b/product_similarity/readme/HISTORY.md @@ -0,0 +1,22 @@ +[ The change log. The goal of this file is to help readers + understand changes between version. The primary audience is + end users and integrators. Purely technical changes such as + code refactoring must not be mentioned here. + + This file may contain ONE level of section titles, underlined + with the ~ (tilde) character. Other section markers are + forbidden and will likely break the structure of the README.rst + or other documents where this fragment is included. ] + +## 11.0.x.y.z (YYYY-MM-DD) + +- [BREAKING] Breaking changes come first. + ([#70](https://github.com/OCA/repo/issues/70)) +- [ADD] New feature. + ([#74](https://github.com/OCA/repo/issues/74)) +- [FIX] Correct this. + ([#71](https://github.com/OCA/repo/issues/71)) + +## 11.0.x.y.z (YYYY-MM-DD) + +- ... diff --git a/product_similarity/readme/INSTALL.md b/product_similarity/readme/INSTALL.md new file mode 100644 index 000000000..77b98e7aa --- /dev/null +++ b/product_similarity/readme/INSTALL.md @@ -0,0 +1,7 @@ +[ This file must only be present if there are very specific + installation instructions, such as installing non-python + dependencies. The audience is systems administrators. ] + +To install this module, you need to: + +1. Do this ... diff --git a/product_similarity/readme/ROADMAP.md b/product_similarity/readme/ROADMAP.md new file mode 100644 index 000000000..446840cfb --- /dev/null +++ b/product_similarity/readme/ROADMAP.md @@ -0,0 +1,5 @@ +[ Enumerate known caveats and future potential improvements. + It is mostly intended for end-users, and can also help + potential new contributors discovering new features to implement. ] + +- ... diff --git a/product_similarity/readme/USAGE.md b/product_similarity/readme/USAGE.md new file mode 100644 index 000000000..2cf127584 --- /dev/null +++ b/product_similarity/readme/USAGE.md @@ -0,0 +1,21 @@ +[ This file is required and contains the instructions on **“how”** to use the module for end-users. + +If the module does not have a visible impact on the user interface, just add the following sentence: + +> This module does not impact the user interface. + +If that’s not the case, please make sure that every usage step is covered and remember that images speak more than words!] + +To use this module, you need to: + +- Go to *App* > Menu > Menu item + + *insert screenshot!* + +- In “Contact” form, add a value to field *xyz* > save + + *insert screenshot!* + +- The value of *xyz* is now displayed in the list view. + + *insert screenshot!* diff --git a/product_similarity/security/ir.model.access.csv b/product_similarity/security/ir.model.access.csv new file mode 100644 index 000000000..144d2a652 --- /dev/null +++ b/product_similarity/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +product_similarity.access_product_vector_characteristic,product_similarity.product.vector.characteristic,product_similarity.model_product_vector_characteristic,base.group_user,1,1,1,1 +access_product_field_vectorization_wizard,product.field.vectorization.wizard.access,model_product_field_vectorization_wizard,base.group_user,1,1,1,1 diff --git a/product_similarity/static/description/icon.png b/product_similarity/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/product_similarity/static/description/icon.png differ diff --git a/product_similarity/static/description/index.html b/product_similarity/static/description/index.html new file mode 100644 index 000000000..ca42b1dbe --- /dev/null +++ b/product_similarity/static/description/index.html @@ -0,0 +1,554 @@ + + + + + +Products Similarity + + + +
+

Products Similarity

+ + +

Beta License: AGPL-3 OCA/odoo-pim Translate me on Weblate Try me on Runboat

+

[ This file must be max 2-3 paragraphs, and is required.

+

The goal of this document is to explain quickly the features of this +module: “what” this module does and “what” it is for. ]

+

Example:

+

This module extends the functionality of … to support … and to allow +users to …

+

Table of contents

+ +
+

Use Cases / Context

+

[ This file is optional but strongly suggested to allow end-users to +evaluate the module’s usefulness in their context. ]

+

BUSINESS NEED: It should explain the “why” of the module:

+
    +
  • what is the business requirement that generated the need to develop +this module
  • +
  • in which context or use cases this module can be useful (practical +examples are welcome!).
  • +
+

APPROACH: It could also explain the approach to address the mentioned +need.

+

USEFUL INFORMATION: It can also inform on related modules:

+
    +
  • modules it depends on and their features
  • +
  • other modules that can work well together with this one
  • +
  • suggested setups where the module is useful (eg: multicompany, +multi-website)
  • +
+
+
+

Installation

+

[ This file must only be present if there are very specific installation +instructions, such as installing non-python dependencies. The audience +is systems administrators. ]

+

To install this module, you need to:

+
    +
  1. Do this …
  2. +
+
+
+

Configuration

+

[ This file is not always required; it should explain how to configure +the module before using it; it is aimed at users with administration +privileges.

+

Please be detailed on the path to configuration (eg: do you need to +activate developer mode?), describe step by step configurations and the +use of screenshots is strongly recommended.]

+

To configure this module, you need to:

+
    +
  • Go to App > Menu > Menu item
  • +
  • Activate boolean… > save
  • +
  • +
+
+
+

Usage

+

[ This file is required and contains the instructions on “how” to +use the module for end-users.

+

If the module does not have a visible impact on the user interface, just +add the following sentence:

+
+This module does not impact the user interface.
+

If that’s not the case, please make sure that every usage step is +covered and remember that images speak more than words!]

+

To use this module, you need to:

+
    +
  • Go to App > Menu > Menu item

    +

    insert screenshot!

    +
  • +
  • In “Contact” form, add a value to field xyz > save

    +

    insert screenshot!

    +
  • +
  • The value of xyz is now displayed in the list view.

    +

    insert screenshot!

    +
  • +
+
+
+

Known issues / Roadmap

+

[ Enumerate known caveats and future potential improvements. It is +mostly intended for end-users, and can also help potential new +contributors discovering new features to implement. ]

+
    +
  • +
+
+
+

Changelog

+

[ The change log. The goal of this file is to help readers understand +changes between version. The primary audience is end users and +integrators. Purely technical changes such as code refactoring must not +be mentioned here.

+

This file may contain ONE level of section titles, underlined with the ~ +(tilde) character. Other section markers are forbidden and will likely +break the structure of the README.rst or other documents where this +fragment is included. ]

+
+

11.0.x.y.z (YYYY-MM-DD)

+
    +
  • [BREAKING] Breaking changes come first. +(#70)
  • +
  • [ADD] New feature. (#74)
  • +
  • [FIX] Correct this. (#71)
  • +
+
+ +
+
+

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

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+

[ This file is optional and contains additional credits, other than +authors, contributors, and maintainers. ]

+

The development of this module has been financially supported by:

+
    +
  • Company 1 name
  • +
  • Company 2 name
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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.

+

This module is part of the OCA/odoo-pim project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/product_similarity/static/src/js/product_vector_characteristic_tree_extend.js b/product_similarity/static/src/js/product_vector_characteristic_tree_extend.js new file mode 100644 index 000000000..7202f65f2 --- /dev/null +++ b/product_similarity/static/src/js/product_vector_characteristic_tree_extend.js @@ -0,0 +1,32 @@ +/** @odoo-module */ +import {ListController} from "@web/views/list/list_controller"; +import {registry} from "@web/core/registry"; +import {listView} from "@web/views/list/list_view"; +export class ProductVectorCharacteristicsListController extends ListController { + setup() { + super.setup(); + } + OnClickVectorizationWizard() { + this.actionService.doAction( + { + type: "ir.actions.act_window", + res_model: "product.field.vectorization.wizard", + name: "Vectorize field", + views: [[false, "form"]], + target: "new", + }, + { + // Ensures the new created records are loaded once we close the wizard + onClose: () => { + this.model.load(); + }, + } + ); + } +} + +registry.category("views").add("vectorization_wizard_button_in_tree", { + ...listView, + Controller: ProductVectorCharacteristicsListController, + buttonTemplate: "product_vector_characteristic.ListView.Buttons", +}); diff --git a/product_similarity/static/src/xml/product_vector_characteristic_button.xml b/product_similarity/static/src/xml/product_vector_characteristic_button.xml new file mode 100644 index 000000000..f156bc0f4 --- /dev/null +++ b/product_similarity/static/src/xml/product_vector_characteristic_button.xml @@ -0,0 +1,18 @@ + + + + + + + + diff --git a/product_similarity/tests/__init__.py b/product_similarity/tests/__init__.py new file mode 100644 index 000000000..b5b1ec1fc --- /dev/null +++ b/product_similarity/tests/__init__.py @@ -0,0 +1,3 @@ +from . import test_product_vector_characteristic +from . import test_field_vectorization_wizard +from . import common diff --git a/product_similarity/tests/common.py b/product_similarity/tests/common.py new file mode 100644 index 000000000..ea963aa6d --- /dev/null +++ b/product_similarity/tests/common.py @@ -0,0 +1,49 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestProductSimilarityCommon(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.boolean_field = cls.env["ir.model.fields"].search( + [ + ("model", "=", "product.product"), + ("name", "=", "sale_ok"), + ] + ) + + cls.selection_field = cls.env["ir.model.fields"].search( + [ + ("model", "=", "product.product"), + ("name", "=", "activity_exception_decoration"), + ] + ) + cls.selection_field_values = cls.env[ + "product.vector.characteristic" + ].get_possible_values(cls.selection_field) + + cls.many_to_many_field = cls.env["ir.model.fields"].search( + [ + ("model", "=", "product.product"), + ("name", "=", "product_tag_ids"), + ] + ) + cls.env[cls.many_to_many_field.relation].create( + [{"name": n} for n in ["red", "green", "blue"]] + ) + cls.many_to_many_field_values = cls.env[ + "product.vector.characteristic" + ].get_possible_values(cls.many_to_many_field) + + cls.many_to_one_field = cls.env["ir.model.fields"].search( + [ + ("model", "=", "product.product"), + ("name", "=", "cost_currency_id"), + ] + ) + cls.many_to_one_field_values = cls.env[ + "product.vector.characteristic" + ].get_possible_values(cls.many_to_one_field) diff --git a/product_similarity/tests/test_field_vectorization_wizard.py b/product_similarity/tests/test_field_vectorization_wizard.py new file mode 100644 index 000000000..54768e4fb --- /dev/null +++ b/product_similarity/tests/test_field_vectorization_wizard.py @@ -0,0 +1,67 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from .common import TestProductSimilarityCommon + + +class TestProductVectorCharacteristic(TestProductSimilarityCommon): + def test_wizard_vectorize_biary_field(self): + wizard_action = self.env[ + "product.vector.characteristic" + ].field_vectorization_wizard_action() + wizard = self.env[wizard_action["res_model"]].create( + {"field_id": self.boolean_field.id} + ) + wizard.put_in_vector_action() + + characteristic = self.env["product.vector.characteristic"].search([]) + self.assertEqual( + len(characteristic), + 1, + "Expected exactly one characteristic to be created by wizard for binary field.", + ) + + def test_wizard_vectorize_selection_field(self): + wizard_action = self.env[ + "product.vector.characteristic" + ].field_vectorization_wizard_action() + wizard = self.env[wizard_action["res_model"]].create( + {"field_id": self.selection_field.id} + ) + wizard.put_in_vector_action() + + characteristic = self.env["product.vector.characteristic"].search([]) + self.assertEqual( + len(characteristic), + len(self.selection_field_values), + ) + + def test_wizard_vectorize_many_to_one_field(self): + wizard_action = self.env[ + "product.vector.characteristic" + ].field_vectorization_wizard_action() + wizard = self.env[wizard_action["res_model"]].create( + {"field_id": self.many_to_one_field.id} + ) + wizard.put_in_vector_action() + + characteristic = self.env["product.vector.characteristic"].search([]) + self.assertEqual( + len(characteristic), + len(self.many_to_one_field_values), + ) + + def test_wizard_vectorize_many_to_many_field(self): + wizard_action = self.env[ + "product.vector.characteristic" + ].field_vectorization_wizard_action() + wizard = self.env[wizard_action["res_model"]].create( + {"field_id": self.many_to_many_field.id} + ) + wizard.put_in_vector_action() + + characteristic = self.env["product.vector.characteristic"].search([]) + self.assertEqual( + len(characteristic), + len(self.many_to_many_field_values), + ) diff --git a/product_similarity/tests/test_product_vector_characteristic.py b/product_similarity/tests/test_product_vector_characteristic.py new file mode 100644 index 000000000..585b338c9 --- /dev/null +++ b/product_similarity/tests/test_product_vector_characteristic.py @@ -0,0 +1,85 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.exceptions import UserError + +from .common import TestProductSimilarityCommon + + +class TestProductVectorCharacteristic(TestProductSimilarityCommon): + def test_ensure_vector_index_starts_at_0(self): + vector_characteristic = self.env["product.vector.characteristic"].create( + {"field_id": self.boolean_field.id, "weight": 1} + ) + self.assertEqual(vector_characteristic.vector_index, 0) + + def test_vector_index_fits_hole(self): + possible_value_ids = list(self.many_to_many_field_values) + vector_characteristic_1 = self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": possible_value_ids[0], + "weight": 1, + } + ) + vector_characteristic_2 = self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": possible_value_ids[1], + "weight": 1, + } + ) + vector_characteristic_3 = self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": possible_value_ids[2], + "weight": 1, + } + ) + + self.assertEqual(vector_characteristic_1.vector_index, 0) + self.assertEqual(vector_characteristic_2.vector_index, 1) + self.assertEqual(vector_characteristic_3.vector_index, 2) + + vector_characteristic_2.unlink() + vector_characteristic_4 = self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": possible_value_ids[1], + "weight": 1, + } + ) + self.assertEqual(vector_characteristic_4.vector_index, 1) + + def test_prevent_creation_for_invalid_value_id(self): + possible_value_ids = list(self.many_to_many_field_values) + + with self.assertRaises(UserError): + self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": sum(possible_value_ids), + "weight": 1, + } + ) + + def test_prevent_duplicate_vector_index(self): + possible_value_ids = list(self.many_to_many_field_values) + vector_characteristic_1 = self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": possible_value_ids[0], + "weight": 1, + "vector_index": 0, # <- should not be taken into account + } + ) + vector_characteristic_2 = self.env["product.vector.characteristic"].create( + { + "field_id": self.many_to_many_field.id, + "value_id": possible_value_ids[1], + "weight": 1, + "vector_index": 0, # <- should not be taken into account + } + ) + self.assertEqual(vector_characteristic_1.vector_index, 0) + self.assertEqual(vector_characteristic_2.vector_index, 1) diff --git a/product_similarity/views/product_vector_characteristic.xml b/product_similarity/views/product_vector_characteristic.xml new file mode 100644 index 000000000..421255b4c --- /dev/null +++ b/product_similarity/views/product_vector_characteristic.xml @@ -0,0 +1,78 @@ + + + + + + product.vector.characteristic + +
+
+
+ + + + + + + + + + + + + +
+
+
+ + + + + product.vector.characteristic + + + + + + + + + + + + + + Product Vector Characteristic + product.vector.characteristic + tree,form + [] + {} + + + + Product Vector Characteristic + + + + + +
diff --git a/product_similarity/wizards/__init__.py b/product_similarity/wizards/__init__.py new file mode 100644 index 000000000..6a4f27b33 --- /dev/null +++ b/product_similarity/wizards/__init__.py @@ -0,0 +1 @@ +from . import product_field_vectorization_wizard diff --git a/product_similarity/wizards/product_field_vectorization_wizard.py b/product_similarity/wizards/product_field_vectorization_wizard.py new file mode 100644 index 000000000..f2df80f66 --- /dev/null +++ b/product_similarity/wizards/product_field_vectorization_wizard.py @@ -0,0 +1,69 @@ +# wizards/product_field_wizard.py + +from odoo import _, fields, models + + +class ProductFieldWizard(models.TransientModel): + _name = "product.field.vectorization.wizard" + _description = "Product Field vectorization Wizard" + + field_id = fields.Many2one( + "ir.model.fields", + required=True, + domain=[ + ("model", "=", "product.product"), + ( + "ttype", + "in", + ["many2one", "many2many", "selection", "boolean"], + ), + ], + ) + + weight = fields.Float(default=1, required=True) + + def put_in_vector_action(self): + self.ensure_one() + possible_values = self.env["product.vector.characteristic"].get_possible_values( + self.field_id + ) + already_existing_value_ids = [ + y["value_id"] + for y in self.env["product.vector.characteristic"].search_read( + [("field_id", "=", self.field_id.id)], ["value_id"] + ) + ] + to_add_value_ids = [ + x for x in possible_values if x not in already_existing_value_ids + ] + nb_new = len(to_add_value_ids) + if self.field_id.ttype == "boolean" and not already_existing_value_ids: + self.env["product.vector.characteristic"].create( + {"field_id": self.field_id.id, "weight": self.weight} + ) + nb_new = 1 + elif to_add_value_ids: + self.env["product.vector.characteristic"].create( + [ + { + "field_id": self.field_id.id, + "value_id": value_id, + "weight": self.weight, + } + for value_id in to_add_value_ids + ] + ) + + self.env["bus.bus"]._sendone( + self.env.user.partner_id, + "simple_notification", + { + "type": "success", + "title": _("New Vector Characteristics Created Successfully"), + "message": _( + "Created %(nb_new)s new Vector Characteristics.", nb_new=nb_new + ), + "sticky": True, + }, + ) + return {"type": "ir.actions.act_window_close"} diff --git a/product_similarity/wizards/product_field_vectorization_wizard.xml b/product_similarity/wizards/product_field_vectorization_wizard.xml new file mode 100644 index 000000000..88601ff65 --- /dev/null +++ b/product_similarity/wizards/product_field_vectorization_wizard.xml @@ -0,0 +1,26 @@ + + + product.field.vectorization.wizard.form + product.field.vectorization.wizard + +
+ + + + + + + +
+
+
+ +
diff --git a/setup/product_similarity/odoo/addons/product_similarity b/setup/product_similarity/odoo/addons/product_similarity new file mode 120000 index 000000000..15332b4e5 --- /dev/null +++ b/setup/product_similarity/odoo/addons/product_similarity @@ -0,0 +1 @@ +../../../../product_similarity \ No newline at end of file diff --git a/setup/product_similarity/setup.py b/setup/product_similarity/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/product_similarity/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)