From a4e30a1b43d0b9acd76508d9e6c466cd1dbdffab Mon Sep 17 00:00:00 2001 From: Elkasitu Date: Tue, 25 Jul 2017 10:28:18 +0200 Subject: [FIX] web: prevent crash in FieldReference Trying to display external identifiers would make the server crash because the method in charge of requesting the external id information would expect an object but it was being given a string and thus not computing the m2o_value for the record. This commit fixes this by changing FieldMany2One objects' m2o_value instatiation to use its _formatValue method to compute it, _formatValue is then overridden in FieldReference so that it properly handles both strings and objects to calculate its m2o_value and relation. --- .../web/static/src/js/fields/relational_fields.js | 26 ++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'addons/web/static/src/js/fields/relational_fields.js') diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 7522bb3bc37..89fcb4fe406 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -99,7 +99,7 @@ var FieldMany2One = AbstractField.extend({ this.nodeOptions = _.defaults(this.nodeOptions, { quick_create: true, }); - this.m2o_value = field_utils.format.many2one(this.value); + this.m2o_value = this._formatValue(this.value); // 'recordParams' is a dict of params used when calling functions // 'getDomain' and 'getContext' on this.record this.recordParams = {fieldName: this.name, viewType: this.viewType}; @@ -2029,7 +2029,20 @@ var FieldReference = FieldMany2One.extend({ // needs to be copied as it is an unmutable object this.field = _.extend({}, this.field); if (this.value) { - this._setRelation(this.value.model); + var model = null; + // The value of a Reference field can either be a string in the form + // of model,record_id or an object + if (typeof this.value === "string") { + var value = this.value.split(','); + model = value[0]; + this.value = { + res_id: parseInt(value[1]), + }; + } else { + model = this.value.model; + } + + this._setRelation(model); } }, /** @@ -2044,6 +2057,15 @@ var FieldReference = FieldMany2One.extend({ // Private //-------------------------------------------------------------------------- + /** + * Get the encompassing record's display_name + * + * @override + */ + _formatValue: function () { + return this.recordData.display_name; + }, + /** * Add a select in edit mode (for the model). * -- cgit v1.2.3 From 2771b23a35c27165b54904a9e433202a5cd226af Mon Sep 17 00:00:00 2001 From: kujiu Date: Tue, 25 Jul 2017 13:43:37 +0200 Subject: [FIX] web: do not delete records for one2many with widget=many2many Some one2many fields are called with a many2many widget to unlink records instead of delete them. But this behavior was modified with the new views and the records were deleted anyway. In this commit, we restore the previous behavior, and create a FORGET command in basic model to do that. Note that the widget='many2many' is a hack, and a better way to accomplish this is to use a new custom option. But this would be a fix for master (PR #18413) --- addons/web/static/src/js/fields/relational_fields.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'addons/web/static/src/js/fields/relational_fields.js') diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 89fcb4fe406..1935e5804dd 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -838,8 +838,10 @@ var FieldX2Many = AbstractField.extend({ */ _onDeleteRecord: function (ev) { ev.stopPropagation(); + var shouldForget = this.attrs.widget === 'many2many' || this.field.type === 'many2many'; + var operation = shouldForget ? 'FORGET' : 'DELETE'; this._setValue({ - operation: 'REMOVE', + operation: operation, ids: [ev.data.id], }); }, @@ -1290,7 +1292,7 @@ var FieldMany2ManyBinaryMultiFiles = AbstractField.extend({ var record = _.findWhere(this.value.data, {res_id: fileID}); if (record) { this._setValue({ - operation: 'REMOVE', + operation: 'FORGET', ids: [record.id], }); var metadata = this.metadata[record.id]; @@ -1469,7 +1471,7 @@ var FieldMany2ManyTags = AbstractField.extend({ _removeTag: function (id) { var record = _.findWhere(this.value.data, {res_id: id}); this._setValue({ - operation: 'REMOVE', + operation: 'FORGET', ids: [record.id], }); }, -- cgit v1.2.3