From 1b8c2a2078468ee040be1ebb87b9c5112c650eab Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 18 Aug 2015 15:34:36 +0200 Subject: [PATCH] [IMP] fields, web: change output format of `onchange` for x2many fields Those fields are serialized as lists of commands. --- .../src/js/views/form_relational_widgets.js | 15 +-------------- .../addons/test_new_api/tests/test_onchange.py | 12 ++++++------ openerp/fields.py | 15 ++++++++++----- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/addons/web/static/src/js/views/form_relational_widgets.js b/addons/web/static/src/js/views/form_relational_widgets.js index 37c98f40077..6b4f79541f4 100644 --- a/addons/web/static/src/js/views/form_relational_widgets.js +++ b/addons/web/static/src/js/views/form_relational_widgets.js @@ -474,19 +474,6 @@ var AbstractManyField = common.AbstractField.extend({ throw new Error("set_value of '"+this.name+"' must receive an list of ids without virtual ids.", ids); } if (_.find(ids, function(id) { return typeof(id) !== "number"; } )) { - this.dataset.reset_ids([], {keep_read_data: true}); - ids = _.map(ids, function(command) { - if (command instanceof Array) { - return command; - } else { - // command is a dictionary, convert it to a command - if (command.id) { - return COMMANDS.update(command.id, command); - } else { - return COMMANDS.create(command); - } - } - }); return this.send_commands(ids, {'_inhibit_on_change_flag': this._inhibit_on_change_flag}); } this.dataset.reset_ids(ids); @@ -616,7 +603,7 @@ var AbstractManyField = common.AbstractField.extend({ } return; case COMMANDS.DELETE_ALL: - return dataset.reset_ids([]); + return dataset.reset_ids([], {keep_read_data: true}); case COMMANDS.REPLACE_WITH: dataset.ids = []; return dataset.alter_ids(command[2], options); diff --git a/openerp/addons/test_new_api/tests/test_onchange.py b/openerp/addons/test_new_api/tests/test_onchange.py index 1bfe4ad5c45..0389ca1244d 100644 --- a/openerp/addons/test_new_api/tests/test_onchange.py +++ b/openerp/addons/test_new_api/tests/test_onchange.py @@ -107,19 +107,19 @@ class TestOnChange(common.TransactionCase): result = self.Discussion.onchange(values, 'name', field_onchange) self.assertIn('messages', result['value']) self.assertEqual(result['value']['messages'], [ - { - 'id': message.id, + (5,), + (1, message.id, { 'name': "[%s] %s" % ("Foo", USER.name), 'body': message.body, 'author': message.author.name_get()[0], 'size': message.size, - }, - { + }), + (0, 0, { 'name': "[%s] %s" % ("Foo", USER.name), 'body': BODY, 'author': USER.name_get()[0], 'size': len(BODY), - }, + }), ]) def test_onchange_specific(self): @@ -149,7 +149,7 @@ class TestOnChange(common.TransactionCase): self.assertIn('participants', result['value']) self.assertEqual( result['value']['participants'], - [{'id': usr.id} for usr in discussion.participants + demo], + [(5,)] + [(4, usr.id) for usr in discussion.participants + demo], ) def test_onchange_one2many_value(self): diff --git a/openerp/fields.py b/openerp/fields.py index 5f09c1044a0..848f9f8d0be 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -1737,14 +1737,19 @@ class _RelationalMulti(_Relational): return result def convert_to_onchange(self, value, fnames=None): - # return the recordset value as a list of dicts + # return the recordset value as a list of commands; the commands may + # give all fields values, the client is responsible for figuring out + # which fields are actually dirty fields = [(name, value._fields[name]) for name in (fnames or [])] - result = [] + result = [(5,)] for record in value: vals = {name: field.convert_to_onchange(record[name]) for name, field in fields} - if record.id: - vals['id'] = record.id - result.append(vals) + if not record.id: + result.append((0, 0, vals)) + elif vals: + result.append((1, record.id, vals)) + else: + result.append((4, record.id)) return result def convert_to_export(self, value, env):