diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index f2fa63d38d3..36f766407b7 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1473,12 +1473,15 @@ instance.web.search.ManyToOneField = instance.web.search.CharField.extend({ }, complete: function (needle) { var self = this; - // TODO: context // FIXME: "concurrent" searches (multiple requests, mis-ordered responses) + var context = instance.web.pyeval.eval( + 'contexts', [this.view.dataset.get_context()]); return this.model.call('name_search', [], { name: needle, + args: instance.web.pyeval.eval( + 'domains', this.attrs.domain ? [this.attrs.domain] : [], context), limit: 8, - context: {} + context: context }).then(function (results) { if (_.isEmpty(results)) { return null; } return [{label: self.attrs.string}].concat( diff --git a/addons/web/static/test/search.js b/addons/web/static/test/search.js index 292954b1992..904af63f590 100644 --- a/addons/web/static/test/search.js +++ b/addons/web/static/test/search.js @@ -318,6 +318,28 @@ openerp.testing.section('defaults', { "facet value should match provided default's selection"); }); }); + test("M2O default: value array", {asserts: 2}, function (instance, $s, mock) { + var view = {inputs: []}, id = 5; + var f = new instance.web.search.ManyToOneField( + {attrs: {name: 'dummy', string: 'Dummy'}}, + {relation: 'dummy.model.name'}, + view); + mock('dummy.model.name:name_get', function (args) { + equal(args[0], id); + return [[id, "DumDumDum"]]; + }); + return f.facet_for_defaults({dummy: [id]}) + .done(function (facet) { + var model = facet; + if (!(model instanceof instance.web.search.Facet)) { + model = new instance.web.search.Facet(facet); + } + deepEqual( + model.values.toJSON(), + [{label: "DumDumDum", value: id}], + "should support default as a singleton"); + }); + }); test("M2O default: value", {asserts: 1}, function (instance, $s, mock) { var view = {inputs: []}, id = 4; var f = new instance.web.search.ManyToOneField( @@ -330,6 +352,15 @@ openerp.testing.section('defaults', { ok(!facet, "an invalid m2o default should yield a non-facet"); }); }); + test("M2O default: values", {rpc: false}, function (instance) { + var view = {inputs: []}; + var f = new instance.web.search.ManyToOneField( + {attrs: {name: 'dummy', string: 'Dummy'}}, + {relation: 'dummy.model.name'}, + view); + raises(function () { f.facet_for_defaults({dummy: [6, 7]}) }, + "should not accept multiple default values"); + }) }); openerp.testing.section('completions', { dependencies: ['web.search'], @@ -526,7 +557,7 @@ openerp.testing.section('completions', { return [[42, "choice 1"], [43, "choice @"]]; }); - var view = {inputs: []}; + var view = {inputs: [], dataset: {get_context: function () {}}}; var f = new instance.web.search.ManyToOneField( {attrs: {string: 'Dummy'}}, {relation: 'dummy.model'}, view); return f.complete("bob") @@ -555,7 +586,7 @@ openerp.testing.section('completions', { strictEqual(kwargs.name, 'bob'); return []; }); - var view = {inputs: []}; + var view = {inputs: [], dataset: {get_context: function () {}}}; var f = new instance.web.search.ManyToOneField( {attrs: {string: 'Dummy'}}, {relation: 'dummy.model'}, view); return f.complete("bob") @@ -563,6 +594,26 @@ openerp.testing.section('completions', { ok(!c, "no match should yield no completion"); }); }); + test("M2O filtered", {asserts: 2}, function (instance, $s, mock) { + mock('dummy.model:name_search', function (args, kwargs) { + deepEqual(args, [], "should have no positional arguments"); + deepEqual(kwargs, { + name: 'bob', + limit: 8, + args: [['foo', '=', 'bar']], + context: {flag: 1}, + }, "should use filtering domain"); + return [[42, "Match"]]; + }); + var view = { + inputs: [], + dataset: {get_context: function () { return {flag: 1}; }} + }; + var f = new instance.web.search.ManyToOneField( + {attrs: {string: 'Dummy', domain: '[["foo", "=", "bar"]]'}}, + {relation: 'dummy.model'}, view); + return f.complete("bob"); + }); }); openerp.testing.section('search-serialization', { dependencies: ['web.search'],