From c120f439050eafca2750ea7d796b310ebc0a8d67 Mon Sep 17 00:00:00 2001 From: Achraf Date: Thu, 22 Sep 2022 14:22:43 +0000 Subject: [PATCH] [FIX] web: Allow users to enable list view with group_by_no_leaf - Go to accounting/Report/Invoices Analysis - Open studio - enable list view - empty view This code bellow compute count_field as `__count` because group_by_no_leaf is in the context. But in the basic_model we don't consider this case. https://github.com/odoo/odoo/blob/5797fd80a63309269f15bcbe4948d4429a53eec2/odoo/models.py#L2798-L2802 opw-2980923 closes odoo/odoo#101570 X-original-commit: bde2985959ef8914da4b8aca96efc5772262d0c0 Signed-off-by: Aaron Bohy (aab) --- .../web/static/src/views/relational_model.js | 4 ++++ .../web/static/tests/helpers/mock_server.js | 8 ++++++- .../web/static/tests/views/list_view_tests.js | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/views/relational_model.js b/addons/web/static/src/views/relational_model.js index d0cb38c4bfa..c56d0064391 100644 --- a/addons/web/static/src/views/relational_model.js +++ b/addons/web/static/src/views/relational_model.js @@ -2401,6 +2401,10 @@ export class DynamicGroupList extends DynamicList { } break; } + // When group_by_no_leaf key is present FIELD_ID_count doesn't exist + // we have to get the count from `__count` instead + // see _read_group_raw in models.py + case `__count`: case `${groupByField.name}_count`: { groupParams.count = value; break; diff --git a/addons/web/static/tests/helpers/mock_server.js b/addons/web/static/tests/helpers/mock_server.js index cc7f8c35894..5f47c0986c5 100644 --- a/addons/web/static/tests/helpers/mock_server.js +++ b/addons/web/static/tests/helpers/mock_server.js @@ -1095,7 +1095,13 @@ export class MockServer { delete group.__range; } // compute count key to match dumb server logic... - const countKey = kwargs.lazy ? groupBy[0].split(":")[0] + "_count" : "__count"; + const groupByNoLeaf = kwargs.context ? "group_by_no_leaf" in kwargs.context : false; + let countKey; + if (kwargs.lazy && (groupBy.length >= 2 || !groupByNoLeaf)) { + countKey = groupBy[0].split(":")[0] + "_count"; + } else { + countKey = "__count"; + } group[countKey] = groupRecords.length; aggregateFields(group, groupRecords); readGroupResult.push(group); diff --git a/addons/web/static/tests/views/list_view_tests.js b/addons/web/static/tests/views/list_view_tests.js index ee3f5e77821..3c1aa37db7c 100644 --- a/addons/web/static/tests/views/list_view_tests.js +++ b/addons/web/static/tests/views/list_view_tests.js @@ -15151,4 +15151,28 @@ QUnit.module("Views", (hooks) => { await pagerPrevious(target); assert.deepEqual([...getPagerValue(target), getPagerLimit(target)], [1, 2]); }); + + QUnit.test("list with group_by_no_leaf and group by", async function (assert) { + assert.expect(4); + + await makeView({ + type: "list", + resModel: "foo", + serverData, + arch: '', + groupBy: ["currency_id"], + context: { group_by_no_leaf: true }, + }); + + const groups = target.querySelectorAll(".o_group_name"); + const groupsRecords = [...target.querySelectorAll(".o_data_row .o_data_cell")]; + assert.strictEqual(groups.length, 2, "There should be 2 groups"); + assert.strictEqual(groups[0].textContent, "USD (3) ", "Second group should have 3 records"); + assert.strictEqual(groups[1].textContent, "EUR (1) ", "First group should have 1 record"); + assert.deepEqual( + groupsRecords.map((groupEl) => groupEl.textContent), + ["blip", "gnap", "blip", "yop"], + "Groups should contains correct records" + ); + }); });