[imp] refactoring of the name_... methods + refactoring of the context and domain handling in many2ones

bzr revid: nicolas.vanhoren@openerp.com-20110628171852-1393loyd5ln1tsom
This commit is contained in:
niv-openerp 2011-06-28 19:18:52 +02:00
parent 4b4e8cdf7d
commit 31e7760e1a
3 changed files with 47 additions and 28 deletions

View File

@ -328,14 +328,19 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base.
args: args || []
}, callback, error_callback);
},
/**
* Arguments:
* name='', args=[], operator='ilike', context=None, limit=100
name_get: function(ids, callback) {
return this.call_and_eval('name_get', [ids, this.context], null, 1, callback);
},
/*
* args = domain
*/
name_search: function (args, callback, error_callback) {
name_search: function (name, args, operator, limit, callback) {
return this.call_and_eval('name_search',
args, 1, 3,
callback, error_callback);
[name || '', args || false, operator || 'ilike', this.context, limit || 100],
1, 3, callback);
},
name_create: function(name, callback) {
return this.call_and_eval('name_create', [name, this.context], null, 1, callback);
},
exec_workflow: function (id, signal, callback) {
return this.rpc('/base/dataset/exec_workflow', {
@ -347,8 +352,8 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base.
});
openerp.base.DataSetStatic = openerp.base.DataSet.extend({
init: function(session, model, ids) {
this._super(session, model);
init: function(session, model, context, ids) {
this._super(session, model, context);
// all local records
this.ids = ids || [];
this.count = this.ids.length;
@ -612,6 +617,7 @@ openerp.base.CompoundDomain = function() {
this.__ref = "compound_domain";
this.__domains = [];
this.__eval_context = null;
var self = this;
_.each(arguments, function(x) {
self.add(x);
});

View File

@ -1083,13 +1083,26 @@ openerp.base.form.FieldSelection = openerp.base.form.Field.extend({
* the fields'context with the action's context.
*/
var build_relation_context = function(relation_field) {
var action = relation_field.view.view_manager.action || {};
var a_context = action.context || {};
var f_context = relation_field.field.context || {};
var a_context = relation_field.view.dataset.context || {};
var fields_values = relation_field.view.get_fields_values();
var ctx = new openerp.base.CompoundContext(a_context).add(f_context).set_eval_context(fields_values);
var parent_values = a_context.get_eval_context ? a_context.get_eval_context() || {} : {};
parent_values = _.clone(parent_values);
delete parent_values.parent;
fields_values.parent = parent_values;
var f_context = new openerp.base.CompoundContext(relation_field.field.context || {}).set_eval_context(fields_values);
var ctx = new openerp.base.CompoundContext(a_context, f_context);
return ctx;
}
var build_relation_domain = function(relation_field) {
var a_context = relation_field.view.dataset.context || {};
var fields_values = relation_field.view.get_fields_values();
var parent_values = a_context.get_eval_context ? a_context.get_eval_context() || {} : {};
parent_values = _.clone(parent_values);
delete parent_values.parent;
fields_values.parent = parent_values;
var f_domain = new openerp.base.CompoundDomain(relation_field.field.domain || []).set_eval_context(fields_values);
return f_domain;
}
openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
init: function(view, node) {
@ -1203,10 +1216,10 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
var search_val = request.term;
var self = this;
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, []);
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, build_relation_context(self));
dataset.name_search([search_val, self.field.domain || [], 'ilike',
build_relation_context(self), this.limit + 1], function(data) {
dataset.name_search(search_val, build_relation_domain(self), 'ilike',
this.limit + 1, function(data) {
self.last_search = data.result;
// possible selections for the m2o
var values = _.map(data.result, function(x) {
@ -1217,8 +1230,8 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
if (values.length > self.limit) {
values = values.slice(0, self.limit);
values.push({label: "<em>   Search More...</em>", action: function() {
dataset.name_search([search_val, self.field.domain || [], 'ilike',
build_relation_context(self), false], function(data) {
dataset.name_search(search_val, build_relation_domain(self), 'ilike'
, false, function(data) {
self._change_int_value(null);
self._search_create_popup("search", data.result);
});
@ -1245,27 +1258,27 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
},
_quick_create: function(name) {
var self = this;
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, []);
dataset.call("name_create", [name, build_relation_context(self)], function(data) {
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, build_relation_context(self));
dataset.name_create(name, function(data) {
self._change_int_ext_value(data.result);
}, function(a, b) {
}).fail(function() {
self._change_int_value(null);
self._search_create_popup("form", undefined, {"default_name": name});
});
},
// all search/create popup handling
_search_create_popup: function(view, ids, context) {
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, []);
var self = this;
var pop = new openerp.base.form.SelectCreatePopup(null, self.view.session);
pop.select_element(self.field.relation,{
initial_ids: ids ? _.map(ids, function(x) {return x[0]}) : undefined,
initial_view: view,
disable_multiple_selection: true
}, self.view.domain || [],
new openerp.base.CompoundContext(build_relation_context(self)).add(context || {}));
}, build_relation_domain(self),
new openerp.base.CompoundContext(build_relation_context(self), context || {}));
pop.on_select_elements.add(function(element_ids) {
dataset.call("name_get", [[element_ids[0]]], function(data) {
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, build_relation_context(self));
dataset.name_get([element_ids[0]], function(data) {
self._change_int_ext_value(data.result[0]);
pop.stop();
});
@ -1302,8 +1315,8 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
self._change_int_ext_value(rval);
};
if(typeof(value) === "number") {
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, []);
dataset.call("name_get", [[value]], function(data) {
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, build_relation_context(self));
dataset.name_get([value], function(data) {
real_set_value(data.result[0]);
}).fail(function() {self.tmp_value = undefined;});
} else {
@ -1631,7 +1644,7 @@ openerp.base.form.SelectCreatePopup = openerp.base.BaseWidget.extend({
});
this.searchview.on_search.add(function(domains, contexts, groupbys) {
if (self.initial_ids) {
self.view_list.do_search.call(self,[[["id", "in", self.initial_ids]]],
self.view_list.do_search.call(self, domains.concat([[["id", "in", self.initial_ids]]]),
contexts, groupbys);
self.initial_ids = undefined;
} else {

View File

@ -259,7 +259,7 @@ openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
if (!action.res_id) {
dataset = new openerp.base.DataSetSearch(session, action.res_model, action.context || null);
} else {
dataset = new openerp.base.DataSetStatic(session, action.res_model, [action.res_id]);
dataset = new openerp.base.DataSetStatic(session, action.res_model, {}, [action.res_id]);
if (action.context) {
// TODO fme: should normalize all DataSets constructors to (session, model, context, ...)
dataset.context = action.context;