From 882fde48f087121b0a58888c0c0ae9734fe8b419 Mon Sep 17 00:00:00 2001 From: Gery Debongnie Date: Mon, 3 Feb 2014 12:33:32 +0100 Subject: [IMP] work in progress: adds the export functionality in graph view (addon web_graph) bzr revid: ged@openerp.com-20140203113332-fvr9cra2dhj4h0ju --- addons/web_graph/controllers/main.py | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 addons/web_graph/controllers/main.py (limited to 'addons/web_graph/controllers/main.py') diff --git a/addons/web_graph/controllers/main.py b/addons/web_graph/controllers/main.py new file mode 100644 index 00000000000..1df85d08f59 --- /dev/null +++ b/addons/web_graph/controllers/main.py @@ -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}) -- cgit v1.2.3 From 6ddbb1f8c6903ccafce2afba22658626e6397383 Mon Sep 17 00:00:00 2001 From: Gery Debongnie Date: Mon, 3 Feb 2014 16:47:04 +0100 Subject: [IMP] work in progress: adds the export functionality in graph view (addon web_graph) bzr revid: ged@openerp.com-20140203154704-t2i3uvezupw7kn3n --- addons/web_graph/controllers/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web_graph/controllers/main.py') diff --git a/addons/web_graph/controllers/main.py b/addons/web_graph/controllers/main.py index 1df85d08f59..813f9426f69 100644 --- a/addons/web_graph/controllers/main.py +++ b/addons/web_graph/controllers/main.py @@ -18,7 +18,7 @@ class TableExporter(http.Controller): @http.route('/web_graph/export_xls', type='http', auth="user") def export_xls(self, data, token): jdata = simplejson.loads(data) - model = jdata['test'] + model = jdata['headers'] # field = jdata['field'] # data = jdata['data'] # id = jdata.get('id', None) -- cgit v1.2.3 From 19d6d98da83d9cb19d66741ed9377647ac34f762 Mon Sep 17 00:00:00 2001 From: Gery Debongnie Date: Wed, 5 Feb 2014 10:58:08 +0100 Subject: [IMP] completes the excel export functionality in graph view (addon web_graph) bzr revid: ged@openerp.com-20140205095808-8lzwv0yqcsjnvff9 --- addons/web_graph/controllers/main.py | 94 ++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 32 deletions(-) (limited to 'addons/web_graph/controllers/main.py') diff --git a/addons/web_graph/controllers/main.py b/addons/web_graph/controllers/main.py index 813f9426f69..589e9c6b76d 100644 --- a/addons/web_graph/controllers/main.py +++ b/addons/web_graph/controllers/main.py @@ -1,13 +1,13 @@ from openerp import http import simplejson from openerp.http import request, serialize_exception as _serialize_exception +from cStringIO import StringIO try: import xlwt except ImportError: xlwt = None - class TableExporter(http.Controller): @http.route('/web_graph/check_xlwt', type='json', auth='none') @@ -18,40 +18,70 @@ class TableExporter(http.Controller): @http.route('/web_graph/export_xls', type='http', auth="user") def export_xls(self, data, token): jdata = simplejson.loads(data) - model = jdata['headers'] - # 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 + nbr_measures = jdata['nbr_measures'] + workbook = xlwt.Workbook() + worksheet = workbook.add_sheet('Sheet 1') + bold_style = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;") + non_bold_style = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;") + bold = xlwt.easyxf("font: bold on;") + + # Step 1: writing headers + headers = jdata['headers'] + x, y, L = 1, 0, [] + for i, header_row in enumerate(headers): + worksheet.write(i,0, '', non_bold_style) + for header in header_row: + while (L and L[0]['x'] == x): + cell = L.pop(0) + for i in range(nbr_measures): + worksheet.write(y,x+i, '', non_bold_style) + if cell['height'] > 1: + L.append({'x': x, 'height':cell['height'] - 1}) + x = x + nbr_measures + style = non_bold_style if 'expanded' in header else bold_style + for i in range(header['width']): + worksheet.write(y, x + i, header['title'] if i == 0 else '', style) + if header['height'] > 1: + L.append({'x': x, 'height':header['height'] - 1}) + x = x + header['width']; + while (L and L[0]['x'] == x): + cell = L.pop(0) + for i in range(nbr_measures): + worksheet.write(y,x+i, '', non_bold_style) + if cell['height'] > 1: + L.append({'x': x, 'height':cell['height'] - 1}) + x = x + nbr_measures + x, y = 1, y + 1 + + # Step 2: measure row + if nbr_measures > 1: + worksheet.write(y,0, '', non_bold_style) + for measure in jdata['measure_row']: + style = bold_style if measure['is_bold'] else non_bold_style + worksheet.write(y,x, measure['text'], style); + x = x + 1 + y = y + 1 + + # Step 3: writing data + x = 0 + for row in jdata['rows']: + worksheet.write(y, x, row['indent'] * ' ' + row['title'], non_bold_style) + for cell in row['cells']: + x = x + 1 + if cell.get('is_bold', False): + worksheet.write(y,x, cell['value'], bold) + else: + worksheet.write(y,x, cell['value']) + x, y = 0, y + 1 + + fp = StringIO() + workbook.save(fp) + fp.seek(0) + filecontent = fp.read() + fp.close() 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}) -- cgit v1.2.3 From d0961eb09ecd2db3e155fd66b0c059ddbb30f992 Mon Sep 17 00:00:00 2001 From: Gery Debongnie Date: Wed, 5 Feb 2014 14:14:06 +0100 Subject: [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 --- addons/web_graph/controllers/main.py | 57 ++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'addons/web_graph/controllers/main.py') diff --git a/addons/web_graph/controllers/main.py b/addons/web_graph/controllers/main.py index 589e9c6b76d..320758e0950 100644 --- a/addons/web_graph/controllers/main.py +++ b/addons/web_graph/controllers/main.py @@ -2,6 +2,7 @@ from openerp import http import simplejson from openerp.http import request, serialize_exception as _serialize_exception from cStringIO import StringIO +from collections import deque try: import xlwt @@ -20,68 +21,68 @@ class TableExporter(http.Controller): jdata = simplejson.loads(data) nbr_measures = jdata['nbr_measures'] workbook = xlwt.Workbook() - worksheet = workbook.add_sheet('Sheet 1') - bold_style = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;") - non_bold_style = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;") + worksheet = workbook.add_sheet(jdata['title']) + header_bold = xlwt.easyxf("font: bold on; pattern: pattern solid, fore_colour gray25;") + header_plain = xlwt.easyxf("pattern: pattern solid, fore_colour gray25;") bold = xlwt.easyxf("font: bold on;") # Step 1: writing 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): - worksheet.write(i,0, '', non_bold_style) + worksheet.write(i,0, '', header_plain) for header in header_row: - while (L and L[0]['x'] == x): - cell = L.pop(0) + while (carry and carry[0]['x'] == x): + cell = carry.popleft() 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: - L.append({'x': x, 'height':cell['height'] - 1}) + carry.append({'x': x, 'height':cell['height'] - 1}) 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']): worksheet.write(y, x + i, header['title'] if i == 0 else '', style) if header['height'] > 1: - L.append({'x': x, 'height':header['height'] - 1}) + carry.append({'x': x, 'height':header['height'] - 1}) x = x + header['width']; - while (L and L[0]['x'] == x): - cell = L.pop(0) + while (carry and carry[0]['x'] == x): + cell = carry.popleft() 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: - L.append({'x': x, 'height':cell['height'] - 1}) + carry.append({'x': x, 'height':cell['height'] - 1}) x = x + nbr_measures x, y = 1, y + 1 # Step 2: measure row if nbr_measures > 1: - worksheet.write(y,0, '', non_bold_style) + worksheet.write(y,0, '', header_plain) for measure in jdata['measure_row']: - style = bold_style if measure['is_bold'] else non_bold_style - worksheet.write(y,x, measure['text'], style); + style = header_bold if measure['is_bold'] else header_plain + worksheet.write(y, x, measure['text'], style); x = x + 1 y = y + 1 # Step 3: writing data x = 0 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']: x = x + 1 if cell.get('is_bold', False): - worksheet.write(y,x, cell['value'], bold) + worksheet.write(y, x, cell['value'], bold) else: - worksheet.write(y,x, cell['value']) + worksheet.write(y, x, cell['value']) x, y = 0, y + 1 - fp = StringIO() - workbook.save(fp) - fp.seek(0) - filecontent = fp.read() - fp.close() - - return request.make_response(filecontent, + response = request.make_response(None, headers=[('Content-Type', 'application/vnd.ms-excel'), ('Content-Disposition', 'attachment; filename=table.xls;')], cookies={'fileToken': token}) + workbook.save(response.stream) + return response -- cgit v1.2.3