[IMP] changes the backend code of graph view to use the new functionality of read_group (use 'field_name:interval' in readgroup instead of context (addon web_graph)

bzr revid: ged@openerp.com-20140122133452-wy3hrsrokhnwc3ru
This commit is contained in:
Gery Debongnie 2014-01-22 14:34:52 +01:00
parent d0806ed3f7
commit a3bb723379
3 changed files with 48 additions and 50 deletions

View File

@ -80,8 +80,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
this.graph_widget.appendTo(this.$el);
this.ViewManager.on('switch_mode', this, function (e) {
var domain = self.graph_widget.get_domain(),
col_gb = self.get_groupbys_from_searchview('ColGroupBy', 'col_group_by'),
var col_gb = self.get_groupbys_from_searchview('ColGroupBy', 'col_group_by'),
row_gb = self.get_groupbys_from_searchview('GroupBy', 'group_by');
if (e === 'graph') this.graph_widget.set(domain, row_gb, col_gb);
@ -95,7 +94,13 @@ instance.web_graph.GraphView = instance.web.View.extend({
get_groupbys_from_searchview: function (cat_name, cat_field) {
var facet = this.search_view.query.findWhere({category:cat_name}),
groupby_list = facet ? facet.values.models : [];
return _.map(groupby_list, function (g) { return g.attributes.value.attrs.context[cat_field]; });
return _.map(groupby_list, function (g) {
if (cat_name === 'GroupBy') {
return py.eval(g.attributes.value.attrs.context).group_by;
} else {
return g.attributes.value.attrs.context[cat_field];
}
});
},
do_show: function () {
@ -158,7 +163,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
make_groupby_values: function (groupbys, category) {
return _.map(groupbys, function (groupby) {
var context = {};
context[category] = groupby.field;
context[category] = groupby.interval ? groupby.field + ':' + groupby.interval : groupby.field;
var value = (category === 'group_by') ? groupby.filter : {attrs:{domain: [], context: context}};
return {
label: groupby.string,

View File

@ -177,13 +177,20 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
},
create_field_value: function (f) {
var important_field = _.findWhere(this.important_fields, {field:f}),
string = important_field ? important_field.string : this.fields[f].string,
result = {field: f, string: string, type: this.fields[f].type };
var field = f.field || ((_.contains(f, ':')) ? f.split(':')[0] : f),
important_field = _.findWhere(this.important_fields, {field:field}),
string = important_field ? important_field.string : this.fields[field].string,
result = {field: field, string: string, type: this.fields[field].type };
if (important_field) {
result.filter = important_field.filter;
}
if (f.interval) {
result.interval = f.interval;
}
if (_.contains(f, ':')) {
result.interval = f.split(':')[1];
}
return result;
},

View File

@ -221,6 +221,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
// make cells
_.each(self.get_ancestors_and_self(group), function (data) {
var values = _.map(self.measures, function (m) {
console.log('m.field', m.field);
return data.attributes.aggregates[m.field];
});
var other = _.find(otherRoot.headers, function (h) {
@ -230,6 +231,8 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
return _.isEqual(_.rest(data.path), h.path);
}
});
console.log('dbg', child.id, other.id, values);
if (_.isEqual(values, [46125])) debugger;
if (other) {
self.add_cell(child.id, other.id, values);
}
@ -378,37 +381,14 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
_query_db: function (groupby, fields, domain, path) {
var self = this,
field_ids = _.without(_.pluck(fields, 'field'), '__count'),
context = {};
formatted_groupby = (groupby.interval) ? groupby.field + ':' + groupby.interval : groupby.field;
if (groupby.interval) {
context.datetime_format = {};
var display_format;
switch (groupby.interval) {
case 'day':
display_format = 'dd MMMM YYYY';
break;
case 'week':
display_format = 'w YYYY';
break;
case 'month':
display_format = 'MMMM YYYY';
break;
case 'quarter':
display_format = 'QQQ YYYY';
break;
case 'year':
display_format = 'YYYY';
break;
}
context.datetime_format[groupby.field] = {
interval: groupby.interval,
display_format: display_format
};
}
fields = _.map(field_ids, function (field) {
return (_.contains(field, ':')) ? field.split(':')[0] : field;
});
return this.model.query(field_ids)
.filter(domain)
.context(context)
.group_by(groupby.field)
.group_by(formatted_groupby)
.then(function (results) {
var groups = _.filter(results, function (group) {
return group.attributes.length > 0;
@ -417,25 +397,32 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
});
},
// if field is a fieldname, returns field, if field is field_id:interval, retuns field_id
raw_field: function (field) {
return (_.contains(field, ':')) ? field.split(':')[0] : field;
},
// add the path to the group and sanitize the value...
format_group: function (group, current_path) {
var value = group.attributes.value;
format_group: function (group, current_path) {
var attrs = group.attributes,
value = attrs.value,
grouped_on = attrs.grouped_on ? this.raw_field(attrs.grouped_on) : false;
if (group.attributes.grouped_on && this.fields[group.attributes.grouped_on].type === 'selection') {
var selection = this.fields[group.attributes.grouped_on].selection,
value_lookup = _.where(selection, {0:value});
group.attributes.value = value_lookup ? value_lookup[1] : 'undefined';
} else if (value === false) {
group.attributes.value = 'undefined';
} else if (value instanceof Array) {
group.attributes.value = value[1];
}
if (grouped_on && this.fields[grouped_on].type === 'selection') {
var selection = this.fields[grouped_on].selection,
value_lookup = _.where(selection, {0:value});
group.attributes.value = value_lookup ? value_lookup[1] : 'undefined';
} else if (value === false) {
group.attributes.value = 'undefined';
} else if (value instanceof Array) {
group.attributes.value = value[1];
}
group.path = value ? (current_path || []).concat(group.attributes.value) : [];
group.path = (value !== undefined) ? (current_path || []).concat(group.attributes.value) : [];
group.attributes.aggregates.__count = group.attributes.length;
return group;
},
return group;
},
format_data: function (total, col_data, row_data, cell_data) {
var self = this,
@ -476,7 +463,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
var row = _.find(rows, function (header) { return _.isEqual(header.path, path.slice(index)); });
var col = _.find(cols, function (header) { return _.isEqual(header.path, path.slice(0, index)); });
self.add_cell(row.id, col.id, values);
if (group.children) {
self.make_cells (group.children, index, path, rows, cols);
}