From ae7f61be44b8b9b93b32c0178e753fc747b27dea Mon Sep 17 00:00:00 2001 From: David Date: Thu, 19 Sep 2019 14:26:53 +0200 Subject: [PATCH] [MIG] report_qr: Migration to 10.0 --- report_qr/__manifest__.py | 7 +++-- report_qr/controllers/__init__.py | 1 + report_qr/controllers/qr.py | 13 ++++++--- report_qr/models/__init__.py | 3 ++- report_qr/models/ir_actions_report.py | 17 ------------ report_qr/models/report.py | 39 +++++++++++++++++++++++++++ report_qr/readme/CONTRIBUTORS.rst | 4 +++ report_qr/tests/test_report_qr.py | 2 +- 8 files changed, 62 insertions(+), 24 deletions(-) delete mode 100644 report_qr/models/ir_actions_report.py create mode 100644 report_qr/models/report.py diff --git a/report_qr/__manifest__.py b/report_qr/__manifest__.py index 4b5803c5..1c44aa6c 100644 --- a/report_qr/__manifest__.py +++ b/report_qr/__manifest__.py @@ -1,16 +1,19 @@ +# -*- coding: utf-8 -*- # Copyright 2019 Creu Blanca +# Copyright 2019 Tecnativa # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { 'name': 'Web QR Manager', - 'version': '11.0.1.0.0', + 'version': '10.0.1.0.0', 'author': 'Creu Blanca, ' + 'Tecnativa, ' 'Odoo Community Association (OCA)', 'category': 'Sales', 'website': 'https://github.com/OCA/reporting-engine', 'license': 'AGPL-3', 'depends': [ - 'web', + 'report', ], 'data': [ ], diff --git a/report_qr/controllers/__init__.py b/report_qr/controllers/__init__.py index da6b5392..c52ced06 100644 --- a/report_qr/controllers/__init__.py +++ b/report_qr/controllers/__init__.py @@ -1 +1,2 @@ +# -*- coding: utf-8 -*- from . import qr diff --git a/report_qr/controllers/qr.py b/report_qr/controllers/qr.py index b0a12721..e032e15b 100644 --- a/report_qr/controllers/qr.py +++ b/report_qr/controllers/qr.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import werkzeug from odoo import http from odoo.http import request @@ -6,12 +9,16 @@ from odoo.http import request class Home(http.Controller): @http.route('/report/qr', type='http', auth="public") - def report_qr(self, value, box_size=3, border=3, **kwargs): + def report_qr(self, value, box_size=3, border=3, factory='png', **kwargs): try: - barcode = request.env['ir.actions.report'].qr_generate( - value, box_size=box_size, border=border, **kwargs) + barcode = request.env['report'].qr_generate( + value, box_size=box_size, border=border, + factory=factory, **kwargs) except (ValueError, AttributeError): raise werkzeug.exceptions.HTTPException( description='Cannot convert into barcode.') + if factory != 'png': + return request.make_response( + barcode, headers=[('Content-Type', 'image/svg+xml')]) return request.make_response( barcode, headers=[('Content-Type', 'image/png')]) diff --git a/report_qr/models/__init__.py b/report_qr/models/__init__.py index a248cf21..8323e741 100644 --- a/report_qr/models/__init__.py +++ b/report_qr/models/__init__.py @@ -1 +1,2 @@ -from . import ir_actions_report +# -*- coding: utf-8 -*- +from . import report diff --git a/report_qr/models/ir_actions_report.py b/report_qr/models/ir_actions_report.py deleted file mode 100644 index e2dd1609..00000000 --- a/report_qr/models/ir_actions_report.py +++ /dev/null @@ -1,17 +0,0 @@ -from odoo import api, models -import qrcode -import io - - -class IrActionsReport(models.Model): - _inherit = 'ir.actions.report' - - @api.model - def qr_generate(self, value, box_size=3, border=5, **kwargs): - try: - qr = qrcode.make(value, box_size=box_size, border=border, **kwargs) - arr = io.BytesIO() - qr.save(arr, format='png') - return arr.getvalue() - except Exception: - raise ValueError("Cannot convert into barcode.") diff --git a/report_qr/models/report.py b/report_qr/models/report.py new file mode 100644 index 00000000..43d60900 --- /dev/null +++ b/report_qr/models/report.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Creu Blanca +# Copyright 2019 Tecnativa - David Vidal +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo import api, models +import qrcode +from qrcode.image import svg, pil +import io + + +class Report(models.Model): + _inherit = 'report' + + @api.model + def qr_generate(self, value, box_size=3, border=5, + factory='png', **kwargs): + factories = { + 'png': pil.PilImage, + 'svg': svg.SvgImage, + 'svg-fragment': svg.SvgFragmentImage, + 'svg-path': svg.SvgPathImage, + } + # Color parameters seem to be inverted in the library + back_color = kwargs.pop("back_color", "black") + fill_color = kwargs.pop("fill_color", "white") + try: + # Defaults to png if the argument is unknown + image_factory = factories.get(factory, pil.PilImage) + qr = qrcode.QRCode( + box_size=box_size, border=border, + image_factory=image_factory, **kwargs) + qr.add_data(value) + qr.make() + img = qr.make_image(fill_color=fill_color, back_color=back_color) + arr = io.BytesIO() + img.save(arr) + return arr.getvalue() + except Exception: + raise ValueError("Cannot convert into barcode.") diff --git a/report_qr/readme/CONTRIBUTORS.rst b/report_qr/readme/CONTRIBUTORS.rst index 93ec993e..ba55fe73 100644 --- a/report_qr/readme/CONTRIBUTORS.rst +++ b/report_qr/readme/CONTRIBUTORS.rst @@ -1 +1,5 @@ * Enric Tobella + +* `Tecnativa `_: + + * David Vidal diff --git a/report_qr/tests/test_report_qr.py b/report_qr/tests/test_report_qr.py index e6f5bf7a..a231c547 100644 --- a/report_qr/tests/test_report_qr.py +++ b/report_qr/tests/test_report_qr.py @@ -18,4 +18,4 @@ class TestReportQr(HttpCase): for i in range(0, 1500): new_data += 'TEST' with self.assertRaises(Exception): - self.env['ir.actions.report'].qr_generate(new_data) + self.env['report'].qr_generate(new_data)