[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: f78b7a76d6

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) <aab@odoo.com>
This commit is contained in:
Sébastien Theys
2019-03-14 14:10:54 +00:00
parent e73ad6127e
commit fec83667bb
2 changed files with 49 additions and 10 deletions
@@ -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;
},
+34 -1
View File
@@ -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: '<kanban class="o_kanban_test">' +
'<field name="id"/>' +
'<field name="image"/>' +
'<templates><t t-name="kanban-box"><div>' +
'<img t-att-src="kanban_image(\'partner\', \'image\', 1)"/>' +
'</div></t></templates>' +
'</kanban>',
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);