Browse Source

[MIG] report_qr: Migration to 10.0

pull/324/head
David 5 years ago
parent
commit
ae7f61be44
  1. 7
      report_qr/__manifest__.py
  2. 1
      report_qr/controllers/__init__.py
  3. 13
      report_qr/controllers/qr.py
  4. 3
      report_qr/models/__init__.py
  5. 17
      report_qr/models/ir_actions_report.py
  6. 39
      report_qr/models/report.py
  7. 4
      report_qr/readme/CONTRIBUTORS.rst
  8. 2
      report_qr/tests/test_report_qr.py

7
report_qr/__manifest__.py

@ -1,16 +1,19 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Creu Blanca # Copyright 2019 Creu Blanca
# Copyright 2019 Tecnativa
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{ {
'name': 'Web QR Manager', 'name': 'Web QR Manager',
'version': '11.0.1.0.0',
'version': '10.0.1.0.0',
'author': 'Creu Blanca, ' 'author': 'Creu Blanca, '
'Tecnativa, '
'Odoo Community Association (OCA)', 'Odoo Community Association (OCA)',
'category': 'Sales', 'category': 'Sales',
'website': 'https://github.com/OCA/reporting-engine', 'website': 'https://github.com/OCA/reporting-engine',
'license': 'AGPL-3', 'license': 'AGPL-3',
'depends': [ 'depends': [
'web',
'report',
], ],
'data': [ 'data': [
], ],

1
report_qr/controllers/__init__.py

@ -1 +1,2 @@
# -*- coding: utf-8 -*-
from . import qr from . import qr

13
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 import werkzeug
from odoo import http from odoo import http
from odoo.http import request from odoo.http import request
@ -6,12 +9,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(
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): 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')])

3
report_qr/models/__init__.py

@ -1 +1,2 @@
from . import ir_actions_report
# -*- coding: utf-8 -*-
from . import report

17
report_qr/models/ir_actions_report.py

@ -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.")

39
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.")

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

2
report_qr/tests/test_report_qr.py

@ -18,4 +18,4 @@ class TestReportQr(HttpCase):
for i in range(0, 1500): for i in range(0, 1500):
new_data += 'TEST' new_data += 'TEST'
with self.assertRaises(Exception): with self.assertRaises(Exception):
self.env['ir.actions.report'].qr_generate(new_data)
self.env['report'].qr_generate(new_data)
Loading…
Cancel
Save