diff --git a/setup/web_field_numeric_formatting/odoo/addons/web_field_numeric_formatting b/setup/web_field_numeric_formatting/odoo/addons/web_field_numeric_formatting new file mode 120000 index 000000000..6e4ef5777 --- /dev/null +++ b/setup/web_field_numeric_formatting/odoo/addons/web_field_numeric_formatting @@ -0,0 +1 @@ +../../../../web_field_numeric_formatting \ No newline at end of file diff --git a/setup/web_field_numeric_formatting/setup.py b/setup/web_field_numeric_formatting/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/web_field_numeric_formatting/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_field_numeric_formatting/README.rst b/web_field_numeric_formatting/README.rst new file mode 100644 index 000000000..d3265be2b --- /dev/null +++ b/web_field_numeric_formatting/README.rst @@ -0,0 +1,97 @@ +============================ +Web Numeric Field Formatting +============================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:cb7b76d33925ee3e34193bf160ca27ef2edfe4c2558f65a8b715fb6b911be118 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/16.0/web_field_numeric_formatting + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_field_numeric_formatting + :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=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Having this module installed allows a developer to render a float or integer field +without a thousands separator using + +.. code-block:: xml + + + +Setting this option is supported for list and form views. In kanban views you +can simply use + +.. code-block:: xml + + + +The code in this module is a backport of +https://github.com/odoo/odoo/commit/592794b397ba7f6468e45a5cc3e93681b1ed8578 +from Odoo 17, so this module should not be ported beyond version 16. + +The new `enable_formatting` option in Odoo 17 replaces the `format` option in +Odoo 15. + +**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 +~~~~~~~ + +* Opener B.V. + +Contributors +~~~~~~~~~~~~ + +* Lucas Perais (lpe) contributed the original code to Odoo. +* Stefan Rijnhart created the backport to Odoo 16 + in this module. + +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_numeric_formatting/__init__.py b/web_field_numeric_formatting/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/web_field_numeric_formatting/__manifest__.py b/web_field_numeric_formatting/__manifest__.py new file mode 100644 index 000000000..5e36e4094 --- /dev/null +++ b/web_field_numeric_formatting/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2023 Odoo S.A. +# License LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0) +{ + "name": "Web Numeric Field Formatting", + "summary": "Allow to render float and integer fields without thousands separator", + "category": "web", + "version": "16.0.1.0.0", + "author": "Opener B.V., Odoo Community Association (OCA)", + "website": "https://github.com/OCA/web", + "depends": ["web"], + "assets": { + "web.assets_backend": [ + "web_field_numeric_formatting/static/src/components/float_field.esm.js", + "web_field_numeric_formatting/static/src/components/integer_field.esm.js", + "web_field_numeric_formatting/static/src/components/list_renderer.esm.js", + ], + "web.qunit_suite_tests": [ + "web_field_numeric_formatting/static/tests/field_tests.esm.js", + ], + }, + "license": "LGPL-3", + "auto_install": False, + "installable": True, +} diff --git a/web_field_numeric_formatting/readme/CONTRIBUTORS.rst b/web_field_numeric_formatting/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..5b7201705 --- /dev/null +++ b/web_field_numeric_formatting/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* Lucas Perais (lpe) contributed the original code to Odoo. +* Stefan Rijnhart created the backport to Odoo 16 + in this module. diff --git a/web_field_numeric_formatting/readme/DESCRIPTION.rst b/web_field_numeric_formatting/readme/DESCRIPTION.rst new file mode 100644 index 000000000..138683eb5 --- /dev/null +++ b/web_field_numeric_formatting/readme/DESCRIPTION.rst @@ -0,0 +1,20 @@ +Having this module installed allows a developer to render a float or integer field +without a thousands separator using + +.. code-block:: xml + + + +Setting this option is supported for list and form views. In kanban views you +can simply use + +.. code-block:: xml + + + +The code in this module is a backport of +https://github.com/odoo/odoo/commit/592794b397ba7f6468e45a5cc3e93681b1ed8578 +from Odoo 17, so this module should not be ported beyond version 16. + +The new `enable_formatting` option in Odoo 17 replaces the `format` option in +Odoo 15. diff --git a/web_field_numeric_formatting/static/description/index.html b/web_field_numeric_formatting/static/description/index.html new file mode 100644 index 000000000..c9f38a4cb --- /dev/null +++ b/web_field_numeric_formatting/static/description/index.html @@ -0,0 +1,437 @@ + + + + + + +Web Numeric Field Formatting + + + +
+

Web Numeric Field Formatting

+ + +

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

+

Having this module installed allows a developer to render a float or integer field +without a thousands separator using

+
+<field name="id" options="{'enable_formatting': false}" />
+
+

Setting this option is supported for list and form views. In kanban views you +can simply use

+
+<t t-esc="record.id.raw_value" />
+
+

The code in this module is a backport of +https://github.com/odoo/odoo/commit/592794b397ba7f6468e45a5cc3e93681b1ed8578 +from Odoo 17, so this module should not be ported beyond version 16.

+

The new enable_formatting option in Odoo 17 replaces the format option in +Odoo 15.

+

Table of contents

+ +
+

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

+
    +
  • Opener B.V.
  • +
+
+
+

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_numeric_formatting/static/src/components/float_field.esm.js b/web_field_numeric_formatting/static/src/components/float_field.esm.js new file mode 100644 index 000000000..867b36287 --- /dev/null +++ b/web_field_numeric_formatting/static/src/components/float_field.esm.js @@ -0,0 +1,30 @@ +/* @odoo-module */ + +import {FloatField} from "@web/views/fields/float/float_field"; +import {patch} from "@web/core/utils/patch"; + +patch(FloatField.prototype, "web_field_numeric_formatting.FloatField", { + get formattedValue() { + if (!this.props.formatNumber) { + return this.props.value; + } + return this._super(...arguments); + }, +}); + +Object.assign(FloatField.props, { + formatNumber: {type: Boolean, optional: true}, +}); +Object.assign(FloatField.defaultProps, { + formatNumber: true, +}); +const superExtractProps = FloatField.extractProps; +FloatField.extractProps = ({attrs, field}) => { + return { + ...superExtractProps({attrs, field}), + formatNumber: + attrs.options.enable_formatting === undefined + ? true + : Boolean(attrs.options.enable_formatting), + }; +}; diff --git a/web_field_numeric_formatting/static/src/components/integer_field.esm.js b/web_field_numeric_formatting/static/src/components/integer_field.esm.js new file mode 100644 index 000000000..8e1386ebe --- /dev/null +++ b/web_field_numeric_formatting/static/src/components/integer_field.esm.js @@ -0,0 +1,30 @@ +/* @odoo-module */ + +import {IntegerField} from "@web/views/fields/integer/integer_field"; +import {patch} from "@web/core/utils/patch"; + +patch(IntegerField.prototype, "web_field_numeric_formatting.IntegerField", { + get formattedValue() { + if (!this.props.formatNumber) { + return this.props.value; + } + return this._super(...arguments); + }, +}); + +Object.assign(IntegerField.props, { + formatNumber: {type: Boolean, optional: true}, +}); +Object.assign(IntegerField.defaultProps, { + formatNumber: true, +}); +const superExtractProps = IntegerField.extractProps; +IntegerField.extractProps = ({attrs}) => { + return { + ...superExtractProps({attrs}), + formatNumber: + attrs.options.enable_formatting === undefined + ? true + : Boolean(attrs.options.enable_formatting), + }; +}; diff --git a/web_field_numeric_formatting/static/src/components/list_renderer.esm.js b/web_field_numeric_formatting/static/src/components/list_renderer.esm.js new file mode 100644 index 000000000..b72cfe933 --- /dev/null +++ b/web_field_numeric_formatting/static/src/components/list_renderer.esm.js @@ -0,0 +1,13 @@ +/* @odoo-module */ + +import {ListRenderer} from "@web/views/list/list_renderer"; +import {patch} from "@web/core/utils/patch"; + +patch(ListRenderer.prototype, "web_field_numeric_formatting.ListRenderer", { + getFormattedValue(column, record) { + if (column.options.enable_formatting === false) { + return record.data[column.name]; + } + return this._super(...arguments); + }, +}); diff --git a/web_field_numeric_formatting/static/tests/field_tests.esm.js b/web_field_numeric_formatting/static/tests/field_tests.esm.js new file mode 100644 index 000000000..7baa27fb0 --- /dev/null +++ b/web_field_numeric_formatting/static/tests/field_tests.esm.js @@ -0,0 +1,159 @@ +/* @odoo-module */ + +import { + click, + clickSave, + editInput, + getFixture, + patchWithCleanup, +} from "@web/../tests/helpers/utils"; +import { + defaultLocalization, + makeFakeLocalizationService, +} from "@web/../tests/helpers/mock_services"; +import {makeView, setupViewRegistries} from "@web/../tests/views/helpers"; +import {localization} from "@web/core/l10n/localization"; +import {registry} from "@web/core/registry"; + +const {QUnit} = window; + +QUnit.module("FieldNumericFormatting", (hooks) => { + let target = {}; + let serverData = {}; + hooks.beforeEach(() => { + target = getFixture(); + serverData = { + models: { + partner: { + fields: { + int_field: {string: "int_field", type: "integer"}, + float_field: {string: "Float field", type: "float"}, + }, + records: [ + {id: 1, int_field: 10, float_field: 0.36}, + {id: 3, int_field: 8069, float_field: 8069}, + ], + }, + }, + }; + setupViewRegistries(); + }); + QUnit.test( + "Float field with enable_formatting option as false", + async function (assert) { + registry.category("services").remove("localization"); + registry + .category("services") + .add( + "localization", + makeFakeLocalizationService({thousandsSep: ",", grouping: [3, 0]}) + ); + + await makeView({ + type: "form", + serverData, + resModel: "partner", + resId: 1, + arch: `
`, + }); + + assert.strictEqual( + target.querySelector(".o_field_widget input").value, + "0.36", + "Integer value must not be formatted" + ); + + await editInput( + target, + ".o_field_widget[name=float_field] input", + "123456.789" + ); + await clickSave(target); + assert.strictEqual( + target.querySelector(".o_field_widget input").value, + "123456.789", + "Integer value must be not formatted if input type is number." + ); + } + ); + QUnit.test( + "Float field with enable_formatting option as false in editable list view", + async function (assert) { + await makeView({ + serverData, + type: "list", + resModel: "partner", + arch: ` + + + `, + }); + + // Switch to edit mode + await click( + target.querySelector("tr.o_data_row td:not(.o_list_record_selector)") + ); + + assert.containsOnce( + target, + 'div[name="float_field"] input', + "The view should have 1 input for editable float." + ); + + await editInput( + target, + 'div[name="float_field"] input', + "108.2458938598598" + ); + assert.strictEqual( + target.querySelector('div[name="float_field"] input').value, + "108.2458938598598", + "The value should not be formatted on blur." + ); + + await editInput( + target, + 'div[name="float_field"] input', + "18.8958938598598" + ); + await click(target.querySelector(".o_list_button_save")); + assert.strictEqual( + target.querySelector(".o_field_widget").textContent, + "18.8958938598598", + "The new value should not be rounded as well." + ); + } + ); + QUnit.test( + "IntegerField with enable_formatting option as false", + async function (assert) { + patchWithCleanup(localization, {...defaultLocalization, grouping: [3, 0]}); + + await makeView({ + type: "form", + serverData, + resModel: "partner", + resId: 3, + arch: `
`, + }); + + assert.strictEqual( + target.querySelector(".o_field_widget input").value, + "8069", + "Integer value must not be formatted" + ); + + await editInput( + target, + ".o_field_widget[name=int_field] input", + "1234567890" + ); + await clickSave(target); + assert.strictEqual( + target.querySelector(".o_field_widget input").value, + "1234567890", + "Integer value must be not formatted if input type is number." + ); + } + ); +});