From 2bf93a1d49f7c79062ab7c237ceb794cdbdec983 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Mon, 25 Jul 2016 13:34:46 +0200 Subject: [PATCH 1/2] [FIX][report_xls] Protect xlwt import. Given `xlwt` could happen to not be installed because it is an optional dependency. See https://github.com/odoo/odoo/issues/12897#issuecomment-234928120. --- report_xls/report_xls.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/report_xls/report_xls.py b/report_xls/report_xls.py index f36f38a8d7..347cb5e9ca 100644 --- a/report_xls/report_xls.py +++ b/report_xls/report_xls.py @@ -20,8 +20,6 @@ # ############################################################################## -import xlwt -from xlwt.Style import default_style import cStringIO from datetime import datetime from openerp.osv.fields import datetime as datetime_field @@ -33,6 +31,12 @@ import logging _logger = logging.getLogger(__name__) +try: + import xlwt + from xlwt.Style import default_style +except ImportError: + _logger.debug("Cannot import xlwt. This module will not be functional.") + class AttrDict(dict): def __init__(self, *args, **kwargs): From b02cc1ce72e40b49d9da0a391f62473f7b1d3567 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Mon, 25 Jul 2016 14:38:39 +0200 Subject: [PATCH 2/2] Fix linter warnings. --- report_xls/__init__.py | 3 +-- report_xls/__openerp__.py | 52 +-------------------------------------- report_xls/ir_report.py | 10 +++----- report_xls/report_xls.py | 17 ++++++------- report_xls/utils.py | 4 +-- 5 files changed, 15 insertions(+), 71 deletions(-) diff --git a/report_xls/__init__.py b/report_xls/__init__.py index e100e78612..6325918e19 100644 --- a/report_xls/__init__.py +++ b/report_xls/__init__.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -22,4 +22,3 @@ from . import ir_report from . import report_xls -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/report_xls/__openerp__.py b/report_xls/__openerp__.py index 31edb10beb..65769da540 100644 --- a/report_xls/__openerp__.py +++ b/report_xls/__openerp__.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -26,57 +26,7 @@ 'author': "Noviat,Odoo Community Association (OCA)", 'website': 'http://www.noviat.com', 'category': 'Reporting', - 'description': """ -Excel report engine -=================== - -This module adds Excel export capabilities to the standard odoo reporting -engine. - -Report development -'''''''''''''''''' -In order to create an Excel report you can\n -- define a report of type 'xls' -- pass ``{'xls_export': 1}`` via the context to the report create method - -The ``report_xls`` class contains a number of attributes and methods to -facilitate the creation XLS reports in OpenERP. - -* cell types - - Supported cell types : text, number, boolean, date. - -* cell styles - - The predefined cell style definitions result in a consistent - look and feel of the OpenERP Excel reports. - -* cell formulas - - Cell formulas can be easily added with the help of the ``rowcol_to_cell()`` - function which you can import from the ``utils.py`` module. - -* Excel templates - - It is possible to define Excel templates which can be adapted - by 'inherited' modules. - Download the ``account_move_line_report_xls`` module - from http://apps.odoo.com as example. - -* XLS with multiple sheets - - Download the ``account_journal_report_xls`` module - from http://apps.odoo.com as example. - -Development assistance -'''''''''''''''''''''' -Contact info@noviat.com for help with the development of -Excel reports in odoo. - - """, 'depends': ['base'], 'external_dependencies': {'python': ['xlwt']}, - 'active': False, 'installable': True, } -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/report_xls/ir_report.py b/report_xls/ir_report.py index 332f32f088..ba6fb12e72 100644 --- a/report_xls/ir_report.py +++ b/report_xls/ir_report.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -20,17 +20,15 @@ # ############################################################################## -from openerp.osv import orm +from openerp import models -class ir_actions_report_xml(orm.Model): +class IrActionsReportXml(models.Model): _inherit = 'ir.actions.report.xml' def _check_selection_field_value(self, cr, uid, field, value, context=None): if field == 'report_type' and value == 'xls': return - return super(ir_actions_report_xml, self)._check_selection_field_value( + return super(IrActionsReportXml, self)._check_selection_field_value( cr, uid, field, value, context=context) - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/report_xls/report_xls.py b/report_xls/report_xls.py index 347cb5e9ca..92a0940b58 100644 --- a/report_xls/report_xls.py +++ b/report_xls/report_xls.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -44,7 +44,7 @@ def __init__(self, *args, **kwargs): self.__dict__ = self -class report_xls(report_sxw): +class ReportXls(report_sxw): xls_types = { 'bool': xlwt.Row.set_cell_boolean, @@ -112,7 +112,7 @@ def create(self, cr, uid, ids, data, context=None): # use model from 'data' when no ir.actions.report.xml entry self.table = data.get('model') or self.table return self.create_source_xls(cr, uid, ids, data, context) - return super(report_xls, self).create(cr, uid, ids, data, context) + return super(ReportXls, self).create(cr, uid, ids, data, context) def create_source_xls(self, cr, uid, ids, data, context=None): if not context: @@ -166,7 +166,8 @@ def render(self, wanted, col_specs, rowtype, render_space='empty'): row = col_specs[wanted][rowtype][:] for i in range(len(row)): if isinstance(row[i], CodeType): - row[i] = eval(row[i], render_space) + # TODO Use safe_eval or document why not and remove pylint hack + row[i] = eval(row[i], render_space) # pylint: disable=W0123 row.insert(0, wanted) return row @@ -205,7 +206,7 @@ def xls_row_template(self, specs, wanted_list): c.append({'formula': s[5]}) else: c.append({ - 'write_cell_func': report_xls.xls_types[c[3]]}) + 'write_cell_func': ReportXls.xls_types[c[3]]}) # Set custom cell style if s_len > 6 and s[6] is not None: c.append(s[6]) @@ -220,7 +221,7 @@ def xls_row_template(self, specs, wanted_list): col += c[1] break if not found: - _logger.warn("report_xls.xls_row_template, " + _logger.warn("ReportXls.xls_row_template, " "column '%s' not found in specs", w) return r @@ -234,7 +235,7 @@ def xls_write_row(self, ws, row_pos, row_data, style = spec[6] and spec[6] or row_style if not data: # if no data, use default values - data = report_xls.xls_types_default[spec[3]] + data = ReportXls.xls_types_default[spec[3]] if size != 1: if formula: ws.write_merge( @@ -250,5 +251,3 @@ def xls_write_row(self, ws, row_pos, row_data, if set_column_size: ws.col(col).width = spec[2] * 256 return row_pos + 1 - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/report_xls/utils.py b/report_xls/utils.py index 943daae7c5..bb57184683 100644 --- a/report_xls/utils.py +++ b/report_xls/utils.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -47,5 +47,3 @@ def rowcol_to_cell(row, col, row_abs=False, col_abs=False): chr2 = chr(ord('A') + m) # Zero index to 1-index return col_abs + chr1 + chr2 + row_abs + str(row + 1) - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: