[IMP] work in progress: adds the export functionality in graph view (addon web_graph)
bzr revid: ged@openerp.com-20140203113332-fvr9cra2dhj4h0ju
This commit is contained in:
parent
1c42440313
commit
882fde48f0
|
@ -0,0 +1 @@
|
||||||
|
import controllers
|
|
@ -0,0 +1 @@
|
||||||
|
import main
|
|
@ -0,0 +1,57 @@
|
||||||
|
from openerp import http
|
||||||
|
import simplejson
|
||||||
|
from openerp.http import request, serialize_exception as _serialize_exception
|
||||||
|
|
||||||
|
try:
|
||||||
|
import xlwt
|
||||||
|
except ImportError:
|
||||||
|
xlwt = None
|
||||||
|
|
||||||
|
|
||||||
|
class TableExporter(http.Controller):
|
||||||
|
|
||||||
|
@http.route('/web_graph/check_xlwt', type='json', auth='none')
|
||||||
|
def check_xlwt(self):
|
||||||
|
return xlwt is not None
|
||||||
|
|
||||||
|
|
||||||
|
@http.route('/web_graph/export_xls', type='http', auth="user")
|
||||||
|
def export_xls(self, data, token):
|
||||||
|
jdata = simplejson.loads(data)
|
||||||
|
model = jdata['test']
|
||||||
|
# field = jdata['field']
|
||||||
|
# data = jdata['data']
|
||||||
|
# id = jdata.get('id', None)
|
||||||
|
# filename_field = jdata.get('filename_field', None)
|
||||||
|
# context = jdata.get('context', {})
|
||||||
|
filecontent='argh'
|
||||||
|
print model
|
||||||
|
print xlwt
|
||||||
|
|
||||||
|
return request.make_response(filecontent,
|
||||||
|
headers=[('Content-Type', 'application/vnd.ms-excel'),
|
||||||
|
('Content-Disposition', 'attachment; filename=table.xls;')],
|
||||||
|
cookies={'fileToken': token})
|
||||||
|
|
||||||
|
# Model = request.session.model(model)
|
||||||
|
# fields = [field]
|
||||||
|
# if filename_field:
|
||||||
|
# fields.append(filename_field)
|
||||||
|
# if data:
|
||||||
|
# res = { field: data }
|
||||||
|
# elif id:
|
||||||
|
# res = Model.read([int(id)], fields, context)[0]
|
||||||
|
# else:
|
||||||
|
# res = Model.default_get(fields, context)
|
||||||
|
# filecontent = base64.b64decode(res.get(field, ''))
|
||||||
|
# if not filecontent:
|
||||||
|
# raise ValueError(_("No content found for field '%s' on '%s:%s'") %
|
||||||
|
# (field, model, id))
|
||||||
|
# else:
|
||||||
|
# filename = '%s_%s' % (model.replace('.', '_'), id)
|
||||||
|
# if filename_field:
|
||||||
|
# filename = res.get(filename_field, '') or filename
|
||||||
|
# return request.make_response(filecontent,
|
||||||
|
# headers=[('Content-Type', 'application/octet-stream'),
|
||||||
|
# ('Content-Disposition', content_disposition(filename))],
|
||||||
|
# cookies={'fileToken': token})
|
|
@ -39,6 +39,10 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
|
||||||
this.$('.graph_heatmap label').addClass('disabled');
|
this.$('.graph_heatmap label').addClass('disabled');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
openerp.session.rpc('/web_graph/check_xlwt').then(function (result) {
|
||||||
|
if (result) { self.$('.graph_options_selection label').toggle(true); }
|
||||||
|
});
|
||||||
|
|
||||||
return this.model.call('fields_get', []).then(function (f) {
|
return this.model.call('fields_get', []).then(function (f) {
|
||||||
self.fields = f;
|
self.fields = f;
|
||||||
self.fields.__count = {field:'__count', type: 'integer', string:_t('Quantity')};
|
self.fields.__count = {field:'__count', type: 'integer', string:_t('Quantity')};
|
||||||
|
@ -264,6 +268,9 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
|
||||||
case 'update_values':
|
case 'update_values':
|
||||||
this.pivot.update_data().then(this.proxy('display_data'));
|
this.pivot.update_data().then(this.proxy('display_data'));
|
||||||
break;
|
break;
|
||||||
|
case 'export_data':
|
||||||
|
this.export_xls();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -362,7 +369,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
|
||||||
this.$('.graph_main_content svg').remove();
|
this.$('.graph_main_content svg').remove();
|
||||||
this.$('.graph_main_content div').remove();
|
this.$('.graph_main_content div').remove();
|
||||||
this.table.empty();
|
this.table.empty();
|
||||||
this.table.toggleClass('heatmap', this.heatmap_mode !== 'none')
|
this.table.toggleClass('heatmap', this.heatmap_mode !== 'none');
|
||||||
this.width = this.$el.width();
|
this.width = this.$el.width();
|
||||||
this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
|
this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
|
||||||
|
|
||||||
|
@ -693,6 +700,91 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
// Convert Pivot data structure into table structure
|
||||||
|
// table = {headers: [header_rows],
|
||||||
|
// measure_row: [_],
|
||||||
|
// rows: [row]}
|
||||||
|
// header_rows = [header_cells],
|
||||||
|
// header_cells = {width:_, height:_, title:_, id:_}
|
||||||
|
// rows = {id:_, indent:_, title:_, cells: [cell]}
|
||||||
|
// cell = {row_id:_, col_id:_, value:_}
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
build_table: function() {
|
||||||
|
return {
|
||||||
|
headers: this.build_headers(),
|
||||||
|
measure_row: this.build_measure_row(),
|
||||||
|
rows: this.build_rows()
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
build_headers: function () {
|
||||||
|
var pivot = this.pivot,
|
||||||
|
height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
|
||||||
|
rows = [];
|
||||||
|
|
||||||
|
_.each(pivot.cols.headers, function (col) {
|
||||||
|
if (col.path.length === 0) { return;}
|
||||||
|
var cell_width = col.expanded ? pivot.get_ancestors(col).length : 1;
|
||||||
|
var cell_height = height - col.path.length + 1;
|
||||||
|
var cell = {width: cell_width, height: cell_height, title: col.title, id: col.id};
|
||||||
|
debugger;
|
||||||
|
if (rows[col.path.length - 1]) {
|
||||||
|
rows[col.path.length - 1].push(cell);
|
||||||
|
} else {
|
||||||
|
rows[col.path.length - 1] = [cell];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
},
|
||||||
|
|
||||||
|
build_measure_row: function () {
|
||||||
|
var nbr_leaves = this.pivot.get_cols_leaves().length,
|
||||||
|
nbr_cols = nbr_leaves + ((nbr_leaves > 1) ? 1 : 0),
|
||||||
|
result = [],
|
||||||
|
i, m;
|
||||||
|
for (i = 0; i < nbr_cols; i++) {
|
||||||
|
for (m = 0; m < this.pivot.measures.length; m++) {
|
||||||
|
result.push(this.pivot.measures[m].string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
build_rows: function () {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
// Controller stuff...
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
export_xls: function() {
|
||||||
|
var c = openerp.webclient.crashmanager;
|
||||||
|
|
||||||
|
var table = this.build_table();
|
||||||
|
console.log(table);
|
||||||
|
return;
|
||||||
|
openerp.web.blockUI();
|
||||||
|
this.session.get_file({
|
||||||
|
url: '/web_graph/export_xls',
|
||||||
|
data: {data: JSON.stringify({
|
||||||
|
test: table
|
||||||
|
// model: this.view.dataset.model,
|
||||||
|
// id: (this.view.datarecord.id || ''),
|
||||||
|
// field: this.name,
|
||||||
|
// filename_field: (this.node.attrs.filename || ''),
|
||||||
|
// data: instance.web.form.is_bin_size(value) ? null : value,
|
||||||
|
// context: this.view.dataset.get_context()
|
||||||
|
})},
|
||||||
|
complete: openerp.web.unblockUI,
|
||||||
|
error: c.rpc_error.bind(c)
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Utility function: returns true if the beginning of array2 is array1 and
|
// Utility function: returns true if the beginning of array2 is array1 and
|
||||||
|
|
|
@ -127,6 +127,10 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
|
||||||
return this._get_headers_with_depth(this.rows.headers, depth);
|
return this._get_headers_with_depth(this.rows.headers, depth);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
get_ancestor_leaves: function (header) {
|
||||||
|
return _.where(this.get_ancestors_and_self(header), {expanded:false});
|
||||||
|
},
|
||||||
|
|
||||||
// return all non expanded rows
|
// return all non expanded rows
|
||||||
get_rows_leaves: function () {
|
get_rows_leaves: function () {
|
||||||
return _.where(this.rows.headers, {expanded:false});
|
return _.where(this.rows.headers, {expanded:false});
|
||||||
|
|
|
@ -41,6 +41,9 @@
|
||||||
<label class="btn btn-default" data-choice="update_values" title="Reload Data">
|
<label class="btn btn-default" data-choice="update_values" title="Reload Data">
|
||||||
<span class="fa fa-refresh"></span>
|
<span class="fa fa-refresh"></span>
|
||||||
</label>
|
</label>
|
||||||
|
<label class="btn btn-default" data-choice="export_data" title="Export Data" style="display:none">
|
||||||
|
<span class="fa fa-download"></span>
|
||||||
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<label class="btn btn-default dropdown-toggle" data-toggle="dropdown">
|
<label class="btn btn-default dropdown-toggle" data-toggle="dropdown">
|
||||||
|
|
Loading…
Reference in New Issue