From fec83667bb7743b5474971deafe861c33fcc2cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Thu, 14 Mar 2019 14:10:54 +0000 Subject: [PATCH] [FIX] web: fix kanban_image when called for a different record The issue is manifesting for example in the Contact kanban view where the company image should be displayed in small. Before this fix, the person image was displayed twice instead. Following PR #30881 and commit: f78b7a76d62d6b5f3e82f2f4a02b14981859df1f The fix introduced by the above commit highlighted another issue with the method: if the `model` and `id` given in parameter are different than the current record, the method returned the url for the current record instead of the requested record. To fix the issue, we return the `raw_value` only if the requested record is the current record. Otherwise, we return the manually built url. PR: #31842 Signed-off-by: Aaron Bohy (aab) --- .../src/js/views/kanban/kanban_record.js | 24 ++++++++----- addons/web/static/tests/views/kanban_tests.js | 35 ++++++++++++++++++- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/addons/web/static/src/js/views/kanban/kanban_record.js b/addons/web/static/src/js/views/kanban/kanban_record.js index 48f1aacf4ad..94621211d2d 100644 --- a/addons/web/static/src/js/views/kanban/kanban_record.js +++ b/addons/web/static/src/js/views/kanban/kanban_record.js @@ -182,24 +182,30 @@ var KanbanRecord = Widget.extend({ */ _getImageURL: function (model, field, id, cache, options) { options = options || {}; + id = (_.isArray(id) ? id[0] : id) || false; + field = options.preview_image || field; + var isCurrentRecord = this.modelName === model && this.recordData.id === id; var url; - if (this.record[field] && this.record[field].raw_value && !utils.is_bin_size(this.record[field].raw_value)) { + if (isCurrentRecord && this.record[field] && this.record[field].raw_value && !utils.is_bin_size(this.record[field].raw_value)) { // Use magic-word technique for detecting image type url = 'data:image/' + this.file_type_magic_word[this.record[field].raw_value[0]] + ';base64,' + this.record[field].raw_value; - } else if (this.record[field] && ! this.record[field].raw_value) { + } else if (!model || !field || !id || (isCurrentRecord && this.record[field] && !this.record[field].raw_value)) { url = "/web/static/src/img/placeholder.png"; } else { - if (_.isArray(id)) { id = id[0]; } - if (!id) { id = undefined; } - if (options.preview_image) - field = options.preview_image; - var unique = this.record.__last_update && this.record.__last_update.value.replace(/[^0-9]/g, ''); var session = this.getSession(); - url = session.url('/web/image', {model: model, field: field, id: id, unique: unique}); + var params = { + model: model, + field: field, + id: id + }; + if (isCurrentRecord) { + params.unique = this.record.__last_update && this.record.__last_update.value.replace(/[^0-9]/g, ''); + } if (cache !== undefined) { // Set the cache duration in seconds. - url += '&cache=' + parseInt(cache, 10); + params.cache = parseInt(cache, 10); } + url = session.url('/web/image', params); } return url; }, diff --git a/addons/web/static/tests/views/kanban_tests.js b/addons/web/static/tests/views/kanban_tests.js index a012eff1ca8..3b9e6435536 100644 --- a/addons/web/static/tests/views/kanban_tests.js +++ b/addons/web/static/tests/views/kanban_tests.js @@ -4847,7 +4847,7 @@ QUnit.module('Views', { } }); - QUnit.test('test displaying image (URL)', function (assert) { + QUnit.test('test displaying image (URL, image field not set)', function (assert) { assert.expect(1); var kanban = createView({ @@ -4899,6 +4899,39 @@ QUnit.module('Views', { kanban.destroy(); }); + QUnit.test('test displaying image (for another record)', function (assert) { + assert.expect(2); + + var kanban = createView({ + View: KanbanView, + model: 'partner', + data: this.data, + arch: '' + + '' + + '' + + '
' + + '' + + '
' + + '
', + mockRPC: function (route, args) { + if (route === 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACAA==') { + assert.ok("The view's image should have been fetched."); + return $.when(); + } + return this._super.apply(this, arguments); + }, + }); + + // the field image is set, but we request the image for a specific id + // -> for the record matching the ID, the base64 should be returned + // -> for all the other records, the image should be displayed by url + var imageOnRecord = kanban.$('img[data-src*="/web/image"][data-src*="&id=1"]'); + assert.strictEqual(imageOnRecord.length, this.data.partner.records.length - 1, + "display image by url when requested for another record"); + + kanban.destroy(); + }); + QUnit.test('check if the view destroys all widgets and instances', function (assert) { assert.expect(1);