Skip to content

Commit

Permalink
Merge pull request #62 from Tecnativa/8.0-report_xls-protected_import
Browse files Browse the repository at this point in the history
[8.0][FIX][report_xls] Protect xlwt import.
  • Loading branch information
pedrobaeza committed Jul 25, 2016
2 parents 8cc6325 + b02cc1c commit 2895aef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 73 deletions.
3 changes: 1 addition & 2 deletions report_xls/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
Expand All @@ -22,4 +22,3 @@

from . import ir_report
from . import report_xls
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
52 changes: 1 addition & 51 deletions report_xls/__openerp__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
Expand Down Expand Up @@ -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:
10 changes: 4 additions & 6 deletions report_xls/ir_report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
Expand All @@ -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:
25 changes: 14 additions & 11 deletions report_xls/report_xls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
Expand All @@ -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
Expand All @@ -33,14 +31,20 @@
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):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self


class report_xls(report_sxw):
class ReportXls(report_sxw):

xls_types = {
'bool': xlwt.Row.set_cell_boolean,
Expand Down Expand Up @@ -108,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:
Expand Down Expand Up @@ -162,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

Expand Down Expand Up @@ -201,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])
Expand All @@ -216,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

Expand All @@ -230,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(
Expand All @@ -246,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:
4 changes: 1 addition & 3 deletions report_xls/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
Expand Down Expand Up @@ -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:

0 comments on commit 2895aef

Please sign in to comment.