Skip to content

Commit

Permalink
[FIX] web: image widget is dependent on its record's last_update
Browse files Browse the repository at this point in the history
As a way to optimize loading, images are not necessarily fetched in db.
They have, in their url a "unique" parameter, which is the last_update date on **the record** and controls on the python-side whether it should get the image from a cache or from the db.

Before this commit, this __last_update field wasn't present in the view, so it wasn't fetched, and writes on a model's image worked but did not refresh.
The image displayed was the old one.

After this commit, when the image field widget is present, we force the loading of the __last_update field of the record.
Upon update, the image displayed is the new one.

OPW 777552
  • Loading branch information
kebeclibre committed Oct 23, 2017
1 parent 0589ee8 commit 151a8f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion addons/web/static/src/js/fields/basic_fields.js
Expand Up @@ -20,6 +20,7 @@ var framework = require('web.framework');
var session = require('web.session');
var utils = require('web.utils');
var view_dialogs = require('web.view_dialogs');
var field_utils = require('web.field_utils');

var qweb = core.qweb;
var _t = core._t;
Expand Down Expand Up @@ -1159,6 +1160,10 @@ var AbstractFieldBinary = AbstractField.extend({
});

var FieldBinaryImage = AbstractFieldBinary.extend({
fieldDependencies: _.extend({}, AbstractFieldBinary.prototype.fieldDependencies, {
__last_update: {type: 'datetime'},
}),

template: 'FieldBinaryImage',
placeholder: "/web/static/src/img/placeholder.png",
events: _.extend({}, AbstractFieldBinary.prototype.events, {
Expand All @@ -1182,7 +1187,7 @@ var FieldBinaryImage = AbstractFieldBinary.extend({
id: JSON.stringify(this.res_id),
field: this.nodeOptions.preview_image || this.name,
// unique forces a reload of the image when the record has been updated
unique: (this.recordData.__last_update || '').replace(/[^0-9]/g, ''),
unique: (this.recordData.__last_update && field_utils.format.datetime(this.recordData.__last_update) || '').replace(/[^0-9]/g, ''),
});
}
}
Expand Down
15 changes: 14 additions & 1 deletion addons/web/static/src/js/services/data_manager.js
Expand Up @@ -268,7 +268,6 @@ return core.Class.extend({
_processField: function (viewType, field, attrs) {
var self = this;
attrs.Widget = this._getFieldWidgetClass(viewType, field, attrs);

if (!_.isObject(attrs.options)) { // parent arch could have already been processed (TODO this should not happen)
attrs.options = attrs.options ? pyeval.py_eval(attrs.options) : {};
}
Expand Down Expand Up @@ -388,6 +387,11 @@ return core.Class.extend({
}
}
}

if (attrs.Widget.prototype.fieldDependencies) {
attrs.fieldDependencies = attrs.Widget.prototype.fieldDependencies;
}

return attrs;
},
/**
Expand All @@ -414,6 +418,15 @@ return core.Class.extend({
if (node.tag === 'field') {
fieldsInfo[node.attrs.name] = self._processField(viewType,
fields[node.attrs.name], node.attrs ? _.clone(node.attrs) : {});

if (fieldsInfo[node.attrs.name].fieldDependencies) {
var deps = fieldsInfo[node.attrs.name].fieldDependencies;
for (var dependency_name in deps) {
if (!(dependency_name in fieldsInfo)) {
fieldsInfo[dependency_name] = {'name': dependency_name, 'dependency_of': node.attrs.name};
}
}
}
return false;
}
return node.tag !== 'arch';
Expand Down

0 comments on commit 151a8f0

Please sign in to comment.