bzr revid: nicolas.vanhoren@openerp.com-20110628140534-3lvqp50zrav7s7ld
This commit is contained in:
niv-openerp 2011-06-28 16:05:34 +02:00
commit 63a5ac2355
7 changed files with 162 additions and 76 deletions

View File

@ -239,7 +239,6 @@ def eval_context_and_domain(session, context, domain=None):
return (e_context, e_domain)
def load_actions_from_ir_values(req, key, key2, models, meta, context):
context['bin_size'] = False # Possible upstream bug. Antony says not to loose time on this.
Values = req.session.model('ir.values')
actions = Values.get(key, key2, models, meta, context)
@ -757,7 +756,6 @@ class Action(openerpweb.Controller):
Actions = req.session.model('ir.actions.actions')
value = False
context = req.session.eval_context(req.context)
context["bin_size"] = False
action_type = Actions.read([action_id], ['type'], context)
if action_type:
action = req.session.model(action_type[0]['type']).read([action_id], False,

View File

@ -137,6 +137,29 @@ openerp.base.Registry = Class.extend( /** @lends openerp.base.Registry# */ {
}
return object_match;
},
/**
* Tries a number of keys, and returns the first object matching one of
* the keys.
*
* @param {Array} keys a sequence of keys to fetch the object for
* @returns {Class} the first class found matching an object
*
* @throws {openerp.base.KeyNotFound} if none of the keys was in the mapping
* @trows {openerp.base.ObjectNotFound} if a found object path was invalid
*/
get_any: function (keys) {
for (var i=0; i<keys.length; ++i) {
try {
return this.get_object(keys[i]);
} catch (e) {
if (e instanceof openerp.base.KeyNotFound) {
continue;
}
throw e;
}
}
throw new openerp.base.KeyNotFound(keys.join(','));
},
/**
* Adds a new key and value to the registry.
*

View File

@ -196,9 +196,7 @@ openerp.base.GrouplessDataGroup = openerp.base.DataGroup.extend(
this._super(session, model, domain, context, null, level);
},
list: function (ifGroups, ifRecords) {
ifRecords(_.extend(
new openerp.base.DataSetSearch(this.session, this.model),
{domain: this.domain, context: this.context}));
ifRecords(new openerp.base.DataSetSearch(this.session, this.model, this.context, this.domain));
}
});
@ -287,12 +285,11 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base.
});
}
},
default_get: function(fields, context, callback) {
context = context || this.context;
default_get: function(fields, callback) {
return this.rpc('/base/dataset/default_get', {
model: this.model,
fields: fields,
context: context
context: this.context
}, callback);
},
create: function(data, callback, error_callback) {
@ -355,6 +352,9 @@ openerp.base.DataSetStatic = openerp.base.DataSet.extend({
// all local records
this.ids = ids || [];
this.count = this.ids.length;
if (this.ids.length) {
this.index = 0;
}
},
read_slice: function (fields, offset, limit, callback) {
var self = this;

View File

@ -241,12 +241,8 @@ openerp.base.FormView = openerp.base.View.extend( /** @lends openerp.base.FormV
},
on_button_new: function() {
var self = this;
var context = new openerp.base.CompoundContext(this.dataset.context);
if (this.view_manager.action && this.view_manager.action.context) {
context.add(this.view_manager.action.context);
}
$.when(this.has_been_loaded).then(function() {
self.dataset.default_get(_.keys(self.fields_view.fields), context, function(result) {
self.dataset.default_get(_.keys(self.fields_view.fields), function(result) {
self.on_record_loaded(result.result);
});
});
@ -455,7 +451,7 @@ openerp.base.form.compute_domain = function(expr, fields) {
}
}
var field = fields[ex[0]].value;
var field = fields[ex[0]].get_value ? fields[ex[0]].get_value() : fields[ex[0]].value;
var op = ex[1];
var val = ex[2];
@ -575,8 +571,8 @@ openerp.base.form.WidgetFrame = openerp.base.form.Widget.extend({
},
handle_node: function(node) {
var type = this.view.fields_view.fields[node.attrs.name] || {};
var widget_type = node.attrs.widget || type.type || node.tag;
var widget = new (this.view.registry.get_object(widget_type)) (this.view, node);
var widget = new (this.view.registry.get_any(
[node.attrs.widget, type.type, node.tag])) (this.view, node);
if (node.tag == 'field') {
if (!this.view.default_focus_field || node.attrs.default_focus == '1') {
this.view.default_focus_field = widget;
@ -673,6 +669,8 @@ openerp.base.form.WidgetButton = openerp.base.form.Widget.extend({
this.view.datarecord.id, function (result) {
self.log("Button returned", result);
self.view.reload();
}, function() {
self.view.reload();
});
}
});
@ -1267,7 +1265,7 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
}, self.view.domain || [],
new openerp.base.CompoundContext(build_relation_context(self)).add(context || {}));
pop.on_select_elements.add(function(element_ids) {
dataset.call("name_get", [element_ids[0]], function(data) {
dataset.call("name_get", [[element_ids[0]]], function(data) {
self._change_int_ext_value(data.result[0]);
pop.stop();
});
@ -1292,14 +1290,14 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
var _super = this._super;
this.tmp_value = value;
var real_set_value = function(rval) {
this.tmp_value = undefined;
self.tmp_value = undefined;
_super.apply(self, rval);
self.original_value = rval;
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) {
dataset.call("name_get", [[value]], function(data) {
real_set_value(data.result[0]);
}).fail(function() {self.tmp_value = undefined;});
} else {
@ -1333,7 +1331,43 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
# (4, ID) link
# (5) unlink all (only valid for one2many)
*/
var commands = {
// (0, _, {values})
CREATE: 0,
'create': function (values) {
return [commands.CREATE, false, values];
},
// (1, id, {values})
UPDATE: 1,
'update': function (id, values) {
return [commands.UPDATE, id, values];
},
// (2, id[, _])
DELETE: 2,
'delete': function (id) {
return [commands.DELETE, id, false];
},
// (3, id[, _]) removes relation, but not linked record itself
FORGET: 3,
'forget': function (id) {
return [commands.FORGET, id, false];
},
// (4, id[, _])
LINK_TO: 4,
'link_to': function (id) {
return [commands.LINK_TO, id, false];
},
// (5[, _[, _]])
FORGET_ALL: 5,
'forget_all': function () {
return [5, false, false];
},
// (6, _, ids) replaces all linked records with provided ids
REPLACE_WITH: 6,
'replace_with': function (ids) {
return [6, false, ids];
}
};
openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
init: function(view, node) {
this._super(view, node);
@ -1368,11 +1402,9 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
this.viewmanager = new openerp.base.ViewManager(this.view.session,
this.element_id, this.dataset, views);
var reg = new openerp.base.Registry();
reg.add("form", openerp.base.views.map["form"]);
reg.add("graph", openerp.base.views.map["graph"]);
reg.add("list", "openerp.base.form.One2ManyListView");
this.viewmanager.registry = reg;
this.viewmanager.registry = openerp.base.views.clone({
list: 'openerp.base.form.One2ManyListView'
});
this.viewmanager.on_controller_inited.add_last(function(view_type, controller) {
if (view_type == "list") {
@ -1399,21 +1431,27 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
var self = this;
if(value.length >= 1 && value[0] instanceof Array) {
var ids = [];
_each(value, function(command) {
if(command[0] == 0) {
var obj = {id: _.uniqueId(self.dataset.virtual_id_prefix), values: command[2]};
self.dataset.to_create.push(obj);
self.dataset.cache.push(_.clone(obj));
ids.push(obj.id);
} else if(command[0] == 1) {
var obj = {id: command[1], values: command[2]};
self.dataset.to_write.push(obj);
self.dataset.cache.push(_.clone(obj));
ids.push(obj.id);
} else if(command[0] == 2) {
self.dataset.to_delete.push({id: command[1]});
} else if(command[0] == 4) {
ids.push(command[1]);
_.each(value, function(command) {
var obj = {values: command[2]};
switch (command[0]) {
case commands.CREATE:
obj['id'] = _.uniqueId(self.dataset.virtual_id_prefix);
self.dataset.to_create.push(obj);
self.dataset.cache.push(_.clone(obj));
ids.push(obj.id);
return;
case commands.UPDATE:
obj['id'] = command[1];
self.dataset.to_write.push(obj);
self.dataset.cache.push(_.clone(obj));
ids.push(obj.id);
return;
case commands.DELETE:
self.dataset.to_delete.push({id: command[1]});
return;
case commands.LINK_TO:
ids.push(command[1]);
return;
}
});
this._super(ids);
@ -1431,16 +1469,17 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
var val = _.map(this.dataset.ids, function(id) {
var alter_order = _.detect(self.dataset.to_create, function(x) {return x.id === id;});
if (alter_order) {
return [0, 0, alter_order.values];
return commands.create(alter_order.values);
}
alter_order = _.detect(self.dataset.to_write, function(x) {return x.id === id;});
if (alter_order) {
return [1, alter_order.id, alter_order.values];
return commands.update(alter_order.id, alter_order.values);
}
return [4, id];
return commands.link_to(id);
});
val = val.concat(_.map(this.dataset.to_delete, function(v, k) {return [2, x.id];}));
return val;
return val.concat(_.map(
this.dataset.to_delete, function(x) {
return commands['delete'](x.id);}));
},
validate: function() {
this.invalid = false;
@ -1509,7 +1548,7 @@ openerp.base.form.FieldMany2Many = openerp.base.form.Field.extend({
});
},
get_value: function() {
return [[6,false,this.dataset.ids]];
return [commands.replace_with(this.dataset.ids)];
}
});

View File

@ -18,7 +18,7 @@ openerp.base.ActionManager = openerp.base.Controller.extend({
* Process an action
* Supported actions: act_window
*/
do_action: function(action) {
do_action: function(action, on_closed) {
var self = this;
action.flags = _.extend({
sidebar : action.target != 'new',
@ -31,6 +31,9 @@ openerp.base.ActionManager = openerp.base.Controller.extend({
// instantiate the right controllers by understanding the action
switch (action.type) {
case 'ir.actions.act_window':
if (!action.target && this.dialog_stack.length) {
action.flags.new_window = true;
}
if (action.target == 'new') {
var element_id = _.uniqueId("act_window_dialog");
$('<div>', {id: element_id}).dialog({
@ -44,6 +47,8 @@ openerp.base.ActionManager = openerp.base.Controller.extend({
});
var viewmanager = new openerp.base.ViewManagerAction(this.session, element_id, action);
viewmanager.start();
viewmanager.on_act_window_closed.add(on_closed);
viewmanager.is_dialog = true;
this.dialog_stack.push(viewmanager);
} else if (action.flags.new_window) {
action.flags.new_window = false;
@ -62,6 +67,9 @@ openerp.base.ActionManager = openerp.base.Controller.extend({
break;
case 'ir.actions.act_window_close':
var dialog = this.dialog_stack.pop();
if (!action.special) {
dialog.on_act_window_closed();
}
dialog.$element.dialog('destroy');
dialog.stop();
break;
@ -84,6 +92,7 @@ openerp.base.ViewManager = openerp.base.Controller.extend({
this.flags = this.flags || {};
this.sidebar = new openerp.base.NullSidebar();
this.registry = openerp.base.views;
this.is_dialog = false;
},
/**
* @returns {jQuery.Deferred} initial view loading promise
@ -127,16 +136,19 @@ openerp.base.ViewManager = openerp.base.Controller.extend({
}
if (view_type === 'list' && this.flags.search_view === false && this.action && this.action['auto_search']) {
// In case the search view is not instanciated: manually call ListView#search
var domains = !_(self.action.domain).isEmpty()
? [self.action.domain] : [],
contexts = !_(self.action.context).isEmpty()
? [self.action.context] : [];
controller.on_loaded.add({
callback: function () {
controller.do_search([self.action.domain], [self.action.context], []);
controller.do_search(domains, contexts, []);
},
position: 'last',
unique: true
});
}
view_promise = controller.start();
var self = this;
$.when(view_promise).then(function() {
self.on_controller_inited(view_type, controller);
});
@ -157,12 +169,13 @@ openerp.base.ViewManager = openerp.base.Controller.extend({
.filter('[data-view-type="' + view_type + '"]')
.attr('disabled', true);
for (var i in this.views) {
if (this.views[i].controller) {
if (i === view_type) {
$.when(view_promise).then(this.views[i].controller.do_show);
for (var view_name in this.views) {
if (!this.views.hasOwnProperty(view_name)) { continue; }
if (this.views[view_name].controller) {
if (view_name === view_type) {
$.when(view_promise).then(this.views[view_name].controller.do_show);
} else {
this.views[i].controller.do_hide();
this.views[view_name].controller.do_hide();
}
}
}
@ -197,6 +210,11 @@ openerp.base.ViewManager = openerp.base.Controller.extend({
});
return this.searchview.start();
},
/**
* Called when this view manager has been created by an action 'act_window@target=new' is closed
*/
on_act_window_closed : function() {
},
/**
* Called when one of the view want to execute an action
*/
@ -238,12 +256,14 @@ openerp.base.NullViewManager = openerp.base.generate_null_object_class(openerp.b
openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
init: function(session, element_id, action) {
var dataset;
if(!action.res_id) {
dataset = new openerp.base.DataSetSearch(session, action.res_model);
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);
dataset.ids = [action.res_id];
dataset.count = 1;
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;
}
}
this._super(session, element_id, dataset, action.views);
this.action = action;
@ -409,14 +429,17 @@ openerp.base.View = openerp.base.Controller.extend({
* @param {Object} [record_id] the identifier of the object on which the action is to be applied
* @param {Function} on_no_action callback to execute if the action does not generate any result (no new action)
*/
execute_action: function (action_data, dataset, action_manager, record_id, on_no_action) {
execute_action: function (action_data, dataset, action_manager, record_id, on_no_action, on_closed) {
var self = this;
var handler = function (r) {
var action = r.result;
if (action && action.constructor == Object) {
action.context = action.context || {};
action.context['active_id'] = dataset.ids[dataset.index];
action.context['active_ids'] = [dataset.ids[dataset.index]];
action.context['active_model'] = dataset.model;
_.extend(action.context, {
active_id: dataset.ids[dataset.index],
active_ids: [dataset.ids[dataset.index]],
active_model: dataset.model
});
action.flags = {
sidebar : false,
search_view : false,
@ -424,7 +447,12 @@ openerp.base.View = openerp.base.Controller.extend({
action_buttons : false,
pager : false
};
action_manager.do_action(action);
action_manager.do_action(action, on_closed);
if (self.view_manager.is_dialog && action.type != 'ir.actions.act_window_close') {
handler({
result : { type: 'ir.actions.act_window_close' }
});
}
} else {
on_no_action(action);
}
@ -432,7 +460,7 @@ openerp.base.View = openerp.base.Controller.extend({
if (action_data.special) {
handler({
result : { type: 'ir.actions.act_window_close' }
result : { type: 'ir.actions.act_window_close', special: action_data.special }
});
} else {
var context = new openerp.base.CompoundContext(dataset.context).add(action_data.context || {});
@ -476,7 +504,7 @@ openerp.base.json_node_to_xml = function(node, single_quote, indent) {
throw("Node a json node");
}
indent = indent || 0;
var sindent = Array(indent + 1).join('\t'),
var sindent = new Array(indent + 1).join('\t'),
r = sindent + '<' + node.tag;
for (var attr in node.attrs) {
var vattr = node.attrs[attr];

View File

@ -197,9 +197,9 @@ class CompoundDomain(BaseDomain):
final_domain = []
for domain in self.domains:
if not isinstance(domain, (list, BaseDomain)):
raise TypeError("Domain %r is not a list or a nonliteral Domain",
domain)
raise TypeError(
"Domain %r is not a list or a nonliteral Domain" % domain)
if isinstance(domain, list):
final_domain.extend(domain)
continue
@ -237,9 +237,9 @@ class CompoundContext(BaseContext):
final_context = {}
for context_to_eval in self.contexts:
if not isinstance(context_to_eval, (dict, BaseContext)):
raise TypeError("Context %r is not a dict or a nonliteral Context",
context_to_eval)
raise TypeError(
"Context %r is not a dict or a nonliteral Context" % context_to_eval)
if isinstance(context_to_eval, dict):
final_context.update(context_to_eval)
continue

View File

@ -138,15 +138,13 @@ class OpenERPSession(object):
"""
assert self._uid, "The user needs to be logged-in to initialize his context"
self.context = self.model('res.users').context_get(self.context)
# set bin_size to True all the time
self.context = self.context or {}
self.context["bin_size"] = True
self.client_timezone = self.context.get("tz", False)
# invalid code, anyway we decided the server will be in UTC
#if self.client_timezone:
# self.remote_timezone = self.execute('common', 'timezone_get')
self._locale = self.context.get('lang','en_US')
lang_ids = self.execute('res.lang','search', [('code', '=', self._locale)])
if lang_ids: