From 1dcf1acd3b8468d77c394d7d48b60de532d0d06c Mon Sep 17 00:00:00 2001 From: Pierre Rousseau Date: Wed, 1 Sep 2021 08:50:40 +0000 Subject: [PATCH] [REF] web: Allow to get ir.filter values without creating the record In the spreadsheet app, we need the exact same values which are saved for "ir.filter" records. This commit refactor a bit things to allows getting those values without actually creating the "ir.filter" record. Task 2572184 Part-of: odoo/odoo#75859 Co-authored-by: LucasLefevre --- .../control_panel_model_extension.js | 91 ++++++++++--------- .../control_panel_model_extension_tests.js | 28 ++++++ 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js b/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js index da93ad344b7..08e54983eee 100644 --- a/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js +++ b/addons/web/static/src/legacy/js/control_panel/control_panel_model_extension.js @@ -199,6 +199,7 @@ odoo.define("web/static/src/js/control_panel/control_panel_model_extension.js", case "groupBy": return this.getGroupBy(); case "orderedBy": return this.getOrderedBy(); case "timeRanges": return this.getTimeRanges(); + case "irFilterValues": return this.getIrFilterValues({ type: "favorite" }); } } @@ -260,6 +261,52 @@ odoo.define("web/static/src/js/control_panel/control_panel_model_extension.js", this.state.query = []; } + getIrFilterValues(preFilter) { + const groups = this._getGroups(); + + const userContext = this.env.session.user_context; + let controllerQueryParams; + this.config.trigger("get-controller-query-params", params => { + controllerQueryParams = params; + }); + controllerQueryParams = controllerQueryParams || {}; + controllerQueryParams.context = controllerQueryParams.context || {}; + + const queryContext = this._getContext(groups); + const context = pyUtils.eval( + 'contexts', + [userContext, controllerQueryParams.context, queryContext] + ); + for (const key in userContext) { + delete context[key]; + } + + const requireEvaluation = false; + const domain = this._getDomain(groups); + const groupBys = this._getGroupBy(groups); + const timeRanges = this._getTimeRanges(requireEvaluation); + const orderedBy = controllerQueryParams.orderedBy ? + controllerQueryParams.orderedBy : + (this._getOrderedBy(groups) || []); + + const userId = preFilter.isShared ? false : this.env.session.uid; + delete preFilter.isShared; + + Object.assign(preFilter, { + context, + domain, + groupBys, + groupNumber: userId ? FAVORITE_PRIVATE_GROUP : FAVORITE_SHARED_GROUP, + orderedBy, + removable: true, + userId, + }); + if (timeRanges) { + preFilter.comparison = timeRanges; + } + return this._favoriteToIrFilter(preFilter); + } + /** * Create a new filter of type 'favorite' and activate it. * A new group containing only that filter is created. @@ -1580,49 +1627,7 @@ odoo.define("web/static/src/js/control_panel/control_panel_model_extension.js", * @returns {Promise} */ async _saveQuery(preFilter) { - const groups = this._getGroups(); - - const userContext = this.env.session.user_context; - let controllerQueryParams; - this.config.trigger("get-controller-query-params", params => { - controllerQueryParams = params; - }); - controllerQueryParams = controllerQueryParams || {}; - controllerQueryParams.context = controllerQueryParams.context || {}; - - const queryContext = this._getContext(groups); - const context = pyUtils.eval( - 'contexts', - [userContext, controllerQueryParams.context, queryContext] - ); - for (const key in userContext) { - delete context[key]; - } - - const requireEvaluation = false; - const domain = this._getDomain(groups); - const groupBys = this._getGroupBy(groups); - const timeRanges = this._getTimeRanges(requireEvaluation); - const orderedBy = controllerQueryParams.orderedBy ? - controllerQueryParams.orderedBy : - (this._getOrderedBy(groups) || []); - - const userId = preFilter.isShared ? false : this.env.session.uid; - delete preFilter.isShared; - - Object.assign(preFilter, { - context, - domain, - groupBys, - groupNumber: userId ? FAVORITE_PRIVATE_GROUP : FAVORITE_SHARED_GROUP, - orderedBy, - removable: true, - userId, - }); - if (timeRanges) { - preFilter.comparison = timeRanges; - } - const irFilter = this._favoriteToIrFilter(preFilter); + const irFilter = this.getIrFilterValues(preFilter); const serverSideId = await this.env.dataManager.create_filter(irFilter); preFilter.serverSideId = serverSideId; diff --git a/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js b/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js index 487414b678e..8c64acaa43f 100644 --- a/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js +++ b/addons/web/static/tests/legacy/control_panel/control_panel_model_extension_tests.js @@ -328,6 +328,34 @@ odoo.define("web/static/tests/control_panel/control_panel_model_extension_tests. }); + QUnit.test('ir.filter values', async function (assert) { + assert.expect(1); + + const context = { + search_default_filter: true, + search_default_bar: 0, + search_default_groupby: 2, + }; + const arch = ` + + + + + `; + const fields = this.fields; + const model = createModel({ arch, fields, context }); + assert.deepEqual(model.get("irFilterValues"), { + action_id: undefined, + context: { + group_by: ["foo"], + }, + domain: '[["foo", "=", "hello"]]', + model_id: undefined, + sort: "[]", + user_id: undefined + }); + }); + QUnit.test('falsy search defaults are not activated', async function (assert) { assert.expect(1);