From c73efc27b72a0d3b6bbbd8d7d502d2a8e72bb68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Fri, 12 Oct 2018 14:29:51 +0000 Subject: [PATCH] [FIX] web: stabilize sequence for new records on last page Before this commit, the compute sequence was relying on the fact the ID was used as a secondary order key to get the right result. This is necessary when adding items between pages because we don't resequence the following elements. However this was not necessary when adding new elements on the last page since there are no following elements to resesquence in that case. PR: #27639 --- .../static/src/js/views/basic/basic_model.js | 8 ++- .../tests/fields/relational_fields_tests.js | 68 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) 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 f6bf46aed68..0afb65a9a88 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -398,8 +398,14 @@ var BasicModel = AbstractModel.extend({ // by 1 (at least until there is a gap in the numbering). // We don't do it now because it's not an important case. + // However, we can for sure increment by 1 if we are on the last page. + var handleFieldValue = computedList.data[index].data[handleField]; - handleFieldValue = position !== 'top' ? handleFieldValue : handleFieldValue - 1; + if (position === 'top') { + handleFieldValue--; + } else if (list.count <= list.offset + list.limit - (list.tempLimitIncrement || 0)) { + handleFieldValue++; + } return { field: handleField, diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index b63b2b25a0c..99f97fbda54 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -9552,6 +9552,74 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('one2many with sequence field, override default_get, not last page', function (assert) { + assert.expect(1); + + this.data.partner.records[0].turtles = [3, 2, 1]; + + this.data.turtle.fields.turtle_int.default = 10; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '', + res_id: 1, + viewOptions: { + mode: 'edit', + }, + }); + + // click add a new line + // check turtle_int for new is the current max of the page + $('.o_field_x2many_list_row_add a').click(); + assert.strictEqual($('.modal .o_input[name="turtle_int"]').val(), '10'); + form.destroy(); + }); + + QUnit.test('one2many with sequence field, override default_get, last page', function (assert) { + assert.expect(1); + + this.data.partner.records[0].turtles = [3, 2, 1]; + + this.data.turtle.fields.turtle_int.default = 10; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '', + res_id: 1, + viewOptions: { + mode: 'edit', + }, + }); + + // click add a new line + // check turtle_int for new is the current max of the page +1 + $('.o_field_x2many_list_row_add a').click(); + assert.strictEqual($('.modal .o_input[name="turtle_int"]').val(), '22'); + form.destroy(); + }); + QUnit.test('one2many with sequence field, fetch name_get from empty list, field text', function (assert) { // There was a bug where a RPC would fail because no route was set. // The scenario is: