From 7b1dd79f4647761e16e16163c839c23b5620a0cb Mon Sep 17 00:00:00 2001 From: "Pierre Pulinckx (pipu)" Date: Tue, 7 Nov 2023 13:18:09 +0100 Subject: [PATCH] [FIX] web: localize date/datetime in domain descriptions The date/datetime values produced in the domain selector for date/datetime fields are not localized (like they are in the date picker) when presented in : - the domain selector in readonly mode - the facets edited/created from the domain selector dialog Here we make sure that the localization parameters are used when we display date/datetime values in the domain descriptions. closes odoo/odoo#141304 X-original-commit: bd1f5dc017a2549889002d5ff5382cbd3be6580b Signed-off-by: Mathieu Duckerts-Antoine (dam) --- .../web/static/src/core/tree_editor/utils.js | 9 +++ .../tests/core/domain_selector_tests.js | 57 +++++++++++++++---- .../static/tests/search/search_bar_tests.js | 2 +- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/addons/web/static/src/core/tree_editor/utils.js b/addons/web/static/src/core/tree_editor/utils.js index 66192134588..93a34b95abb 100644 --- a/addons/web/static/src/core/tree_editor/utils.js +++ b/addons/web/static/src/core/tree_editor/utils.js @@ -5,6 +5,7 @@ import { getOperatorLabel } from "@web/core/tree_editor/tree_editor_operator_edi import { Expression } from "@web/core/tree_editor/condition_tree"; import { useService } from "@web/core/utils/hooks"; import { _t } from "@web/core/l10n/translation"; +import { deserializeDate, deserializeDateTime, formatDate, formatDateTime } from "../l10n/dates"; /** * @param {import("@web/core/tree_editor/condition_tree").Value} val @@ -30,6 +31,14 @@ function formatValue(val, disambiguate, fieldDef, displayNames) { val = label; } } + if (typeof val === "string") { + if (fieldDef?.type === "datetime") { + return formatDateTime(deserializeDateTime(val)); + } + if (fieldDef?.type === "date") { + return formatDate(deserializeDate(val)); + } + } if (disambiguate && typeof val === "string") { return JSON.stringify(val); } diff --git a/addons/web/static/tests/core/domain_selector_tests.js b/addons/web/static/tests/core/domain_selector_tests.js index c44723438ad..342a652133e 100644 --- a/addons/web/static/tests/core/domain_selector_tests.js +++ b/addons/web/static/tests/core/domain_selector_tests.js @@ -75,6 +75,7 @@ export const SELECTORS = { debugArea: ".o_domain_selector_debug_container textarea", resetButton: ".o_domain_selector_row > button", }; +import { localization } from "@web/core/l10n/localization"; let serverData; let target; @@ -1223,6 +1224,9 @@ QUnit.module("Components", (hooks) => { }); QUnit.test("support properties (mode readonly)", async (assert) => { + patchWithCleanup(localization, { + dateFormat: `dd|MM|yyyy`, + }); serverData.models.partner.fields.properties = { string: "Properties", type: "properties", @@ -1271,7 +1275,7 @@ QUnit.module("Components", (hooks) => { }, { domain: `[("properties.xphone_prop_5", "=", "2023-10-05")]`, - result: `PropertiesDate = 2023-10-05`, + result: `PropertiesDate = 05|10|2023`, }, { domain: `[("properties.xphone_prop_6", "in", "g")]`, @@ -1509,6 +1513,9 @@ QUnit.module("Components", (hooks) => { }); QUnit.test("date field (readonly)", async (assert) => { + patchWithCleanup(localization, { + dateFormat: `dd|MM|yyyy`, + }); const parent = await makeDomainSelector({ readonly: true, domain: `[]`, @@ -1516,20 +1523,20 @@ QUnit.module("Components", (hooks) => { const toTest = [ { domain: `[("date", "=", False)]`, text: `Date = false` }, { domain: `[("date", "!=", False)]`, text: `Date != false` }, - { domain: `[("date", "=", "2023-07-03")]`, text: `Date = 2023-07-03` }, + { domain: `[("date", "=", "2023-07-03")]`, text: `Date = 03|07|2023` }, { domain: `[("date", "=", context_today())]`, text: `Date = context_today()` }, - { domain: `[("date", "!=", "2023-07-03")]`, text: `Date != 2023-07-03` }, - { domain: `[("date", "<", "2023-07-03")]`, text: `Date < 2023-07-03` }, - { domain: `[("date", "<=", "2023-07-03")]`, text: `Date <= 2023-07-03` }, - { domain: `[("date", ">", "2023-07-03")]`, text: `Date > 2023-07-03` }, - { domain: `[("date", ">=", "2023-07-03")]`, text: `Date >= 2023-07-03` }, + { domain: `[("date", "!=", "2023-07-03")]`, text: `Date != 03|07|2023` }, + { domain: `[("date", "<", "2023-07-03")]`, text: `Date < 03|07|2023` }, + { domain: `[("date", "<=", "2023-07-03")]`, text: `Date <= 03|07|2023` }, + { domain: `[("date", ">", "2023-07-03")]`, text: `Date > 03|07|2023` }, + { domain: `[("date", ">=", "2023-07-03")]`, text: `Date >= 03|07|2023` }, { domain: `["&", ("date", ">=", "2023-07-03"),("date","<=", "2023-07-15")]`, - text: `Date is between 2023-07-03 and 2023-07-15`, + text: `Date is between 03|07|2023 and 15|07|2023`, }, { domain: `["&", ("date", ">=", "2023-07-03"),("date","<=", context_today())]`, - text: `Date is between "2023-07-03" and context_today()`, + text: `Date is between 03|07|2023 and context_today()`, }, ]; for (const { domain, text } of toTest) { @@ -1796,7 +1803,7 @@ QUnit.module("Components", (hooks) => { }); QUnit.test("many2one field on record with falsy display_name", async (assert) => { - serverData.models.product.records[0].display_name = false + serverData.models.product.records[0].display_name = false; patchWithCleanup(browser, { setTimeout: (fn) => fn() }); await makeDomainSelector({ @@ -2201,4 +2208,34 @@ QUnit.module("Components", (hooks) => { assert.deepEqual(getOperatorOptions(target, -1), ["="]); assert.deepEqual(getValueOptions(target, -1), ["1"]); }); + + QUnit.test("datetime domain in readonly mode (check localization)", async (assert) => { + patchWithCleanup(localization, { + dateTimeFormat: "MM.dd.yyyy HH:mm:ss", + }); + patchTimeZone(120); + await makeDomainSelector({ + domain: `["&", ("datetime", ">=", "2023-11-03 11:41:23"), ("datetime", "<=", "2023-11-13 09:45:11")]`, + readonly: true, + }); + assert.strictEqual( + target.querySelector(".o_tree_editor_condition").textContent, + `Date Timeis between11.03.2023 13:41:23 and 11.13.2023 11:45:11` + ); + }); + + QUnit.test("date domain in readonly mode (check localization)", async (assert) => { + patchWithCleanup(localization, { + dateFormat: `dd|MM|yyyy`, + }); + patchTimeZone(120); + await makeDomainSelector({ + domain: `["&", ("date", ">=", "2023-11-03"), ("date", "<=", "2023-11-13")]`, + readonly: true, + }); + assert.strictEqual( + target.querySelector(".o_tree_editor_condition").textContent, + `Dateis between03|11|2023 and 13|11|2023` + ); + }); }); diff --git a/addons/web/static/tests/search/search_bar_tests.js b/addons/web/static/tests/search/search_bar_tests.js index 6b4152d4a41..ec740076783 100644 --- a/addons/web/static/tests/search/search_bar_tests.js +++ b/addons/web/static/tests/search/search_bar_tests.js @@ -1628,7 +1628,7 @@ QUnit.module("Search", (hooks) => { await click(target.querySelector(".modal footer button")); assert.containsNone(target, ".modal"); - assert.deepEqual(getFacetTexts(target), [`Birthday is between 2023-04-01 and 2023-04-30`]); + assert.deepEqual(getFacetTexts(target), [`Birthday is between 04/01/2023 and 04/30/2023`]); }); QUnit.test("edit a field", async function (assert) {