[FIX] web: contexts on field widgets + enforce bin_size for binary

Field widgets now have a key 'context' that let them
extend the context of the dataPoint (e.g. list).

In particular, widgets on binary fields enforce the contextual
item {bin_size: True}, so that the server gives the size of the
binary field as its value, instead of its content.

This change on binary fields reduces network load when accessing
a view with binary fields.
This commit is contained in:
Alexandre Kühn
2018-01-12 16:12:49 +01:00
committed by Alexandre Kühn
parent 7813a8e714
commit 41fe7f9d11
8 changed files with 50 additions and 14 deletions
-3
View File
@@ -31,9 +31,6 @@ var utils = {
* @return {string} bin_size (human-readable)
*/
binaryToBinsize: function (value) {
if (!value) {
return this.human_size(0);
}
if (!this.is_bin_size(value)) {
// Computing approximate size out of base64 encoded string
// http://en.wikipedia.org/wiki/Base64#MIME
@@ -44,6 +44,11 @@ var AbstractField = Widget.extend({
navigation_move: '_onNavigationMove',
},
/**
* fields can extend the context, e.g. binary fields add {bin_size: true}
*/
context: null,
/**
* An object representing fields to be fetched by the model eventhough not present in the view
* This object contains "field name" as key and an object as value.
@@ -1080,6 +1080,7 @@ var AbstractFieldBinary = AbstractField.extend({
},
'click .o_clear_file_button': 'on_clear',
}),
context: { bin_size: true },
init: function (parent, name, record) {
this._super.apply(this, arguments);
this.fields = record.fields;
@@ -387,6 +387,10 @@ return core.Class.extend({
attrs.fieldDependencies = attrs.Widget.prototype.fieldDependencies;
}
if (attrs.Widget.prototype.context) {
attrs.context = attrs.Widget.prototype.context;
}
return attrs;
},
/**
@@ -33,7 +33,7 @@ odoo.define('web.BasicModel', function (require) {
* isOpen: {boolean},
* loadMoreOffset: {integer},
* limit: {integer},
* model: {string,
* model: {string},
* offset: {integer},
* openGroupByDefault: {boolean},
* orderedBy: {Object[]},
@@ -1854,7 +1854,7 @@ var BasicModel = AbstractModel.extend({
model: record.model,
method: 'read',
args: [[record.res_id], fieldNames],
context: _.extend({}, record.getContext(), {bin_size: true}),
context: record.getContext(),
})
.then(function (result) {
if (result.length === 0) {
@@ -3040,10 +3040,33 @@ var BasicModel = AbstractModel.extend({
* the resource in the localData object.
*
* @param {Object} params
* @param {Object} [params.fieldsInfo={}] contains the fieldInfo of each field
* @param {Array} [params.fieldNames] the name of fields to load, the list
* of all fields by default
* @param {Object} [params.aggregateValues={}]
* @param {Object} [params.context={}] context of the action
* @param {integer} [params.count=0] number of record being manipulated
* @param {Object|Object[]} [params.data={}|[]] data of the record
* @param {*[]} [params.domain=[]]
* @param {Object} params.fields contains the description of each field
* @param {Object} [params.fieldsInfo={}] contains the fieldInfo of each field
* @param {Object[]} [params.fieldNames] the name of fields to load, the list
* of all fields by default
* @param {string[]} [params.groupedBy=[]]
* @param {boolean} [params.isOpen]
* @param {integer} params.limit max number of records shown on screen (pager size)
* @param {string} params.modelName
* @param {integer} [params.offset]
* @param {boolean} [params.openGroupByDefault]
* @param {Object[]} [params.orderedBy=[]]
* @param {integer[]} [params.orderedResIDs]
* @param {string} [params.parentID] model name ID of the parent model
* @param {Object} [params.rawContext]
* @param {[type]} [params.ref]
* @param {string} [params.relationField]
* @param {integer|null} [params.res_id] actual id of record in the server
* @param {integer[]} [params.res_ids] context in which the data point is used, from a list of res_id
* @param {boolean} [params.static=false]
* @param {string} [params.type='record'|'list']
* @param {[type]} [params.value]
* @param {string} [params.viewType] the type of the view, e.g. 'list' or 'form'
* @returns {Object} the resource created
*/
_makeDataPoint: function (params) {
@@ -3069,13 +3092,23 @@ var BasicModel = AbstractModel.extend({
id: {type: 'integer'},
}, params.fields);
// datapoint context is extended by fields widgets
var context = _.extend({}, params.context);
if (params.fieldsInfo && params.viewType) {
_.each(params.fieldsInfo[params.viewType], function (field) {
if (field.context) {
_.extend(context, field.context);
}
});
}
var dataPoint = {
_cache: type === 'list' ? {} : undefined,
_changes: null,
_domains: {},
_rawChanges: {},
aggregateValues: params.aggregateValues || {},
context: params.context || {},
context: context,
count: params.count || res_ids.length,
data: data,
domain: params.domain || [],
@@ -6916,7 +6916,6 @@ QUnit.module('relational_fields', {
if (args.method === 'read' && args.model === 'partner') {
assert.deepEqual(args.kwargs.context, {
active_field: 2,
bin_size: true,
someKey: 'some value',
}, "sent context should be correct");
}
@@ -74,7 +74,6 @@ QUnit.module('Views', {
mockRPC: function (route, args) {
assert.deepEqual(args.kwargs.context, {
active_field: 2,
bin_size: true,
someKey: 'some value',
}, "should have sent the correct context");
return this._super.apply(this, arguments);
+1 -3
View File
@@ -199,7 +199,7 @@ QUnit.module('Views', {
});
QUnit.test('only necessary fields are fetched with correct context', function (assert) {
assert.expect(2);
assert.expect(1);
var form = createView({
View: FormView,
@@ -214,8 +214,6 @@ QUnit.module('Views', {
// field, not sure why. Maybe this test should be modified.
assert.deepEqual(args.args[1], ["foo", "display_name"],
"should only fetch requested fields");
assert.deepEqual(args.kwargs.context, {bin_size: true},
"bin_size should always be in the context");
return this._super(route, args);
}
});