[FIX] many small tweaks, add some comment, rename variables to have better more informative names in excel export functionality in graph view (addon web_graph)

bzr revid: ged@openerp.com-20140205131406-kdw5ht9u8gct9xbo
This commit is contained in:
Gery Debongnie 2014-02-05 14:14:06 +01:00
parent 19d6d98da8
commit d0961eb09e
2 changed files with 35 additions and 32 deletions

View File

@ -2,6 +2,7 @@ from openerp import http
import simplejson import simplejson
from openerp.http import request, serialize_exception as _serialize_exception from openerp.http import request, serialize_exception as _serialize_exception
from cStringIO import StringIO from cStringIO import StringIO
from collections import deque
try: try:
import xlwt import xlwt
@ -20,68 +21,68 @@ class TableExporter(http.Controller):
jdata = simplejson.loads(data) jdata = simplejson.loads(data)
nbr_measures = jdata['nbr_measures'] nbr_measures = jdata['nbr_measures']
workbook = xlwt.Workbook() workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet 1') worksheet = workbook.add_sheet(jdata['title'])
bold_style = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;") header_bold = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;")
non_bold_style = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;") header_plain = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;")
bold = xlwt.easyxf("font: bold on;") bold = xlwt.easyxf("font: bold on;")
# Step 1: writing headers # Step 1: writing headers
headers = jdata['headers'] headers = jdata['headers']
x, y, L = 1, 0, []
# x,y: current coordinates
# carry: queue containing cell information when a cell has a >= 2 height
# and the drawing code needs to add empty cells below
x, y, carry = 1, 0, deque()
for i, header_row in enumerate(headers): for i, header_row in enumerate(headers):
worksheet.write(i,0, '', non_bold_style) worksheet.write(i,0, '', header_plain)
for header in header_row: for header in header_row:
while (L and L[0]['x'] == x): while (carry and carry[0]['x'] == x):
cell = L.pop(0) cell = carry.popleft()
for i in range(nbr_measures): for i in range(nbr_measures):
worksheet.write(y,x+i, '', non_bold_style) worksheet.write(y, x+i, '', header_plain)
if cell['height'] > 1: if cell['height'] > 1:
L.append({'x': x, 'height':cell['height'] - 1}) carry.append({'x': x, 'height':cell['height'] - 1})
x = x + nbr_measures x = x + nbr_measures
style = non_bold_style if 'expanded' in header else bold_style style = header_plain if 'expanded' in header else header_bold
for i in range(header['width']): for i in range(header['width']):
worksheet.write(y, x + i, header['title'] if i == 0 else '', style) worksheet.write(y, x + i, header['title'] if i == 0 else '', style)
if header['height'] > 1: if header['height'] > 1:
L.append({'x': x, 'height':header['height'] - 1}) carry.append({'x': x, 'height':header['height'] - 1})
x = x + header['width']; x = x + header['width'];
while (L and L[0]['x'] == x): while (carry and carry[0]['x'] == x):
cell = L.pop(0) cell = carry.popleft()
for i in range(nbr_measures): for i in range(nbr_measures):
worksheet.write(y,x+i, '', non_bold_style) worksheet.write(y, x+i, '', header_plain)
if cell['height'] > 1: if cell['height'] > 1:
L.append({'x': x, 'height':cell['height'] - 1}) carry.append({'x': x, 'height':cell['height'] - 1})
x = x + nbr_measures x = x + nbr_measures
x, y = 1, y + 1 x, y = 1, y + 1
# Step 2: measure row # Step 2: measure row
if nbr_measures > 1: if nbr_measures > 1:
worksheet.write(y,0, '', non_bold_style) worksheet.write(y,0, '', header_plain)
for measure in jdata['measure_row']: for measure in jdata['measure_row']:
style = bold_style if measure['is_bold'] else non_bold_style style = header_bold if measure['is_bold'] else header_plain
worksheet.write(y,x, measure['text'], style); worksheet.write(y, x, measure['text'], style);
x = x + 1 x = x + 1
y = y + 1 y = y + 1
# Step 3: writing data # Step 3: writing data
x = 0 x = 0
for row in jdata['rows']: for row in jdata['rows']:
worksheet.write(y, x, row['indent'] * ' ' + row['title'], non_bold_style) worksheet.write(y, x, row['indent'] * ' ' + row['title'], header_plain)
for cell in row['cells']: for cell in row['cells']:
x = x + 1 x = x + 1
if cell.get('is_bold', False): if cell.get('is_bold', False):
worksheet.write(y,x, cell['value'], bold) worksheet.write(y, x, cell['value'], bold)
else: else:
worksheet.write(y,x, cell['value']) worksheet.write(y, x, cell['value'])
x, y = 0, y + 1 x, y = 0, y + 1
fp = StringIO() response = request.make_response(None,
workbook.save(fp)
fp.seek(0)
filecontent = fp.read()
fp.close()
return request.make_response(filecontent,
headers=[('Content-Type', 'application/vnd.ms-excel'), headers=[('Content-Type', 'application/vnd.ms-excel'),
('Content-Disposition', 'attachment; filename=table.xls;')], ('Content-Disposition', 'attachment; filename=table.xls;')],
cookies={'fileToken': token}) cookies={'fileToken': token})
workbook.save(response.stream)
return response

View File

@ -25,6 +25,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
this.bar_ui = options.bar_ui || 'group'; this.bar_ui = options.bar_ui || 'group';
this.graph_view = options.graph_view || null; this.graph_view = options.graph_view || null;
this.pivot_options = options; this.pivot_options = options;
this.title = options.title || 'Data';
}, },
start: function() { start: function() {
@ -40,7 +41,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
} }
openerp.session.rpc('/web_graph/check_xlwt').then(function (result) { openerp.session.rpc('/web_graph/check_xlwt').then(function (result) {
if (result) { self.$('.graph_options_selection label').toggle(true); } self.$('.graph_options_selection label').toggle(result);
}); });
return this.model.call('fields_get', []).then(function (f) { return this.model.call('fields_get', []).then(function (f) {
@ -371,7 +372,8 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
headers: this.build_headers(), headers: this.build_headers(),
measure_row: this.build_measure_row(), measure_row: this.build_measure_row(),
rows: this.build_rows(), rows: this.build_rows(),
nbr_measures: this.pivot.measures.length nbr_measures: this.pivot.measures.length,
title: this.title,
}; };
}, },
@ -394,10 +396,10 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
}); });
if (pivot.get_cols_leaves().length > 1) { if (pivot.get_cols_leaves().length > 1) {
rows[0].push({width:nbr_measures, height:height, title: _t('Total'), id: pivot.cols.headers[0].id }); rows[0].push({width: nbr_measures, height: height, title: _t('Total'), id: pivot.main_col().id });
} }
if (pivot.cols.headers.length === 1) { if (pivot.cols.headers.length === 1) {
rows = [[{width:nbr_measures, height:1, title: _t('Total'), id: pivot.cols.headers[0].id, expanded: false}]]; rows = [[{width: nbr_measures, height: 1, title: _t('Total'), id: pivot.main_col().id, expanded: false}]];
} }
return rows; return rows;
}, },