mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[IMP] web_pivot_computed_measure: black, isort, prettier
This commit is contained in:
committed by
CarlosRoca13
parent
4182a77259
commit
bea8716db7
@@ -1,32 +1,35 @@
|
||||
/* Copyright 2020 Tecnativa - Alexandre Díaz
|
||||
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */
|
||||
|
||||
odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
odoo.define("web_pivot_computed_measure.PivotModel", function(require) {
|
||||
"use strict";
|
||||
|
||||
var core = require('web.core');
|
||||
var PivotModel = require('web.PivotModel');
|
||||
var ComparisonUtils = require('web.dataComparisonUtils');
|
||||
var core = require("web.core");
|
||||
var PivotModel = require("web.PivotModel");
|
||||
var ComparisonUtils = require("web.dataComparisonUtils");
|
||||
|
||||
var _t = core._t;
|
||||
|
||||
|
||||
PivotModel.include({
|
||||
_computed_measures: [],
|
||||
|
||||
/**
|
||||
* Create a new computed measure
|
||||
*
|
||||
* @param {string} id
|
||||
* @param {string} field1
|
||||
* @param {string} field2
|
||||
* @param {string} operation
|
||||
* @param {string} name
|
||||
* @param {string} format
|
||||
* @param {String} id
|
||||
* @param {String} field1
|
||||
* @param {String} field2
|
||||
* @param {String} operation
|
||||
* @param {String} name
|
||||
* @param {String} format
|
||||
*/
|
||||
createComputedMeasure: function(id, field1, field2, operation, name, format) {
|
||||
var measure = _.find(this._computed_measures, function(item) {
|
||||
return item.field1 === field1 && item.field2 === field2 && item.operation === operation;
|
||||
return (
|
||||
item.field1 === field1 &&
|
||||
item.field2 === field2 &&
|
||||
item.operation === operation
|
||||
);
|
||||
});
|
||||
if (measure) {
|
||||
return $.Deferred(function(d) {
|
||||
@@ -35,13 +38,17 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
}
|
||||
var fieldM1 = this.fields[field1];
|
||||
var fieldM2 = this.fields[field2];
|
||||
var cmId = '__computed_' + id;
|
||||
var cmId = "__computed_" + id;
|
||||
var oper = operation.replace(/m1/g, field1).replace(/m2/g, field2);
|
||||
var oper_human = operation.replace(
|
||||
/m1/g,
|
||||
fieldM1.__computed_id?"("+fieldM1.string+")":fieldM1.string).replace(
|
||||
/m2/g,
|
||||
fieldM2.__computed_id?"("+fieldM2.string+")":fieldM2.string);
|
||||
var oper_human = operation
|
||||
.replace(
|
||||
/m1/g,
|
||||
fieldM1.__computed_id ? "(" + fieldM1.string + ")" : fieldM1.string
|
||||
)
|
||||
.replace(
|
||||
/m2/g,
|
||||
fieldM2.__computed_id ? "(" + fieldM2.string + ")" : fieldM2.string
|
||||
);
|
||||
var cmTotal = this._computed_measures.push({
|
||||
field1: field1,
|
||||
field2: field2,
|
||||
@@ -51,7 +58,7 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
format: format,
|
||||
});
|
||||
|
||||
return this._createVirtualMeasure(this._computed_measures[cmTotal-1]);
|
||||
return this._createVirtualMeasure(this._computed_measures[cmTotal - 1]);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -64,10 +71,10 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
var arrFields = fields || this.fields;
|
||||
// This is a minimal 'fake' field info
|
||||
arrFields[cmDef.id] = {
|
||||
type: cmDef.format, // Used to format the value
|
||||
string: cmDef.name, // Used to print the header name
|
||||
__computed_id: cmDef.id, // Used to know if is a computed measure field
|
||||
}
|
||||
type: cmDef.format, // Used to format the value
|
||||
string: cmDef.name, // Used to print the header name
|
||||
__computed_id: cmDef.id, // Used to know if is a computed measure field
|
||||
};
|
||||
this.trigger_up("add_measure", {
|
||||
id: cmDef.id,
|
||||
def: arrFields[cmDef.id],
|
||||
@@ -93,7 +100,7 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
}
|
||||
return $.Deferred(function(d) {
|
||||
d.resolve();
|
||||
})
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -109,22 +116,22 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
* @param {Object} dataPoint
|
||||
* @param {Object} dataPointComp
|
||||
*/
|
||||
_fillComputedMeasuresData: function (dataPoint, dataPointComp) {
|
||||
_fillComputedMeasuresData: function(dataPoint, dataPointComp) {
|
||||
var self = this;
|
||||
_.each(this._computed_measures, function (cm) {
|
||||
_.each(this._computed_measures, function(cm) {
|
||||
if (!self._isMeasureEnabled(cm.id)) {
|
||||
return;
|
||||
}
|
||||
if (dataPointComp) {
|
||||
var resData =
|
||||
py.eval(cm.operation, dataPointComp.data);
|
||||
var resComparison =
|
||||
py.eval(cm.operation, dataPointComp.comparison);
|
||||
var resData = py.eval(cm.operation, dataPointComp.data);
|
||||
var resComparison = py.eval(cm.operation, dataPointComp.comparison);
|
||||
dataPoint[cm.id] = {
|
||||
data: resData,
|
||||
comparisonData: resComparison,
|
||||
variation: ComparisonUtils.computeVariation(
|
||||
resData, resComparison),
|
||||
resData,
|
||||
resComparison
|
||||
),
|
||||
};
|
||||
} else {
|
||||
dataPoint[cm.id] = py.eval(cm.operation, dataPoint);
|
||||
@@ -137,7 +144,7 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_mergeData: function (data, comparisonData, groupBys) {
|
||||
_mergeData: function(data, comparisonData, groupBys) {
|
||||
var res = this._super.apply(this, arguments);
|
||||
var len = groupBys.length; // Cached loop (This is not python! hehe)
|
||||
for (var index = 0; index < len; ++index) {
|
||||
@@ -148,24 +155,23 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
if (_.isEmpty(dataPoint)) {
|
||||
break;
|
||||
}
|
||||
if ('__comparisonDomain' in dataPoint) {
|
||||
if ("__comparisonDomain" in dataPoint) {
|
||||
// Transform comparison dataPoint object to be compatible
|
||||
var pairsDataPoint = _.pairs(dataPoint);
|
||||
var dataPointComp = {
|
||||
data: _.object(_.map(
|
||||
pairsDataPoint, (item) => {
|
||||
return [
|
||||
item[0],
|
||||
item[1] && item[1].data,
|
||||
];
|
||||
})),
|
||||
comparison: _.object(_.map(
|
||||
pairsDataPoint, (item) => {
|
||||
data: _.object(
|
||||
_.map(pairsDataPoint, item => {
|
||||
return [item[0], item[1] && item[1].data];
|
||||
})
|
||||
),
|
||||
comparison: _.object(
|
||||
_.map(pairsDataPoint, item => {
|
||||
return [
|
||||
item[0],
|
||||
item[1] && item[1].comparisonData,
|
||||
];
|
||||
})),
|
||||
})
|
||||
),
|
||||
};
|
||||
// Update datas. Required by computed measures that uses
|
||||
// other computed measures to work
|
||||
@@ -188,9 +194,12 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
load: function (params) {
|
||||
load: function(params) {
|
||||
var self = this;
|
||||
this._computed_measures = params.context.pivot_computed_measures || params.computed_measures || [];
|
||||
this._computed_measures =
|
||||
params.context.pivot_computed_measures ||
|
||||
params.computed_measures ||
|
||||
[];
|
||||
var toActive = [];
|
||||
var l = this._computed_measures.length;
|
||||
for (var x = 0; x < l; ++x) {
|
||||
@@ -199,7 +208,7 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
type: cmDef.format,
|
||||
string: cmDef.name,
|
||||
__computed_id: cmDef.id,
|
||||
}
|
||||
};
|
||||
toActive.push(cmDef.field1, cmDef.field2, cmDef.id);
|
||||
}
|
||||
return this._super(params).then(function() {
|
||||
@@ -221,9 +230,12 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
reload: function (handle, params) {
|
||||
if ('context' in params) {
|
||||
this._computed_measures = params.context.pivot_computed_measures || params.computed_measures || [];
|
||||
reload: function(handle, params) {
|
||||
if ("context" in params) {
|
||||
this._computed_measures =
|
||||
params.context.pivot_computed_measures ||
|
||||
params.computed_measures ||
|
||||
[];
|
||||
}
|
||||
var l = this._computed_measures.length;
|
||||
for (var x = 0; x < l; ++x) {
|
||||
@@ -234,10 +246,13 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
for (var x = 0; x < fieldNames.length; ++x) {
|
||||
var field = this.fields[fieldNames[x]];
|
||||
if (field.__computed_id) {
|
||||
var cm = _.find(this._computed_measures, {id:field.__computed_id});
|
||||
var cm = _.find(this._computed_measures, {id: field.__computed_id});
|
||||
if (!cm) {
|
||||
delete this.fields[fieldNames[x]];
|
||||
this.data.measures = _.without(this.data.measures, fieldNames[x]);
|
||||
this.data.measures = _.without(
|
||||
this.data.measures,
|
||||
fieldNames[x]
|
||||
);
|
||||
this.trigger_up("remove_measure", {
|
||||
id: fieldNames[x],
|
||||
});
|
||||
@@ -252,7 +267,7 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
get: function () {
|
||||
get: function() {
|
||||
var res = this._super.apply(this, arguments);
|
||||
res.computed_measures = this._computed_measures;
|
||||
return res;
|
||||
@@ -264,15 +279,19 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
toggleMeasure: function (field) {
|
||||
toggleMeasure: function(field) {
|
||||
if (this._isMeasureEnabled(field)) {
|
||||
// Measure is disabled
|
||||
var umeasures = _.filter(this._computed_measures, function(item) {
|
||||
return item.field1 === field || item.field2 === field;
|
||||
})
|
||||
});
|
||||
if (umeasures.length && this._isMeasureEnabled(umeasures[0].id)) {
|
||||
return $.Deferred(function(d) {
|
||||
d.reject(_t("This measure is currently used by a 'computed measure'. Please, disable the computed measure first."));
|
||||
d.reject(
|
||||
_t(
|
||||
"This measure is currently used by a 'computed measure'. Please, disable the computed measure first."
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -283,7 +302,9 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
var afield = toAnalize.shift();
|
||||
var fieldDef = this.fields[afield];
|
||||
if (fieldDef.__computed_id) {
|
||||
var cm = _.find(this._computed_measures, {id:fieldDef.__computed_id});
|
||||
var cm = _.find(this._computed_measures, {
|
||||
id: fieldDef.__computed_id,
|
||||
});
|
||||
toAnalize.push(cm.field1, cm.field2);
|
||||
var toEnableFields = [];
|
||||
if (!this.fields[cm.field1].__computed_id) {
|
||||
@@ -303,5 +324,4 @@ odoo.define('web_pivot_computed_measure.PivotModel', function (require) {
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user