From 877c7bb61b5ba1c99b2b3792e5313643061b621b Mon Sep 17 00:00:00 2001 From: Cristian Salamea Date: Sun, 16 Jun 2013 00:49:46 -0500 Subject: [PATCH 1/5] [IMP] float field as float cell in XLS --- web_export_view/controllers.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/web_export_view/controllers.py b/web_export_view/controllers.py index ba7f4e1d..c2b78757 100644 --- a/web_export_view/controllers.py +++ b/web_export_view/controllers.py @@ -18,6 +18,10 @@ # along with this program. If not, see . # ############################################################################## +import re +import xlwt +from cStringIO import StringIO + try: import json except ImportError: @@ -31,6 +35,33 @@ from web.controllers.main import ExcelExport class ExcelExportView(ExcelExport): _cp_path = '/web/export/xls_view' + def from_data(self, fields, rows): + workbook = xlwt.Workbook() + worksheet = workbook.add_sheet('Sheet 1') + + for i, fieldname in enumerate(fields): + worksheet.write(0, i, fieldname) + worksheet.col(i).width = 8000 # around 220 pixels + + style = xlwt.easyxf('align: wrap yes') + + for row_index, row in enumerate(rows): + for cell_index, cell_value in enumerate(row): + if isinstance(cell_value, basestring): + cell_value = re.sub("\r", " ", cell_value) + if re.match('^[\d,]+(\.\d+)?$', cell_value): + cell_value = float(cell_value.replace(',','')) + style = xlwt.easyxf(num_format_str='#,##0.00') + if cell_value is False: cell_value = None + worksheet.write(row_index + 1, cell_index, cell_value, style) + + fp = StringIO() + workbook.save(fp) + fp.seek(0) + data = fp.read() + fp.close() + return data + @openerpweb.httprequest def index(self, req, data, token): data = json.loads(data) From 8b84594401f13aa1b076ad6fb3f886171c53c4c2 Mon Sep 17 00:00:00 2001 From: Cristian Salamea Date: Sun, 16 Jun 2013 01:30:10 -0500 Subject: [PATCH 2/5] [IMP] use separators from language configuration to float fields --- web_export_view/controllers.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/web_export_view/controllers.py b/web_export_view/controllers.py index c2b78757..56daaff6 100644 --- a/web_export_view/controllers.py +++ b/web_export_view/controllers.py @@ -35,7 +35,7 @@ from web.controllers.main import ExcelExport class ExcelExportView(ExcelExport): _cp_path = '/web/export/xls_view' - def from_data(self, fields, rows): + def from_data(self, fields, rows, separators): workbook = xlwt.Workbook() worksheet = workbook.add_sheet('Sheet 1') @@ -44,12 +44,12 @@ class ExcelExportView(ExcelExport): worksheet.col(i).width = 8000 # around 220 pixels style = xlwt.easyxf('align: wrap yes') - + m = "^[\d%s]+(\%s\d+)?$" % (separators['thousands_sep'], separators['decimal_point']) for row_index, row in enumerate(rows): for cell_index, cell_value in enumerate(row): if isinstance(cell_value, basestring): cell_value = re.sub("\r", " ", cell_value) - if re.match('^[\d,]+(\.\d+)?$', cell_value): + if re.match(m, cell_value): cell_value = float(cell_value.replace(',','')) style = xlwt.easyxf(num_format_str='#,##0.00') if cell_value is False: cell_value = None @@ -70,8 +70,12 @@ class ExcelExportView(ExcelExport): rows = data.get('rows',[]) context = req.session.eval_context(req.context) - - return req.make_response(self.from_data(columns_headers, rows), + lang = context.get('lang') + Model = req.session.model('res.lang') + ids = Model.search([['code','=',lang]]) + record = Model.read(ids, ['decimal_point','thousands_sep']) + print record[0] + return req.make_response(self.from_data(columns_headers, rows, record[0]), headers=[('Content-Disposition', 'attachment; filename="%s"' % self.filename(model)), ('Content-Type', self.content_type)], cookies={'fileToken': int(token)}) From ada82c731f25e62c131b508391e3d6a7b21ba5fd Mon Sep 17 00:00:00 2001 From: Cristian Salamea Date: Sun, 16 Jun 2013 01:33:16 -0500 Subject: [PATCH 3/5] [FIX] remove print statement --- web_export_view/controllers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_export_view/controllers.py b/web_export_view/controllers.py index 56daaff6..7359be6e 100644 --- a/web_export_view/controllers.py +++ b/web_export_view/controllers.py @@ -74,7 +74,7 @@ class ExcelExportView(ExcelExport): Model = req.session.model('res.lang') ids = Model.search([['code','=',lang]]) record = Model.read(ids, ['decimal_point','thousands_sep']) - print record[0] + return req.make_response(self.from_data(columns_headers, rows, record[0]), headers=[('Content-Disposition', 'attachment; filename="%s"' % self.filename(model)), ('Content-Type', self.content_type)], From 1a78c9f7ecaad03c684903f4411d015d94cac4aa Mon Sep 17 00:00:00 2001 From: Cristian Salamea Date: Tue, 18 Jun 2013 10:15:22 -0500 Subject: [PATCH 4/5] [FIX] lang by default en_US and replace decimal_point in cell value CHECK: compile regexp is needed in 47 line? --- web_export_view/controllers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_export_view/controllers.py b/web_export_view/controllers.py index 7359be6e..17234a23 100644 --- a/web_export_view/controllers.py +++ b/web_export_view/controllers.py @@ -50,7 +50,7 @@ class ExcelExportView(ExcelExport): if isinstance(cell_value, basestring): cell_value = re.sub("\r", " ", cell_value) if re.match(m, cell_value): - cell_value = float(cell_value.replace(',','')) + cell_value = float(cell_value.replace(separators['thousands_sep'],'')replace(separators['decimal_point'],'.')) style = xlwt.easyxf(num_format_str='#,##0.00') if cell_value is False: cell_value = None worksheet.write(row_index + 1, cell_index, cell_value, style) @@ -70,7 +70,7 @@ class ExcelExportView(ExcelExport): rows = data.get('rows',[]) context = req.session.eval_context(req.context) - lang = context.get('lang') + lang = context.get('lang', 'en_US') Model = req.session.model('res.lang') ids = Model.search([['code','=',lang]]) record = Model.read(ids, ['decimal_point','thousands_sep']) From f4a3e650793f88677d8dc571219f86aea538793c Mon Sep 17 00:00:00 2001 From: ovnicraft Date: Sat, 17 Aug 2013 15:11:24 -0500 Subject: [PATCH 5/5] [FIX] type error replacing separators and credits for fixing :) --- web_export_view/AUTHORS.txt | 1 + web_export_view/controllers.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/web_export_view/AUTHORS.txt b/web_export_view/AUTHORS.txt index d7fa7280..0ddb1641 100644 --- a/web_export_view/AUTHORS.txt +++ b/web_export_view/AUTHORS.txt @@ -4,3 +4,4 @@ Authors Simone Orsi [simahawk] Lorenzo Battistini Stefan Rijnhart +Cristian Salamea diff --git a/web_export_view/controllers.py b/web_export_view/controllers.py index 17234a23..4e4c6bd7 100644 --- a/web_export_view/controllers.py +++ b/web_export_view/controllers.py @@ -50,7 +50,7 @@ class ExcelExportView(ExcelExport): if isinstance(cell_value, basestring): cell_value = re.sub("\r", " ", cell_value) if re.match(m, cell_value): - cell_value = float(cell_value.replace(separators['thousands_sep'],'')replace(separators['decimal_point'],'.')) + cell_value = float(cell_value.replace(separators['thousands_sep'],'').replace(separators['decimal_point'],'.')) style = xlwt.easyxf(num_format_str='#,##0.00') if cell_value is False: cell_value = None worksheet.write(row_index + 1, cell_index, cell_value, style)