From f5bfa3d3b653c8ec8a9a1820eeff995a7f954ca9 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 21 Apr 2017 08:48:35 +0200 Subject: [PATCH] [FIX] web: FormView: don't modify record's context Before this rev., some keys were added to the context of a record if there were x2many fields in the form view with a context defined on their node (containing *_view_ref keys, specifying the fields_view to load to display the relational data). Those keys should not be added to the main record's context, and it may cause errors (e.g. click on the 'Procurements' stat button in the Product form view). --- .../web/static/src/js/chrome/view_manager.js | 2 +- .../web/static/src/js/views/abstract_view.js | 2 ++ .../web/static/src/js/views/form/form_view.js | 8 +++---- addons/web/static/tests/views/form_tests.js | 23 +++++++++++++++---- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/addons/web/static/src/js/chrome/view_manager.js b/addons/web/static/src/js/chrome/view_manager.js index 978271d9228..502f1f2c5b6 100644 --- a/addons/web/static/src/js/chrome/view_manager.js +++ b/addons/web/static/src/js/chrome/view_manager.js @@ -354,7 +354,7 @@ var ViewManager = Widget.extend(ControlPanelMixin, { var self = this; var arch = view.fields_view.arch; var View = this.registry.get(arch.attrs.js_class || view.type); - var params = _.extend({}, view_options); + var params = _.extend({}, view_options, {userContext: this.getSession().user_context}); if (view.type === "form" && ((this.action.target === 'new' || this.action.target === 'inline') || (view_options && view_options.mode === 'edit'))) { params.mode = params.initial_mode || 'edit'; diff --git a/addons/web/static/src/js/views/abstract_view.js b/addons/web/static/src/js/views/abstract_view.js index e1066e56e92..fdb029da6c3 100644 --- a/addons/web/static/src/js/views/abstract_view.js +++ b/addons/web/static/src/js/views/abstract_view.js @@ -102,6 +102,8 @@ var AbstractView = Class.extend({ if (params.modelName) { this.loadParams.modelName = params.modelName; } + + this.userContext = params.userContext; }, //-------------------------------------------------------------------------- diff --git a/addons/web/static/src/js/views/form/form_view.js b/addons/web/static/src/js/views/form/form_view.js index 2a1eb2b4da5..4fff1a74ee4 100644 --- a/addons/web/static/src/js/views/form/form_view.js +++ b/addons/web/static/src/js/views/form/form_view.js @@ -64,16 +64,16 @@ var FormView = BasicView.extend({ attrs.limit = attrs.mode === "tree" ? 80 : 40; if (attrs.Widget.prototype.useSubview && !(attrs.invisible && JSON.parse(attrs.invisible)) && !attrs.views[attrs.mode]) { + var context = {}; var regex = /'([a-z]*_view_ref)' *: *'(.*?)'/g; var matches; while (matches = regex.exec(attrs.context)) { - self.loadParams.context[matches[1]] = matches[2]; + context[matches[1]] = matches[2]; } defs.push(parent.loadViews( field.relation, - new Context(self.loadParams.context), - [[null, attrs.mode === 'tree' ? 'list' : attrs.mode]], - {}) + new Context(context, self.userContext), + [[null, attrs.mode === 'tree' ? 'list' : attrs.mode]]) .then(function (views) { for (var viewName in views) { attrs.views[viewName] = views[viewName]; diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index c780d2afa6f..8fdd76934b7 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -3084,7 +3084,7 @@ QUnit.module('Views', { QUnit.test('*_view_ref in context are passed correctly', function (assert) { var done = assert.async(); - assert.expect(1); + assert.expect(4); createAsyncView({ View: FormView, @@ -3095,15 +3095,30 @@ QUnit.module('Views', { '' + '' + '', + res_id: 1, intercepts: { load_views: function (event) { - assert.strictEqual(event.data.context.eval().tree_view_ref, - 'module.tree_view_ref', + var context = event.data.context.eval(); + assert.strictEqual(context.tree_view_ref, 'module.tree_view_ref', "context should contain tree_view_ref"); + assert.notOk('some_context' in context, + "should not send record's context to load p's fields_view"); event.data.on_success(); } - } + }, + viewOptions: { + context: {some_context: false}, + }, + mockRPC: function (route, args) { + if (args.method === 'read') { + assert.deepEqual(args.kwargs.context, {some_context: false}, + "record's context shouldn't have been modified"); + } + return this._super.apply(this, arguments); + }, }).then(function (form) { + // reload to check that the record's context hasn't been modified + form.reload(); form.destroy(); done(); });