From 2d5d780db0e0347a55ad4400aa5f360f9d614ebc Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 24 Aug 2015 16:12:12 +0200 Subject: [PATCH] [ADD] support qweb reports --- report_custom_filename/README.rst | 11 ++- .../controllers/__init__.py | 1 + .../controllers/report_controller.py | 68 +++++++++++++++++++ .../model/ir_actions_report_xml.py | 4 +- 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 report_custom_filename/controllers/report_controller.py diff --git a/report_custom_filename/README.rst b/report_custom_filename/README.rst index ff4f92db..ca2d8b61 100644 --- a/report_custom_filename/README.rst +++ b/report_custom_filename/README.rst @@ -6,13 +6,7 @@ This addon allows for custom filenames for reports. Configuration ============= -To configure this module, open the report whose filename you want to change and fill in the `Download filename` field. This field is evaluated as jinja2 template with `objects` being a list of browse records of the records to print, and `o` the first record. - -Known issues / Roadmap -====================== - - * Currently, only old-style reports (ir.actions.report.xml) are supported, it should be simple to add support for qweb reports. - +To configure this module, open the report whose filename you want to change and fill in the `Download filename` field. This field is evaluated as jinja2 template with `objects` being a list of browse records of the records to print, and `o` the first record. If your model contains a name field, you might write something like `${o.name}_report.pdf` as filename. Bug Tracker =========== @@ -22,6 +16,9 @@ In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback `here `_. +.. 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/8.0 Credits ======= diff --git a/report_custom_filename/controllers/__init__.py b/report_custom_filename/controllers/__init__.py index 329fca16..6eb84a90 100644 --- a/report_custom_filename/controllers/__init__.py +++ b/report_custom_filename/controllers/__init__.py @@ -19,3 +19,4 @@ # ############################################################################## from . import reports +from . import report_controller diff --git a/report_custom_filename/controllers/report_controller.py b/report_custom_filename/controllers/report_controller.py new file mode 100644 index 00000000..536d2d69 --- /dev/null +++ b/report_custom_filename/controllers/report_controller.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import http +from openerp.addons.email_template import email_template +from openerp.addons.report.controllers.main import ReportController +from openerp.addons.web.controllers.main import content_disposition + + +class ReportController(ReportController): + @http.route([ + '/report//', + '/report///', + ]) + def report_routes(self, reportname, docids=None, converter=None, **data): + response = super(ReportController, self).report_routes( + reportname, docids=docids, converter=converter, **data) + if docids: + docids = [int(i) for i in docids.split(',')] + report_xml = http.request.session.model('ir.actions.report.xml') + report_ids = report_xml.search( + [('report_name', '=', reportname)]) + for report in report_xml.browse(report_ids): + if not report.download_filename: + continue + objects = http.request.session.model(report.model)\ + .browse(docids or []) + generated_filename = email_template.mako_template_env\ + .from_string(report.download_filename)\ + .render({ + 'objects': objects, + 'o': objects[:1], + 'object': objects[:1], + 'ext': report.report_type.replace('qweb-', ''), + }) + response.headers['Content-Disposition'] = content_disposition( + generated_filename) + return response + + @http.route(['/report/download']) + def report_download(self, data, token): + response = super(ReportController, self).report_download(data, token) + # if we got another content disposition before, ditch the one added + # by super() + last_index = None + for i in range(len(response.headers) - 1, -1, -1): + if response.headers[i][0] == 'Content-Disposition': + if last_index: + response.headers.pop(last_index) + last_index = i + return response diff --git a/report_custom_filename/model/ir_actions_report_xml.py b/report_custom_filename/model/ir_actions_report_xml.py index 27cd638c..5ea04300 100644 --- a/report_custom_filename/model/ir_actions_report_xml.py +++ b/report_custom_filename/model/ir_actions_report_xml.py @@ -32,4 +32,6 @@ class IrActionsReportXml(models.Model): 'the objects for which the report is being generated.\n' 'Check for this list\'s length to determine if it is a report being ' 'printed for multiple records or not. You also have access to `o`, ' - 'which is the first record in the list') + 'which is the first record in the list.\n' + 'For qweb reports, the variable `ext` gives you the requested format' + '\'s extension')