From db8b3f1dbf780287fe807ab8d05f2858f3fa03f1 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Fri, 8 Jan 2021 11:09:21 +0000 Subject: [PATCH] [IMP] base: improve qweb barcode widget When printing barcodes for a large amount of records, wkhtmltopdf will make the same amount of http requests to the server in order to retrieve barcodes. This can lead to performance issues or wkhtml to crash. Fortunately, an already existsing qweb widget exists that include barcodes images as inline base64. With this commit, the web widget is a bit improved to accept attributes for the generated image tag. Each option dictionary key starting with `img_` will be converted into a tag attribute with the corresponding value. e.g.: `'img_alt': 'Barcode'` will result in `Barcode' % base64.b64encode(barcode).decode('ascii') + **{key: value for key, value in options.items() if key in ['width', 'height', 'humanreadable', 'quiet', 'mask']}) + img_attributes = {k[4:]: v for k, v in options.items() if k.startswith('img_')} + if 'alt' not in img_attributes: + img_attributes.update({'alt': 'Barcode %s' % value}) + attributes = ' '.join(['%s="%s"' % attrs for attrs in img_attributes.items()]) + return u'' % (base64.b64encode(barcode).decode('ascii'), attributes) class Contact(models.AbstractModel): diff --git a/odoo/addons/base/tests/test_qweb.py b/odoo/addons/base/tests/test_qweb.py index 5c80d204a19..72de3e9c3e9 100644 --- a/odoo/addons/base/tests/test_qweb.py +++ b/odoo/addons/base/tests/test_qweb.py @@ -557,6 +557,30 @@ class TestQWebNS(TransactionCase): rendered = view2.with_context(lang=current_lang)._render().strip() self.assertEqual(rendered, b'9/000/000*00') + def test_render_barcode(self): + partner = self.env['res.partner'].create({ + 'name': 'bacode_test', + 'barcode': 'test' + }) + + view = self.env['ir.ui.view'].create({ + 'name': "a_barcode_view", + 'type': 'qweb', + }) + + view.arch = u"""
""" + rendered = view._render(values={'partner': partner}).strip().decode() + self.assertRegex(rendered,r'
Barcode test
') + + partner.barcode = '4012345678901' + view.arch = u"""
""" + ean_rendered = view._render(values={'partner': partner}).strip().decode() + self.assertRegex(ean_rendered,r'
Barcode
') + + view.arch = u"""
""" + auto_rendered = view._render(values={'partner': partner}).strip().decode() + self.assertRegex(auto_rendered,r'
Barcode
') + from copy import deepcopy class FileSystemLoader(object):