diff --git a/web_widget_pattern/README.rst b/web_widget_pattern/README.rst new file mode 100644 index 000000000000..24b69de5b79d --- /dev/null +++ b/web_widget_pattern/README.rst @@ -0,0 +1,118 @@ +============== +Input patterns +============== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:049fefbca58070d1187e57e952f2eac13aa3aa4f636ca6c84e2ec8e67f4cd125 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |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%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/18.0/web_widget_pattern + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-18-0/web-18-0-web_widget_pattern + :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=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to use +`patterns `__ +in text fields in the backend. + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +You can either define a pattern in your Python source code on a field as +in + +.. code:: python + + my_field = fields.Char(pattern='[0-9]') + +or on view level as in + +.. code:: xml + + + +Demo data adds a pattern on the street2 field on partner forms to only +allow numbers. + +Known issues / Roadmap +====================== + +- allow to optionally validate fields against its pattern server side +- support text, url, email, phone, ... fields +- support using a pattern from a computed field + +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 +------- + +* Hunki Enterprises BV + +Contributors +------------ + +- Holger Brunn + (https://hunki-enteprises.com) + +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-hbrunn| image:: https://github.com/hbrunn.png?size=40px + :target: https://github.com/hbrunn + :alt: hbrunn + +Current `maintainer `__: + +|maintainer-hbrunn| + +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_widget_pattern/__init__.py b/web_widget_pattern/__init__.py new file mode 100644 index 000000000000..0650744f6bc6 --- /dev/null +++ b/web_widget_pattern/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/web_widget_pattern/__manifest__.py b/web_widget_pattern/__manifest__.py new file mode 100644 index 000000000000..0742753df691 --- /dev/null +++ b/web_widget_pattern/__manifest__.py @@ -0,0 +1,30 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +{ + "name": "Input patterns", + "summary": "Allows to define a regex for validating input on the backend", + "version": "18.0.1.0.0", + "development_status": "Alpha", + "category": "Technical", + "website": "https://github.com/OCA/web", + "author": "Hunki Enterprises BV, Odoo Community Association (OCA)", + "maintainers": ["hbrunn"], + "license": "AGPL-3", + "depends": [ + "web", + ], + "data": [], + "demo": [ + "demo/res_partner_views.xml", + ], + "assets": { + "web.assets_backend": [ + "web_widget_pattern/static/src/*.xml", + "web_widget_pattern/static/src/*.esm.js", + ], + "web.assets_unit_tests": [ + "web_widget_pattern/static/tests/*", + ], + }, +} diff --git a/web_widget_pattern/demo/res_partner_views.xml b/web_widget_pattern/demo/res_partner_views.xml new file mode 100644 index 000000000000..becb50e84a0b --- /dev/null +++ b/web_widget_pattern/demo/res_partner_views.xml @@ -0,0 +1,14 @@ + + + + + res.partner + + + + [0-9]+ + + + + diff --git a/web_widget_pattern/i18n/it.po b/web_widget_pattern/i18n/it.po new file mode 100644 index 000000000000..05893cedbe63 --- /dev/null +++ b/web_widget_pattern/i18n/it.po @@ -0,0 +1,22 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_widget_pattern +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-05-20 13:38+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: web_widget_pattern +#: model:ir.model,name:web_widget_pattern.model_base +msgid "Base" +msgstr "Base" diff --git a/web_widget_pattern/i18n/web_widget_pattern.pot b/web_widget_pattern/i18n/web_widget_pattern.pot new file mode 100644 index 000000000000..d82ead953139 --- /dev/null +++ b/web_widget_pattern/i18n/web_widget_pattern.pot @@ -0,0 +1,19 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_widget_pattern +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: web_widget_pattern +#: model:ir.model,name:web_widget_pattern.model_base +msgid "Base" +msgstr "" diff --git a/web_widget_pattern/models/__init__.py b/web_widget_pattern/models/__init__.py new file mode 100644 index 000000000000..0e44449338cf --- /dev/null +++ b/web_widget_pattern/models/__init__.py @@ -0,0 +1 @@ +from . import base diff --git a/web_widget_pattern/models/base.py b/web_widget_pattern/models/base.py new file mode 100644 index 000000000000..234702127a18 --- /dev/null +++ b/web_widget_pattern/models/base.py @@ -0,0 +1,28 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from odoo import api, fields, models + + +class Base(models.AbstractModel): + _inherit = "base" + + def _valid_field_parameter(self, field, name): + return super()._valid_field_parameter(field, name) or ( + name == "pattern" and isinstance(field, fields.Char) + ) + + @api.model + def _get_view_field_attributes(self): + return super()._get_view_field_attributes() + ["pattern"] + + @api.model + def fields_get(self, allfields=None, attributes=None): + result = super().fields_get(allfields=allfields, attributes=attributes) + if not attributes or "pattern" in attributes: + for field_name, description in result.items(): + field = self._fields.get(field_name) + pattern = getattr(field, "pattern", None) + if pattern is not None: + description["pattern"] = pattern + return result diff --git a/web_widget_pattern/pyproject.toml b/web_widget_pattern/pyproject.toml new file mode 100644 index 000000000000..4231d0cccb3d --- /dev/null +++ b/web_widget_pattern/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/web_widget_pattern/readme/CONTRIBUTORS.md b/web_widget_pattern/readme/CONTRIBUTORS.md new file mode 100644 index 000000000000..f94e7fa07c4e --- /dev/null +++ b/web_widget_pattern/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Holger Brunn \ (https://hunki-enteprises.com) diff --git a/web_widget_pattern/readme/DESCRIPTION.md b/web_widget_pattern/readme/DESCRIPTION.md new file mode 100644 index 000000000000..8aadddc78649 --- /dev/null +++ b/web_widget_pattern/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module allows to use [patterns](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern) in text fields in the backend. diff --git a/web_widget_pattern/readme/ROADMAP.md b/web_widget_pattern/readme/ROADMAP.md new file mode 100644 index 000000000000..83f2056eb8fb --- /dev/null +++ b/web_widget_pattern/readme/ROADMAP.md @@ -0,0 +1,3 @@ +- allow to optionally validate fields against its pattern server side +- support text, url, email, phone, ... fields +- support using a pattern from a computed field diff --git a/web_widget_pattern/readme/USAGE.md b/web_widget_pattern/readme/USAGE.md new file mode 100644 index 000000000000..e3446b818064 --- /dev/null +++ b/web_widget_pattern/readme/USAGE.md @@ -0,0 +1,13 @@ +You can either define a pattern in your Python source code on a field as in + +```python + my_field = fields.Char(pattern='[0-9]') +``` + +or on view level as in + +```xml + +``` + +Demo data adds a pattern on the street2 field on partner forms to only allow numbers. diff --git a/web_widget_pattern/static/description/icon.png b/web_widget_pattern/static/description/icon.png new file mode 100644 index 000000000000..3a0328b516c4 Binary files /dev/null and b/web_widget_pattern/static/description/icon.png differ diff --git a/web_widget_pattern/static/description/index.html b/web_widget_pattern/static/description/index.html new file mode 100644 index 000000000000..60368d2069ce --- /dev/null +++ b/web_widget_pattern/static/description/index.html @@ -0,0 +1,458 @@ + + + + + +Input patterns + + + +
+

Input patterns

+ + +

Alpha License: AGPL-3 OCA/web Translate me on Weblate Try me on Runboat

+

This module allows to use +patterns +in text fields in the backend.

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Usage

+

You can either define a pattern in your Python source code on a field as +in

+
+my_field = fields.Char(pattern='[0-9]')
+
+

or on view level as in

+
+<field name="my_field" pattern="[0-9]" />
+
+

Demo data adds a pattern on the street2 field on partner forms to only +allow numbers.

+
+
+

Known issues / Roadmap

+
    +
  • allow to optionally validate fields against its pattern server side
  • +
  • support text, url, email, phone, … fields
  • +
  • support using a pattern from a computed field
  • +
+
+
+

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

+
    +
  • Hunki Enterprises BV
  • +
+
+ +
+

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.

+

Current maintainer:

+

hbrunn

+

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_widget_pattern/static/src/char_field.esm.js b/web_widget_pattern/static/src/char_field.esm.js new file mode 100644 index 000000000000..03aae791c83f --- /dev/null +++ b/web_widget_pattern/static/src/char_field.esm.js @@ -0,0 +1,43 @@ +import {CharField, charField} from "@web/views/fields/char/char_field"; +import {Field} from "@web/views/fields/field"; +import {_t} from "@web/core/l10n/translation"; +import {patch} from "@web/core/utils/patch"; + +patch(CharField.prototype, { + parse(value) { + const result = super.parse(...arguments); + const pattern = this.props.pattern; + if (result && pattern) { + const regex = new RegExp(pattern, "v"); + const match = regex.exec(result); + if (!match || match[0] !== value) { + throw new Error( + _t("%s does not match required pattern %s", value, pattern) + ); + } + } + return result; + }, +}); + +patch(charField, { + extractProps(fieldInfo) { + return Object.assign(super.extractProps(fieldInfo), { + pattern: fieldInfo.pattern, + }); + }, +}); + +patch(Field, { + parseFieldNode(node, models, modelName) { + const fieldInfo = super.parseFieldNode(...arguments); + fieldInfo.pattern = + (node.attributes.pattern && node.attributes.pattern.value) || + models[modelName].fields[fieldInfo.name].pattern; + return fieldInfo; + }, +}); + +patch(CharField.props, { + pattern: {type: String, optional: true}, +}); diff --git a/web_widget_pattern/static/src/char_field.xml b/web_widget_pattern/static/src/char_field.xml new file mode 100644 index 000000000000..865e14df76bc --- /dev/null +++ b/web_widget_pattern/static/src/char_field.xml @@ -0,0 +1,9 @@ + + + + + + props.pattern + + + diff --git a/web_widget_pattern/static/tests/web_widget_pattern.test.js b/web_widget_pattern/static/tests/web_widget_pattern.test.js new file mode 100644 index 000000000000..fe836ba433d2 --- /dev/null +++ b/web_widget_pattern/static/tests/web_widget_pattern.test.js @@ -0,0 +1,60 @@ +import {defineModels, fields, models, mountView} from "@web/../tests/web_test_helpers"; +import {describe, expect, test} from "@odoo/hoot"; + +describe.current.tags("desktop"); + +class Main extends models.Model { + char_field_without_pattern = fields.Char({ + string: "Char field 1", + }); + char_field_with_pattern = fields.Char({ + string: "Char field 2", + pattern: "[0-9]", + }); + + _records = [{id: 1}]; +} + +defineModels([Main]); + +describe("WebWidgetPattern", () => { + test("fields without pattern don't render any, those with do", async () => { + await mountView({ + type: "form", + resModel: "main", + resId: 1, + arch: ` +
+ + + `, + }); + expect("div[name='char_field_without_pattern'] input").not.toHaveAttribute( + "pattern" + ); + expect("div[name='char_field_with_pattern'] input").toHaveAttribute( + "pattern", + "[0-9]" + ); + }); + test("server side pattern can be overridden in view", async () => { + await mountView({ + type: "form", + resModel: "main", + resId: 1, + arch: ` +
+ + + `, + }); + expect("div[name='char_field_without_pattern'] input").toHaveAttribute( + "pattern", + "[a-z]" + ); + expect("div[name='char_field_with_pattern'] input").toHaveAttribute( + "pattern", + "[A-Z]" + ); + }); +}); diff --git a/web_widget_pattern/tests/__init__.py b/web_widget_pattern/tests/__init__.py new file mode 100644 index 000000000000..5dad2ba9da1b --- /dev/null +++ b/web_widget_pattern/tests/__init__.py @@ -0,0 +1 @@ +from . import test_web_widget_pattern diff --git a/web_widget_pattern/tests/test_web_widget_pattern.py b/web_widget_pattern/tests/test_web_widget_pattern.py new file mode 100644 index 000000000000..222eb9d61895 --- /dev/null +++ b/web_widget_pattern/tests/test_web_widget_pattern.py @@ -0,0 +1,27 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from odoo.tests.common import HttpCase, TransactionCase, tagged + + +class TestWebWidgetPattern(TransactionCase): + def test_fields_get(self): + ResPartner = self.env["res.partner"] + ResPartner._fields["street2"].pattern = "[0-9]" + field_description = ResPartner.get_views( + [(self.env.ref("base.view_partner_form").id, "form")] + )["models"]["res.partner"]["fields"]["street2"] + self.assertEqual(field_description["pattern"], "[0-9]") + + +@tagged("post_install", "-at_install") +class TestWebWidgetPatternHoot(HttpCase): + def test_js(self): + self.browser_js( + "/web/tests?headless&loglevel=2&preset=desktop&filter=WebWidgetPattern", + "", + "", + login="admin", + success_signal="[HOOT] Test suite succeeded", + error_checker=lambda x: "[HOOT]" not in x, + )