diff --git a/report_xlsx/README.rst b/report_xlsx/README.rst index bd2a96e5..a0548a83 100644 --- a/report_xlsx/README.rst +++ b/report_xlsx/README.rst @@ -1,5 +1,5 @@ .. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg - :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :target: https://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 ================ @@ -15,16 +15,22 @@ Make sure you have ``xlsxwriter`` Python module installed:: $ pip install xlsxwriter +For testing it is also necessary ``xlrd`` Python module installed:: + +$ pip install xlrd + Usage ===== -An example of XLSX report for partners: +An example of XLSX report for partners on a module called `module_name`: A python class :: - from odoo.addons.report_xlsx.report.report_xlsx import ReportXlsx + from odoo import models - class PartnerXlsx(ReportXlsx): + class PartnerXlsx(models.AbstractModel): + _name = 'report.module_name.report_name' + _inherit = 'report.report_xlsx.abstract' def generate_xlsx_report(self, workbook, data, partners): for obj in partners: @@ -34,10 +40,6 @@ A python class :: bold = workbook.add_format({'bold': True}) sheet.write(0, 0, obj.name, bold) - - PartnerXlsx('report.res.partner.xlsx', - 'res.partner') - To manipulate the ``workbook`` and ``sheet`` objects, refer to the `documentation `_ of ``xlsxwriter``. @@ -48,14 +50,14 @@ A report XML record :: model="res.partner" string="Print to XLSX" report_type="xlsx" - name="res.partner.xlsx" - file="res.partner.xlsx" + name="module_name.report_name" + file="res_partner" attachment_use="False" /> .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/143/10.0 + :target: https://runbot.odoo-community.org/runbot/143/11.0 Bug Tracker =========== @@ -72,6 +74,7 @@ Contributors ------------ * Adrien Peiffer +* Enric Tobella Maintainer ---------- diff --git a/report_xlsx/__init__.py b/report_xlsx/__init__.py index 9a384baf..2a4823f8 100644 --- a/report_xlsx/__init__.py +++ b/report_xlsx/__init__.py @@ -1,5 +1,5 @@ -# -*- coding: utf-8 -*- -# Copyright 2015 ACSONE SA/NV () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import controllers from . import models from . import report diff --git a/report_xlsx/__manifest__.py b/report_xlsx/__manifest__.py index 2d8368b3..8283dda4 100644 --- a/report_xlsx/__manifest__.py +++ b/report_xlsx/__manifest__.py @@ -1,20 +1,30 @@ -# -*- coding: utf-8 -*- # Copyright 2015 ACSONE SA/NV () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). { 'name': "Base report xlsx", - 'summary': """ - Base module to create xlsx report""", + 'summary': "Base module to create xlsx report", 'author': 'ACSONE SA/NV,' + 'Creu Blanca,' 'Odoo Community Association (OCA)', - 'website': "http://acsone.eu", + 'website': "http://github.com/oca/reporting-engine", 'category': 'Reporting', - 'version': '10.0.1.0.1', + 'version': '11.0.1.0.0', 'license': 'AGPL-3', - 'external_dependencies': {'python': ['xlsxwriter']}, + 'external_dependencies': { + 'python': [ + 'xlsxwriter', + 'xlrd', + ], + }, 'depends': [ - 'base', + 'base', 'web', + ], + 'data': [ + 'views/webclient_templates.xml', + ], + 'demo': [ + 'demo/report.xml', ], 'installable': True, } diff --git a/report_xlsx/controllers/__init__.py b/report_xlsx/controllers/__init__.py new file mode 100644 index 00000000..12a7e529 --- /dev/null +++ b/report_xlsx/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/report_xlsx/controllers/main.py b/report_xlsx/controllers/main.py new file mode 100644 index 00000000..3c8c6a4c --- /dev/null +++ b/report_xlsx/controllers/main.py @@ -0,0 +1,44 @@ +# Copyright (C) 2017 Creu Blanca +# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html). + +from odoo.addons.web.controllers import main as report +from odoo.http import route, request + +import json + + +class ReportController(report.ReportController): + @route() + def report_routes(self, reportname, docids=None, converter=None, **data): + if converter == 'xlsx': + report = request.env['ir.actions.report']._get_report_from_name( + reportname) + context = dict(request.env.context) + if docids: + docids = [int(i) for i in docids.split(',')] + if data.get('options'): + data.update(json.loads(data.pop('options'))) + if data.get('context'): + # Ignore 'lang' here, because the context in data is the one + # from the webclient *but* if the user explicitely wants to + # change the lang, this mechanism overwrites it. + data['context'] = json.loads(data['context']) + if data['context'].get('lang'): + del data['context']['lang'] + context.update(data['context']) + xlsx = report.with_context(context).render_xlsx( + docids, data=data + )[0] + xlsxhttpheaders = [ + ('Content-Type', 'application/vnd.openxmlformats-' + 'officedocument.spreadsheetml.sheet'), + ('Content-Length', len(xlsx)), + ( + 'Content-Disposition', + 'attachment; filename=' + report.report_file + '.xlsx' + ) + ] + return request.make_response(xlsx, headers=xlsxhttpheaders) + return super(ReportController, self).report_routes( + reportname, docids, converter, **data + ) diff --git a/report_xlsx/demo/report.xml b/report_xlsx/demo/report.xml new file mode 100644 index 00000000..9f82fbf8 --- /dev/null +++ b/report_xlsx/demo/report.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/report_xlsx/models/__init__.py b/report_xlsx/models/__init__.py index 521aa82b..54dbf3df 100644 --- a/report_xlsx/models/__init__.py +++ b/report_xlsx/models/__init__.py @@ -1,5 +1 @@ -# -*- coding: utf-8 -*- -# Copyright 2015 ACSONE SA/NV () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).+ - from . import ir_report diff --git a/report_xlsx/models/ir_report.py b/report_xlsx/models/ir_report.py index 7bf10a9a..e295d1e7 100644 --- a/report_xlsx/models/ir_report.py +++ b/report_xlsx/models/ir_report.py @@ -1,11 +1,33 @@ -# -*- coding: utf-8 -*- # Copyright 2015 ACSONE SA/NV () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import fields, models +from odoo import api, fields, models, _ +from odoo.exceptions import UserError -class IrActionsReportXml(models.Model): - _inherit = 'ir.actions.report.xml' +class ReportAction(models.Model): + _inherit = 'ir.actions.report' report_type = fields.Selection(selection_add=[("xlsx", "xlsx")]) + + @api.model + def render_xlsx(self, docids, data): + report_model_name = 'report.%s' % self.report_name + report_model = self.env.get(report_model_name) + if report_model is None: + raise UserError(_('%s model was not found' % report_model_name)) + return report_model.with_context({ + 'active_model': self.model + }).create_xlsx_report(docids, data) + + @api.model + def _get_report_from_name(self, report_name): + res = super(ReportAction, self)._get_report_from_name(report_name) + if res: + return res + report_obj = self.env['ir.actions.report'] + qwebtypes = ['xlsx'] + conditions = [('report_type', 'in', qwebtypes), + ('report_name', '=', report_name)] + context = self.env['res.users'].context_get() + return report_obj.with_context(context).search(conditions, limit=1) diff --git a/report_xlsx/report/__init__.py b/report_xlsx/report/__init__.py index 37c50512..0d77311a 100644 --- a/report_xlsx/report/__init__.py +++ b/report_xlsx/report/__init__.py @@ -1,4 +1,2 @@ -# -*- coding: utf-8 -*- -# Copyright 2015 ACSONE SA/NV () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import report_xlsx +from . import report_partner_xlsx diff --git a/report_xlsx/report/report_partner_xlsx.py b/report_xlsx/report/report_partner_xlsx.py new file mode 100644 index 00000000..e4fe715a --- /dev/null +++ b/report_xlsx/report/report_partner_xlsx.py @@ -0,0 +1,15 @@ +# Copyright 2017 Creu Blanca +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class PartnerXlsx(models.AbstractModel): + _name = 'report.report_xlsx.partner_xlsx' + _inherit = 'report.report_xlsx.abstract' + + def generate_xlsx_report(self, workbook, data, partners): + for obj in partners: + sheet = workbook.add_worksheet('Report') + bold = workbook.add_format({'bold': True}) + sheet.write(0, 0, obj.name, bold) diff --git a/report_xlsx/report/report_xlsx.py b/report_xlsx/report/report_xlsx.py index a199beb5..45bf9cee 100644 --- a/report_xlsx/report/report_xlsx.py +++ b/report_xlsx/report/report_xlsx.py @@ -1,11 +1,9 @@ -# -*- coding: utf-8 -*- # Copyright 2015 ACSONE SA/NV () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from cStringIO import StringIO +from io import BytesIO -from odoo.report.report_sxw import report_sxw -from odoo.api import Environment +from odoo import models import logging _logger = logging.getLogger(__name__) @@ -16,30 +14,17 @@ except ImportError: _logger.debug('Can not import xlsxwriter`.') -class ReportXlsx(report_sxw): - - def create(self, cr, uid, ids, data, context=None): - self.env = Environment(cr, uid, context) - report_obj = self.env['ir.actions.report.xml'] - report = report_obj.search([('report_name', '=', self.name[7:])]) - if report.ids: - self.title = report.name - if report.report_type == 'xlsx': - return self.create_xlsx_report(ids, data, report) - return super(ReportXlsx, self).create(cr, uid, ids, data, context) - - def create_xlsx_report(self, ids, data, report): - self.parser_instance = self.parser( - self.env.cr, self.env.uid, self.name2, self.env.context) - objs = self.getObjects( - self.env.cr, self.env.uid, ids, self.env.context) - self.parser_instance.set_context(objs, data, ids, 'xlsx') - file_data = StringIO() +class ReportXlsxAbstract(models.AbstractModel): + _name = 'report.report_xlsx.abstract' + + def create_xlsx_report(self, docids, data): + objs = self.env[self.env.context.get('active_model')].browse(docids) + file_data = BytesIO() workbook = xlsxwriter.Workbook(file_data, self.get_workbook_options()) self.generate_xlsx_report(workbook, data, objs) workbook.close() file_data.seek(0) - return (file_data.read(), 'xlsx') + return file_data.read(), 'xlsx' def get_workbook_options(self): return {} diff --git a/report_xlsx/static/src/js/report/qwebactionmanager.js b/report_xlsx/static/src/js/report/qwebactionmanager.js new file mode 100644 index 00000000..3d560d86 --- /dev/null +++ b/report_xlsx/static/src/js/report/qwebactionmanager.js @@ -0,0 +1,42 @@ +// © 2017 Creu Blanca +// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html). +odoo.define('report_xlsx.report', function(require){ +'use strict'; + +var ActionManager= require('web.ActionManager'); +var crash_manager = require('web.crash_manager'); +var framework = require('web.framework'); + +ActionManager.include({ + ir_actions_report: function (action, options){ + var self = this; + var cloned_action = _.clone(action); + if (cloned_action.report_type === 'xlsx') { + framework.blockUI(); + var report_xlsx_url = 'report/xlsx/' + cloned_action.report_name; + if(cloned_action.context.active_ids){ + report_xlsx_url += '/' + cloned_action.context.active_ids.join(','); + }else{ + report_xlsx_url += '?options=' + encodeURIComponent(JSON.stringify(cloned_action.data)); + report_xlsx_url += '&context=' + encodeURIComponent(JSON.stringify(cloned_action.context)); + } + self.getSession().get_file({ + url: report_xlsx_url, + data: {data: JSON.stringify([ + report_xlsx_url, + cloned_action.report_type + ])}, + error: crash_manager.rpc_error.bind(crash_manager), + success: function (){ + if(cloned_action && options && !cloned_action.dialog){ + options.on_close(); + } + } + }); + framework.unblockUI(); + return; + } + return self._super(action, options); + } +}); +}); diff --git a/report_xlsx/tests/__init__.py b/report_xlsx/tests/__init__.py new file mode 100644 index 00000000..32ae3c2c --- /dev/null +++ b/report_xlsx/tests/__init__.py @@ -0,0 +1 @@ +from . import test_report diff --git a/report_xlsx/tests/test_report.py b/report_xlsx/tests/test_report.py new file mode 100644 index 00000000..1222f3ad --- /dev/null +++ b/report_xlsx/tests/test_report.py @@ -0,0 +1,24 @@ +# Copyright 2017 Creu Blanca +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo.tests import common +import logging +_logger = logging.getLogger(__name__) + +try: + from xlrd import open_workbook +except ImportError: + _logger.debug('Can not import xlsxwriter`.') + + +class TestReport(common.TransactionCase): + def test_report(self): + report_object = self.env['ir.actions.report'] + report_name = 'report_xlsx.partner_xlsx' + report = report_object._get_report_from_name(report_name) + docs = self.env['res.company'].search([], limit=1).partner_id + self.assertEqual(report.report_type, 'xlsx') + rep = report.render(docs.ids, {}) + wb = open_workbook(file_contents=rep[0]) + sheet = wb.sheet_by_index(0) + self.assertEqual(sheet.cell(0, 0).value, docs.name) diff --git a/report_xlsx/views/webclient_templates.xml b/report_xlsx/views/webclient_templates.xml new file mode 100644 index 00000000..31529e56 --- /dev/null +++ b/report_xlsx/views/webclient_templates.xml @@ -0,0 +1,13 @@ + + + +