From ad1cceb4fbd2f054af647833bd34e4e06012bb6c Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 20 Feb 2018 11:01:22 +0100 Subject: [FIX] web: one2many and onchange corner case issue Let's assume a one2many list view inside a form view, displaying a single char field A. There is an onchange on the form view returning a command 0 (CREATE) for the one2many. The command 0 only specifies a value for field A. The one2many list isn't editable, so when a sub-record is clicked, it is open in a form view (in a dialog). In this form view, an x2many field B is displayed in a sub list or kanban view. Before this rev., it crashed when the user tried to open the created one2many sub-record (returned by the onchange) in the form view, because of the presence of the x2many field B, which wasn't correctly processed by the BasicModel (its value was undefined, whereas it must be a valid, empty, dataPoint). Moreover, the limit of the x2many field B wasn't set, so a pager was displayed as soon as it contained at least one record. Fixes #22050 --- .../web/static/src/js/views/basic/basic_model.js | 143 +++++++++++++-------- addons/web/static/src/js/views/basic/basic_view.js | 7 +- 2 files changed, 97 insertions(+), 53 deletions(-) (limited to 'addons/web/static/src/js/views/basic') 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 6eef41f149d..e2cc60f7fc4 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -189,6 +189,89 @@ var BasicModel = AbstractModel.extend({ return id; }); }, + /** + * Add and process default values for a given record. Those values are + * parsed and stored in the '_changes' key of the record. For relational + * fields, sub-dataPoints are created, and missing relational data is + * fetched. Also generate default values for fields with no given value. + * Typically, this function is called with the result of a 'default_get' + * RPC, to populate a newly created dataPoint. It may also be called when a + * one2many subrecord is open in a form view (dialog), to generate the + * default values for the fields displayed in the o2m form view, but not in + * the list or kanban (mainly to correctly create sub-dataPoints for + * relational fields). + * + * @param {string} recordID local id for a record + * @param {Object} values dict of default values for the given record + * @param {Object} [options] + * @param {string} [options.viewType] current viewType. If not set, we will + * assume main viewType from the record + * @param {Array} [options.fieldNames] list of field names for which a + * default value must be generated (used to complete the values dict) + * @returns {Deferred} + */ + applyDefaultValues: function (recordID, values, options) { + options = options || {}; + var record = this.localData[recordID]; + var viewType = options.viewType || record.viewType; + var fieldNames = options.fieldNames || Object.keys(record.fieldsInfo[viewType]); + var field; + var fieldName; + record._changes = record._changes || {}; + + // fill default values for missing fields + for (var i = 0; i < fieldNames.length; i++) { + fieldName = fieldNames[i]; + if (!(fieldName in values) && !(fieldName in record._changes)) { + field = record.fields[fieldName]; + if (field.type === 'float' || + field.type === 'integer' || + field.type === 'monetary') { + values[fieldName] = 0; + } else if (field.type === 'one2many' || field.type === 'many2many') { + values[fieldName] = []; + } else { + values[fieldName] = null; + } + } + } + + // parse each value and create dataPoints for relational fields + var defs = []; + for (fieldName in values) { + field = record.fields[fieldName]; + if (!field) { + continue; // ignore values for unknown fields + } + record.data[fieldName] = null; + var dp; + if (field.type === 'many2one' && values[fieldName]) { + dp = this._makeDataPoint({ + context: record.context, + data: {id: values[fieldName]}, + modelName: field.relation, + parentID: record.id, + }); + record._changes[fieldName] = dp.id; + } else if (field.type === 'reference' && values[fieldName]) { + var ref = values[fieldName].split(','); + dp = this._makeDataPoint({ + context: record.context, + data: {id: parseInt(ref[1])}, + modelName: ref[0], + parentID: record.id, + }); + defs.push(this._fetchNameGet(dp)); + record._changes[fieldName] = dp.id; + } else if (field.type === 'one2many' || field.type === 'many2many') { + defs.push(this._processX2ManyCommands(record, fieldName, values[fieldName], options)); + } else { + record._changes[fieldName] = this._parseServerValue(field, values[fieldName]); + } + } + + return $.when.apply($, defs); + }, /** * Onchange RPCs may return values for fields that are not in the current * view. Those fields might even be unknown when the onchange returns (e.g. @@ -1397,6 +1480,7 @@ var BasicModel = AbstractModel.extend({ list = self._makeDataPoint({ fields: view ? view.fields : fieldInfo.relatedFields, fieldsInfo: view ? view.fieldsInfo : fieldInfo.fieldsInfo, + limit: fieldInfo.limit, modelName: field.relation, parentID: record.id, static: true, @@ -3227,27 +3311,8 @@ var BasicModel = AbstractModel.extend({ context: params.context, }) .then(function (result) { - // fill default values for missing fields - for (var i = 0; i < fieldNames.length; i++) { - var fieldName = fieldNames[i]; - if (!(fieldName in result)) { - var field = params.fields[fieldName]; - if (field.type === 'float' || - field.type === 'integer' || - field.type === 'monetary') { - result[fieldName] = 0; - } else if (field.type === 'one2many' || field.type === 'many2many') { - result[fieldName] = []; - } else { - result[fieldName] = null; - } - } - } - - var data = {}; var record = self._makeDataPoint({ modelName: modelName, - data: data, fields: params.fields, fieldsInfo: params.fieldsInfo, context: params.context, @@ -3256,37 +3321,7 @@ var BasicModel = AbstractModel.extend({ viewType: params.viewType, }); - var defs = []; - _.each(fieldNames, function (name) { - var field = params.fields[name]; - data[name] = null; - record._changes = record._changes || {}; - var dp; - if (field.type === 'many2one' && result[name]) { - dp = self._makeDataPoint({ - context: record.context, - data: {id: result[name]}, - modelName: field.relation, - parentID: record.id, - }); - record._changes[name] = dp.id; - } else if (field.type === 'reference' && result[name]) { - var ref = result[name].split(','); - dp = self._makeDataPoint({ - context: record.context, - data: {id: parseInt(ref[1])}, - modelName: ref[0], - parentID: record.id, - }); - defs.push(self._fetchNameGet(dp)); - record._changes[name] = dp.id; - } else if (field.type === 'one2many' || field.type === 'many2many') { - defs.push(self._processX2ManyCommands(record, name, result[name])); - } else { - record._changes[name] = self._parseServerValue(field, result[name]); - } - }); - return $.when.apply($, defs) + return self.applyDefaultValues(record.id, result) .then(function () { var def = $.Deferred(); self._performOnChange(record, fields_key).always(function () { @@ -3469,13 +3504,17 @@ var BasicModel = AbstractModel.extend({ * @param {Object} record * @param {string} fieldName * @param {Array[Array]} commands + * @param {Object} [options] + * @param {string} [options.viewType] current viewType. If not set, we will + * assume main viewType from the record * @returns {Deferred} */ - _processX2ManyCommands: function (record, fieldName, commands) { + _processX2ManyCommands: function (record, fieldName, commands, options) { var self = this; + options = options || {}; var defs = []; var field = record.fields[fieldName]; - var fieldInfo = record.fieldsInfo[record.viewType][fieldName]; + var fieldInfo = record.fieldsInfo[options.viewType || record.viewType][fieldName]; var view = fieldInfo.views && fieldInfo.views[fieldInfo.mode]; var fieldsInfo = view ? view.fieldsInfo : fieldInfo.fieldsInfo; var fields = view ? view.fields : fieldInfo.relatedFields; diff --git a/addons/web/static/src/js/views/basic/basic_view.js b/addons/web/static/src/js/views/basic/basic_view.js index 8a91e2b90c7..c76020cacac 100644 --- a/addons/web/static/src/js/views/basic/basic_view.js +++ b/addons/web/static/src/js/views/basic/basic_view.js @@ -120,7 +120,12 @@ var BasicView = AbstractView.extend({ // (because those fields were unknow at that time). So we ask // the model to process them. def = this.model.applyRawChanges(record.id, viewType).then(function () { - if (!self.model.isNew(record.id)) { + if (self.model.isNew(record.id)) { + return self.model.applyDefaultValues(record.id, {}, { + fieldNames: fieldNames, + viewType: viewType, + }); + } else { return self.model.reload(record.id, { fieldNames: fieldNames, keepChanges: true, -- cgit v1.2.3 From a16013be6636db58a4252fafd67033c6643a1e48 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 23 Feb 2018 13:48:20 +0100 Subject: [FIX] web: multi pages ordered o2m with onchange Let's assume a one2many displayed over several pages with a default order such that a record, say R, that would normally be on page 1 is actually on another page. Moreover, there is an onchange on that one2many which converts all commands 4 (LINK_TO) into commands 1 (UPDATE), which is standard in the ORM. When the first line of the one2many was edited, the record R was fully loaded, whereas it shouldn't have been (as it is not on the current page). So basically, a useless read was performed. Moreover, if there was an x2many field in the one2many list, a crash occurred because relational data of R that shouldn't have been read wasn't postprocessed by the BasicModel. The useless read occurred because the one2many subrecords weren't correctly sorted before checking if some of them/some of their fields should be read. This is not the case anymore. OPW-1816800 --- addons/web/static/src/js/views/basic/basic_model.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'addons/web/static/src/js/views/basic') 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 e2cc60f7fc4..18193a5c364 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -3804,14 +3804,14 @@ var BasicModel = AbstractModel.extend({ // for multi-pages list datapoints, we might need to read the // order field first to apply the order on all pages - if (list.res_ids.length > list.limit) { - if (!list.orderedResIDs && list.orderedBy.length) { + if (list.res_ids.length > list.limit && list.orderedBy.length) { + if (!list.orderedResIDs) { var fieldNames = _.pluck(list.orderedBy, 'name'); def = this._readMissingFields(list, _.filter(list.res_ids, _.isNumber), fieldNames); - def.then(function () { - self._sortList(list); - }); } + def.then(function () { + self._sortList(list); + }); } return def.then(function () { var resIDs = []; -- cgit v1.2.3 From 034c075e7e58ad9e5a2dce34782106c0dbd1c786 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 26 Feb 2018 08:48:19 +0100 Subject: [FIX] web: default_get returning unknown field Before this rev., it crashed when trying to create records of the res.users model. This is due to commit ad1cceb. The 'default_get' of 'res.users' always returns a value for field 'groups_id', whereas the docstring of 'default_get' states that it only returns default values for fields given in arguments. Rev. ad1cceb changed a bit the way the result of 'default_get' was processed, by iterating over the values instead of over the fields in the view. So, before ad1cceb, 'groups_id' was simply ignored, and after, the BasicModel tried to process it and it crashed when it attempted to retrieve its fieldsInfo, as this field isn't in the view. This rev. restores the previous behavior. Fixes #23303 Fixes #23305 --- addons/web/static/src/js/views/basic/basic_model.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'addons/web/static/src/js/views/basic') 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 18193a5c364..c7f61edd541 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -219,6 +219,10 @@ var BasicModel = AbstractModel.extend({ var fieldName; record._changes = record._changes || {}; + // ignore values for non requested fields (for instance, fields that are + // not in the view) + values = _.pick(values, fieldNames); + // fill default values for missing fields for (var i = 0; i < fieldNames.length; i++) { fieldName = fieldNames[i]; @@ -240,9 +244,6 @@ var BasicModel = AbstractModel.extend({ var defs = []; for (fieldName in values) { field = record.fields[fieldName]; - if (!field) { - continue; // ignore values for unknown fields - } record.data[fieldName] = null; var dp; if (field.type === 'many2one' && values[fieldName]) { -- cgit v1.2.3 From 132137939cc8460868a0e77ff78814a9d95e5a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 26 Feb 2018 09:38:44 +0100 Subject: [FIX] web: prevent footer from being rerendered In some cases, the footer in a form view dialog could be duplicated. The reason was that the form view was rerendered completely, footer included, by the renderer, out of the formcontroller control (it is the formcontroller's job to move the footer buttons to the footer). A possible fix is to work on the form renderer to make it aware that the footer buttons are expected to be only rendered once, but it has the disadvantage to be a fix specific to this issue, and any other form view behaviour with some side effects may have the same issue. This commit tries to prevent the form view from rerendering itself completely in those cases. This also has the disadvantage of possibly not being a complete fix, if some edge cases are missed. However, I have the feeling that we still want to avoid rerendering the full view if possible. --- addons/web/static/src/js/views/basic/basic_controller.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'addons/web/static/src/js/views/basic') diff --git a/addons/web/static/src/js/views/basic/basic_controller.js b/addons/web/static/src/js/views/basic/basic_controller.js index 086428c2613..328daf956ee 100644 --- a/addons/web/static/src/js/views/basic/basic_controller.js +++ b/addons/web/static/src/js/views/basic/basic_controller.js @@ -271,6 +271,13 @@ var BasicController = AbstractController.extend(FieldManagerMixin, { * @returns {Deferred} */ _confirmChange: function (id, fields, e) { + if (e.name === 'discard_changes' && e.target.reset) { + // the target of the discard event is a field widget. In that + // case, we simply want to reset the specific field widget, + // not the full view + return e.target.reset(this.model.get(e.target.dataPointID), e, true); + } + var state = this.model.get(this.handle); return this.renderer.confirmChange(state, id, fields, e); }, -- cgit v1.2.3 From 9ccda2076444631a3a491bd0ddb231054a139bce Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 6 Mar 2018 14:13:28 +0100 Subject: [FIX] web: BasicView: prevent crash in tests A crash occured in several Studio tests that have just been forwardported to saas-11.2, because of a line that hadn't been adapted in rev. cb339842 (technically, the previous code was correct, but it crashed when viewInfo.fields was undefined, which doesn't happen outside the tests environment). --- addons/web/static/src/js/views/basic/basic_view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/views/basic') diff --git a/addons/web/static/src/js/views/basic/basic_view.js b/addons/web/static/src/js/views/basic/basic_view.js index a0574c6a401..b4df9c8e09e 100644 --- a/addons/web/static/src/js/views/basic/basic_view.js +++ b/addons/web/static/src/js/views/basic/basic_view.js @@ -45,7 +45,7 @@ var BasicView = AbstractView.extend({ this.rendererParams.viewType = this.viewType; this.controllerParams.confirmOnDelete = true; - this.controllerParams.archiveEnabled = 'active' in viewInfo.fields; + this.controllerParams.archiveEnabled = 'active' in this.fields; this.controllerParams.hasButtons = 'action_buttons' in params ? params.action_buttons : true; -- cgit v1.2.3 From 3809fc891f5b4794493b6ad4ed4bc4000c9a416e Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 7 Mar 2018 09:23:24 +0100 Subject: [FIX] web,mail: always reload widget mail_activity Before this rev., the field widget 'mail_activity' kept an internal cache of fetched activities (like the 'mail_thread' and 'mail_followers'). However, for activities, this cache may cause issues. For instance, create a new lead in 'My pipeline', schedule a 'Meeting' activity on that lead (it opens the Calendar view so that real meetings can be created), create a meeting 2 weeks from now, go back to the lead form view: the activity still diplays that the meeting is today (when the view is refreshed, the correct date is displayed). This rev. removes the internal cache for this field widget, as the activities can easily be updated in business flows (unlike messages or followers). This rev. also fixes a bug in the BasicModel, where the res_ids of a list datapoint was altered in place to return a list of x2many commands instead of the list of ids when being stringified. This caused a traceback when opening a lead with activities, as soon as the internal cache had been removed. --- addons/web/static/src/js/views/basic/basic_model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/views/basic') 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 c7f61edd541..c8bd4ae030f 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -3047,7 +3047,7 @@ var BasicModel = AbstractModel.extend({ if (field.type === 'one2many' || field.type === 'many2many') { var ids; if (!context[fieldName] || _.isArray(context[fieldName])) { // no dataPoint created yet - ids = context[fieldName] || []; + ids = context[fieldName] ? context[fieldName].slice(0) : []; } else { relDataPoint = this._applyX2ManyOperations(this.localData[context[fieldName]]); ids = relDataPoint.res_ids.slice(0); -- cgit v1.2.3 From c7b5b12be6b6faf6b58057a64d5f7ba1e57b85fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 8 Mar 2018 16:34:01 +0100 Subject: [FIX] web: add the node attribute to the form view widget This could (should?) be considered a refactoring, but this is actually trying to restore lost functionality. In v10, the custom form widgets were created with access to the node element that define them. We lost that in v11, with the new views. However, it is actually very useful for generic widgets. Since this is not really a risky fix, I think that it can be done in 11.0. Note: this feature was already reintroduced in master in commit https://github.com/odoo/odoo/commit/b7aa439826df6b3e442b3f60bb593ff28efc8679 --- addons/web/static/src/js/views/basic/basic_renderer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/views/basic') diff --git a/addons/web/static/src/js/views/basic/basic_renderer.js b/addons/web/static/src/js/views/basic/basic_renderer.js index 204c35acaef..4b22a5dc66f 100644 --- a/addons/web/static/src/js/views/basic/basic_renderer.js +++ b/addons/web/static/src/js/views/basic/basic_renderer.js @@ -607,7 +607,7 @@ var BasicRenderer = AbstractRenderer.extend({ */ _renderWidget: function (record, node) { var Widget = widgetRegistry.get(node.attrs.name); - var widget = new Widget(this, record); + var widget = new Widget(this, record, node); this.widgets.push(widget); -- cgit v1.2.3