summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Kühn <aku@odoo.com>2018-04-09 14:32:37 +0200
committerGéry Debongnie <ged@odoo.com>2018-04-10 17:41:12 +0200
commit8dbc1d95c36b18398c7284eddda2fe48842a8904 (patch)
tree35c0de6d39b5ecbcd1c77437c5408fa4f8b6a849
parent36b4468d15bf96de097cfc3d7e2d5f99febb984b (diff)
[IMP] web: many2one as measure in graph & pivot only when desirable
With this commit, the fields many2one are not always measures in pivot and graph views. By default, they are *not* measures. In order to have them as measures, we should pass `additionalMeasures` as a param to any of the views, which contains a list of fields to consider as measures. example: Suppose a model with the following fields: - `product_id` (many2one) - `categ_id` (many2one) - `sold` (float) By default, only the field `sold` is a measure in a graph or pivot view. In order to pass `product_id` as a measure of a graph view, you should instantiate this view as follow: `` var viewInfo = /* ... */; var params = { additionalMeasures = ['product_id'], }; var graph = new GraphView(viewInfo, params); `` An exception to the rule above is when the many2one field is of type `measure` in the arch: this field is set as the default measure of the view, therefore it is implicitly added as an additional measure.
-rw-r--r--addons/web/static/src/js/views/graph/graph_view.js12
-rw-r--r--addons/web/static/src/js/views/pivot/pivot_view.js8
-rw-r--r--addons/web/static/src/xml/base.xml2
-rw-r--r--addons/web/static/tests/views/graph_tests.js46
-rw-r--r--addons/web/static/tests/views/pivot_tests.js40
5 files changed, 101 insertions, 7 deletions
diff --git a/addons/web/static/src/js/views/graph/graph_view.js b/addons/web/static/src/js/views/graph/graph_view.js
index 8cfe2dc0272..54ff6642b15 100644
--- a/addons/web/static/src/js/views/graph/graph_view.js
+++ b/addons/web/static/src/js/views/graph/graph_view.js
@@ -36,12 +36,15 @@ var GraphView = AbstractView.extend({
/**
* @override
*/
- init: function () {
+ init: function (viewInfo, params) {
this._super.apply(this, arguments);
+ var self = this;
var measure;
var groupBys = [];
+ var measures = {__count__: {string: _t("Count"), type: "integer"}};
this.fields.__count__ = {string: _t("Count"), type: "integer"};
+
this.arch.children.forEach(function (field) {
var name = field.attrs.name;
if (field.attrs.interval) {
@@ -49,16 +52,17 @@ var GraphView = AbstractView.extend({
}
if (field.attrs.type === 'measure') {
measure = name;
+ measures[name] = self.fields[name];
} else {
groupBys.push(name);
}
});
- var measures = {__count__: {string: _t("Count"), type: "integer"}};
_.each(this.fields, function (field, name) {
if (name !== 'id' && field.store === true) {
- if (field.type === 'integer' || field.type === 'float' || field.type === 'monetary' || field.type === 'many2one') {
- measures[name] = field;
+ if (_.contains(['integer', 'float', 'monetary'], field.type) ||
+ _.contains(params.additionalMeasures, name)) {
+ measures[name] = field;
}
}
});
diff --git a/addons/web/static/src/js/views/pivot/pivot_view.js b/addons/web/static/src/js/views/pivot/pivot_view.js
index 199042ec12a..e0b232b6310 100644
--- a/addons/web/static/src/js/views/pivot/pivot_view.js
+++ b/addons/web/static/src/js/views/pivot/pivot_view.js
@@ -35,6 +35,8 @@ var PivotView = AbstractView.extend({
init: function (viewInfo, params) {
this._super.apply(this, arguments);
+ var self = this;
+
var activeMeasures = [];
var colGroupBys = [];
var rowGroupBys = [];
@@ -45,8 +47,9 @@ var PivotView = AbstractView.extend({
this.fields.__count = {string: _t("Count"), type: "integer"};
_.each(this.fields, function (field, name) {
if ((name !== 'id') && (field.store === true)) {
- if (_.contains(['integer', 'float', 'monetary', 'many2one'], field.type)) {
- measures[name] = field;
+ if (_.contains(['integer', 'float', 'monetary'], field.type) ||
+ _.contains(params.additionalMeasures, name)) {
+ measures[name] = field;
}
if (_.contains(GROUPABLE_TYPES, field.type)) {
groupableFields[name] = field;
@@ -73,6 +76,7 @@ var PivotView = AbstractView.extend({
}
if (field.attrs.type === 'measure' || 'operator' in field.attrs) {
activeMeasures.push(name);
+ measures[name] = self.fields[name];
}
if (field.attrs.type === 'col') {
colGroupBys.push(name);
diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml
index 96f80c96eb2..0cd931553f0 100644
--- a/addons/web/static/src/xml/base.xml
+++ b/addons/web/static/src/xml/base.xml
@@ -498,7 +498,7 @@
</button>
<ul class="dropdown-menu o_graph_measures_list">
<li t-foreach="measures" t-as="measure" t-att-data-field="measure[0]">
- <a href="#"><t t-esc="measure[1].string"/></a>
+ <a href="#"><t t-esc="measure[1].string"/><t t-if="measure[1].type === 'many2one'"> (count)</t></a>
</li>
<li class="divider"></li>
<li data-field="__count__"><a href="#">Count</a></li>
diff --git a/addons/web/static/tests/views/graph_tests.js b/addons/web/static/tests/views/graph_tests.js
index d383194dc6f..a13130e4b27 100644
--- a/addons/web/static/tests/views/graph_tests.js
+++ b/addons/web/static/tests/views/graph_tests.js
@@ -540,6 +540,9 @@ QUnit.module('Views', {
arch: '<graph string="Partners">' +
'<field name="product_id" type="row"/>' +
'</graph>',
+ viewOptions: {
+ additionalMeasures: ['product_id'],
+ },
});
var done = assert.async();
return concurrency.delay(0).then(function () {
@@ -556,6 +559,49 @@ QUnit.module('Views', {
done();
});
});
+
+ QUnit.test('not use a many2one as a measure by default', function (assert) {
+ assert.expect(1);
+
+ var graph = createView({
+ View: GraphView,
+ model: "foo",
+ data: this.data,
+ arch: '<graph string="Partners">' +
+ '<field name="product_id"/>' +
+ '</graph>',
+ });
+ var done = assert.async();
+ return concurrency.delay(0).then(function () {
+ assert.notOk(graph.measures.product_id,
+ "should not have product_id as measure");
+ graph.destroy();
+ done();
+ });
+ });
+
+ QUnit.test('use a many2one as a measure if set as additional fields', function (assert) {
+ assert.expect(1);
+
+ var graph = createView({
+ View: GraphView,
+ model: "foo",
+ data: this.data,
+ arch: '<graph string="Partners">' +
+ '<field name="product_id"/>' +
+ '</graph>',
+ viewOptions: {
+ additionalMeasures: ['product_id'],
+ },
+ });
+ var done = assert.async();
+ return concurrency.delay(0).then(function () {
+ assert.ok(graph.measures.product_id,
+ "should have product_id as measure");
+ graph.destroy();
+ done();
+ });
+ });
});
});
diff --git a/addons/web/static/tests/views/pivot_tests.js b/addons/web/static/tests/views/pivot_tests.js
index 446ab36e20e..a86ba3c28a6 100644
--- a/addons/web/static/tests/views/pivot_tests.js
+++ b/addons/web/static/tests/views/pivot_tests.js
@@ -967,6 +967,43 @@ QUnit.module('Views', {
pivot.destroy();
});
+ QUnit.test('not use a many2one as a measure by default', function (assert) {
+ assert.expect(1);
+
+ var pivot = createView({
+ View: PivotView,
+ model: "partner",
+ data: this.data,
+ arch: '<pivot>' +
+ '<field name="product_id"/>' +
+ '<field name="date" interval="month" type="col"/>' +
+ '</pivot>',
+ });
+ assert.notOk(pivot.measures.product_id,
+ "should not have product_id as measure");
+ pivot.destroy();
+ });
+
+ QUnit.test('use a many2one as a measure with specified additional measure', function (assert) {
+ assert.expect(1);
+
+ var pivot = createView({
+ View: PivotView,
+ model: "partner",
+ data: this.data,
+ arch: '<pivot>' +
+ '<field name="product_id"/>' +
+ '<field name="date" interval="month" type="col"/>' +
+ '</pivot>',
+ viewOptions: {
+ additionalMeasures: ['product_id'],
+ },
+ });
+ assert.ok(pivot.measures.product_id,
+ "should have product_id as measure");
+ pivot.destroy();
+ });
+
QUnit.test('pivot view with many2one field as a measure', function (assert) {
assert.expect(1);
@@ -1015,6 +1052,9 @@ QUnit.module('Views', {
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'</pivot>',
+ viewOptions: {
+ additionalMeasures: ['product_id'],
+ },
});
pivot.$buttons.find('li[data-field=product_id] a').click();