Browse Source

[IMP] report_qr: Allow to use SVG as output

pull/288/head
David 5 years ago
committed by Enric Tobella
parent
commit
4496a7eaff
  1. 8
      report_qr/controllers/qr.py
  2. 25
      report_qr/models/ir_actions_report.py
  3. 4
      report_qr/readme/CONTRIBUTORS.rst

8
report_qr/controllers/qr.py

@ -6,12 +6,16 @@ from odoo.http import request
class Home(http.Controller): class Home(http.Controller):
@http.route('/report/qr', type='http', auth="public") @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: try:
barcode = request.env['ir.actions.report'].qr_generate( barcode = request.env['ir.actions.report'].qr_generate(
value, box_size=box_size, border=border, **kwargs)
value, box_size=box_size, border=border,
factory=factory, **kwargs)
except (ValueError, AttributeError): except (ValueError, AttributeError):
raise werkzeug.exceptions.HTTPException( raise werkzeug.exceptions.HTTPException(
description='Cannot convert into barcode.') description='Cannot convert into barcode.')
if factory != 'png':
return request.make_response(
barcode, headers=[('Content-Type', 'image/svg+xml')])
return request.make_response( return request.make_response(
barcode, headers=[('Content-Type', 'image/png')]) barcode, headers=[('Content-Type', 'image/png')])

25
report_qr/models/ir_actions_report.py

@ -1,5 +1,6 @@
from odoo import api, models from odoo import api, models
import qrcode import qrcode
from qrcode.image import svg, pil
import io import io
@ -7,11 +8,29 @@ class IrActionsReport(models.Model):
_inherit = 'ir.actions.report' _inherit = 'ir.actions.report'
@api.model @api.model
def qr_generate(self, value, box_size=3, border=5, **kwargs):
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: try:
qr = qrcode.make(value, box_size=box_size, border=border, **kwargs)
# 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() arr = io.BytesIO()
qr.save(arr, format='png')
img.save(arr)
return arr.getvalue() return arr.getvalue()
except Exception: except Exception:
raise ValueError("Cannot convert into barcode.") raise ValueError("Cannot convert into barcode.")

4
report_qr/readme/CONTRIBUTORS.rst

@ -1 +1,5 @@
* Enric Tobella <etobella@creublanca.es> * Enric Tobella <etobella@creublanca.es>
* `Tecnativa <https://www.tecnativa.com>`_:
* David Vidal
Loading…
Cancel
Save