diff --git a/setup/web_field_reference_domain/odoo/addons/web_field_reference_domain b/setup/web_field_reference_domain/odoo/addons/web_field_reference_domain new file mode 120000 index 000000000000..272379ba3f3d --- /dev/null +++ b/setup/web_field_reference_domain/odoo/addons/web_field_reference_domain @@ -0,0 +1 @@ +../../../../web_field_reference_domain \ No newline at end of file diff --git a/setup/web_field_reference_domain/setup.py b/setup/web_field_reference_domain/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/web_field_reference_domain/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_field_reference_domain/README.rst b/web_field_reference_domain/README.rst new file mode 100644 index 000000000000..7b4b0db1ea08 --- /dev/null +++ b/web_field_reference_domain/README.rst @@ -0,0 +1,93 @@ +========================== +Web Field Reference Domain +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:e1fc658206388842a964783e100c6442d011687ad0a692bdb83c8b5c0301e357 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/14.0/web_field_reference_domain + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-14-0/web-14-0-web_field_reference_domain + :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/web&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Add support to define domains in reference fields + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Use the attribute ```domain_map``` to declare the field domains: + +.. code-block:: html + + + +Known issues / Roadmap +====================== + +* Add tests. +* Extends the reference field in python to allow the 'domain_map' attribute. + +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 +~~~~~~~ + +* Grupo Isonor + +Contributors +~~~~~~~~~~~~ + +* `Grupo Isonor `_: + + * Alexandre D. Díaz + +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/web `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_field_reference_domain/__init__.py b/web_field_reference_domain/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/web_field_reference_domain/__manifest__.py b/web_field_reference_domain/__manifest__.py new file mode 100644 index 000000000000..63ed508c28f3 --- /dev/null +++ b/web_field_reference_domain/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2024 Alexandre D. Díaz - Grupo Isonor +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +{ + "name": "Web Field Reference Domain", + "summary": "Add support to define domains on reference fields", + "version": "14.0.1.0.0", + "category": "Web", + "website": "https://github.com/OCA/web", + "author": "Grupo Isonor, Odoo Community Association (OCA)", + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": ["web"], + "data": ["templates/assets.xml"], + "demo": [], +} diff --git a/web_field_reference_domain/static/description/index.html b/web_field_reference_domain/static/description/index.html new file mode 100644 index 000000000000..cb0dcf7e2bb8 --- /dev/null +++ b/web_field_reference_domain/static/description/index.html @@ -0,0 +1,440 @@ + + + + + + +Web Field Reference Domain + + + +
+

Web Field Reference Domain

+ + +

Beta License: LGPL-3 OCA/web Translate me on Weblate Try me on Runboat

+

Add support to define domains in reference fields

+

Table of contents

+ +
+

Usage

+

Use the attribute `domain_map` to declare the field domains:

+
+<field name="reference_field" domain_map="{'model_a': [domain], 'model_n': [domain_n]}" />
+
+
+
+

Known issues / Roadmap

+
    +
  • Add tests.
  • +
  • Extends the reference field in python to allow the ‘domain_map’ attribute.
  • +
+
+
+

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

+
    +
  • Grupo Isonor
  • +
+
+
+

Contributors

+ +
+
+

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/web project on GitHub.

+

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

+
+
+
+ + diff --git a/web_field_reference_domain/static/src/js/basic_model.js b/web_field_reference_domain/static/src/js/basic_model.js new file mode 100644 index 000000000000..e7b2f6a4fd77 --- /dev/null +++ b/web_field_reference_domain/static/src/js/basic_model.js @@ -0,0 +1,24 @@ +// Copyright 2024 Alexandre D. Díaz - Grupo Isonor +// License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +odoo.define("web_field_reference_domain.BasicModel", function (require) { + "use strict"; + + var BasicModel = require("web.BasicModel"); + + BasicModel.include({ + /** + * This is donde in this way to avoid do extra searchs. + * FIXME: If someday Odoo changes the implementation of the + * '_search' method in the 'Many2One', see if you can avoid + * doing this. + * + * @override + */ + _getDomain: function (element, options) { + if (options && options.referenceDomain) { + return options.referenceDomain; + } + return this._super(...arguments); + }, + }); +}); diff --git a/web_field_reference_domain/static/src/js/relational_fields.js b/web_field_reference_domain/static/src/js/relational_fields.js new file mode 100644 index 000000000000..d020065f66d3 --- /dev/null +++ b/web_field_reference_domain/static/src/js/relational_fields.js @@ -0,0 +1,56 @@ +/* global py */ +// Copyright 2024 Alexandre D. Díaz - Grupo Isonor +// License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +odoo.define("web_field_reference_domain.relational_fields", function (require) { + "use strict"; + + const relational_fields = require("web.relational_fields"); + + relational_fields.FieldMany2One.include({ + /** + * @override + */ + init: function () { + this._super(...arguments); + this.isReferenceField = this.field.type === "reference"; + }, + + /** + * Gets the domain for the selected model + * + * @returns {Array} + */ + _getReferenceDomain: function () { + if (Object.hasOwn(this.attrs, "domain_map")) { + const state = (this.getParent() && this.getParent().state) || {}; + const domain_map = py.eval(this.attrs.domain_map, state.evalContext); + if (Object.hasOwn(domain_map, this.field.relation)) { + return domain_map[this.field.relation]; + } + } + return []; + }, + + /** + * @override + */ + _search: async function () { + // See why this is necessary in the 'basic_model.js' file + if (this.isReferenceField) { + this.recordParams.referenceDomain = this._getReferenceDomain(); + } + return this._super(...arguments); + }, + + /** + * @override + */ + _getSearchCreatePopupOptions: function () { + const res = this._super(...arguments); + if (this.isReferenceField) { + res.domain = this._getReferenceDomain(); + } + return res; + }, + }); +}); diff --git a/web_field_reference_domain/templates/assets.xml b/web_field_reference_domain/templates/assets.xml new file mode 100644 index 000000000000..fe0b63bc661c --- /dev/null +++ b/web_field_reference_domain/templates/assets.xml @@ -0,0 +1,21 @@ + + + +