From 2febb8778f13ef7eafd56ab6e3366a039b55a8cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= <ged@odoo.com>
Date: Thu, 4 May 2017 21:05:58 +0200
Subject: [PATCH] [FIX] web: use action context in evaluation contexts

The process of evaluating a domain is not really difficult, but the hard
part is getting the evaluation context right.  The evaluation context is
supposed to contain informations coming from various sources (active id/ids,
session context, ...).  Before this commit, we just ignored the context coming
from the action.

This could cause many problems. For example, in the Inventory
application: create a picking, add 10 ice cream in initial demand, force
assign, click on scrap.  A form view should be opened (with the context
coming back from the button_scrap method).  In that formview, there is a
many2one Product.  Clicking on it should perform a name_search with a
domain looking like ['id', 'in', [64]].  This domain is the result of
the evaluation of "[('id', 'in', context.get('product_ids', []))]".
Before this commit, product_ids was ignored, so the domain was always
['id', 'in', []].

In this commit, we simply add the element context in the list of sources
when computing the evaluation context (the element context is the
context given to the view, which is the way an action give a context to
a view)

In short, handling context is hard...
---
 .../static/src/js/views/basic/basic_model.js  |  2 +-
 addons/web/static/tests/views/form_tests.js   | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js
index 64030cea866d..cde2d2479ac3 100644
--- a/addons/web/static/src/js/views/basic/basic_model.js
+++ b/addons/web/static/src/js/views/basic/basic_model.js
@@ -2002,7 +2002,7 @@ var BasicModel = AbstractModel.extend({
             }
             _.extend(evalContext, {parent: parent.data});
         }
-        return _.extend({}, session.user_context, evalContext);
+        return _.extend({}, session.user_context, element.context, evalContext);
     },
     /**
      * Returns the list of field names of the given element according to its
diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js
index d4ff7af9bb54..bedcf83b139f 100644
--- a/addons/web/static/tests/views/form_tests.js
+++ b/addons/web/static/tests/views/form_tests.js
@@ -4024,5 +4024,35 @@ QUnit.module('Views', {
 
         form.destroy();
     });
+
+    QUnit.test('action context is used when evaluating domains', function (assert) {
+        assert.expect(1);
+
+        var form = createView({
+            View: FormView,
+            model: 'partner',
+            data: this.data,
+            arch: '<form string="Partners">' +
+                    '<sheet>' +
+                        '<field name="trululu" domain="[(\'id\', \'in\', context.get(\'product_ids\', []))]"/>' +
+                    '</sheet>' +
+                '</form>',
+            res_id: 1,
+            viewOptions: {
+                context: {product_ids: [45,46,47]}
+            },
+            mockRPC: function (route, args) {
+                if (args.method === 'name_search') {
+                    assert.deepEqual(args.kwargs.args[0], ['id', 'in', [45,46,47]],
+                        "domain should be properly evaluated");
+                }
+                return this._super.apply(this, arguments);
+            },
+        });
+        form.$buttons.find('.o_form_button_edit').click();
+        form.$('div[name="trululu"] input').click();
+
+        form.destroy();
+    });
 });
 });
-- 
GitLab