From 654ee788d998299679644bf9c5514baddb4bf0b9 Mon Sep 17 00:00:00 2001
From: "Lucas Perais (lpe)" <lpe@odoo.com>
Date: Thu, 31 May 2018 10:06:37 +0200
Subject: [PATCH] [FIX] web: new record with o2m also with new records prevent
 name_get

Have a model A with a o2m to B.
In the list have a field  B > m2o > C

Also have a sequence field (widget handle) on B

Then Create a record A
Arrange yourself to have two new records B popping right away (through an onchange)
in the o2m. Their C field have to be empty

Resequence those two B lines with the handle

Before this commit, there was a traceback when trying to do an name_get on the C fields
Since both of them are empty, there is no record in the localData
Hence, no model. And also no records anyway, so no need to do a name_get

After this commit, there is no traceback, the name_get is avoided when no data is to be fetched

OPW 1853088
closes #24988
---
 .../static/src/js/views/basic/basic_model.js  |  7 +--
 .../tests/fields/relational_fields_tests.js   | 52 +++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)

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 042f7bafa971..da0d58664c2c 100644
--- a/addons/web/static/src/js/views/basic/basic_model.js
+++ b/addons/web/static/src/js/views/basic/basic_model.js
@@ -1972,9 +1972,7 @@ var BasicModel = AbstractModel.extend({
         var records = [];
         var ids = [];
         list = this._applyX2ManyOperations(list);
-        if (_.isEmpty(list.data)) {
-            return $.when();
-        }
+
         _.each(list.data, function (localId) {
             var record = self.localData[localId];
             var data = record._changes || record.data;
@@ -1985,6 +1983,9 @@ var BasicModel = AbstractModel.extend({
             ids.push(many2oneRecord.res_id);
             model = many2oneRecord.model;
         });
+        if (!ids.length) {
+            return $.when();
+        }
         return this._rpc({
                 model: model,
                 method: 'name_get',
diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js
index 022fa8022742..190e2949fe73 100644
--- a/addons/web/static/tests/fields/relational_fields_tests.js
+++ b/addons/web/static/tests/fields/relational_fields_tests.js
@@ -2550,6 +2550,58 @@ QUnit.module('relational_fields', {
 
     QUnit.module('FieldOne2Many');
 
+    QUnit.test('New record with a o2m also with 2 new records, ordered, and resequenced', function (assert) {
+        assert.expect(3);
+
+        // Needed to have two new records in a single stroke
+        this.data.partner.onchanges = {
+            foo: function(obj) {
+                obj.p = [
+                            [5],
+                            [0, 0, {trululu: false}],
+                            [0, 0, {trululu: false}],
+                ]
+            }
+        };
+
+        var form = createView({
+            View: FormView,
+            model: 'partner',
+            data: this.data,
+            arch:'<form string="Partners">' +
+                    '<field name="foo" />' +
+                    '<field name="p">' +
+                        '<tree editable="bottom" default_order="int_field">' +
+                            '<field name="int_field" widget="handle"/>' +
+                            '<field name="trululu"/>' +
+                        '</tree>' +
+                    '</field>' +
+                '</form>',
+            viewOptions: {
+                mode: 'create',
+            },
+            mockRPC: function (route, args) {
+                assert.step(args.method + ' ' + args.model)
+                return this._super(route, args);
+            },
+        });
+
+        // change the int_field through drag and drop
+        // that way, we'll trigger the sorting and the name_get
+        // of the lines of "p"
+        testUtils.dragAndDrop(
+            form.$('.ui-sortable-handle').eq(1),
+            form.$('tbody tr').first(),
+            {position: 'top'}
+        );
+
+        // Only those two should have been called
+        // name_get on trululu would trigger an traceback
+        assert.verifySteps(['default_get partner', 'onchange partner']);
+
+        form.destroy();
+    });
+
     QUnit.test('O2M List with pager, decoration and default_order: add and cancel adding', function (assert) {
         assert.expect(3);
 
-- 
GitLab