diff --git a/report_qweb_parameter/README.rst b/report_qweb_parameter/README.rst new file mode 100644 index 00000000..261adc78 --- /dev/null +++ b/report_qweb_parameter/README.rst @@ -0,0 +1,65 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +===================== +Report QWeb Parameter +===================== + +This module allows you to add new parameters on QWeb reports. +Currently, we have defined a field maximum on a report and a validation of +maximal and minimal size. +It is useful on xml reports in order to validate length. +XML are sometimes XSD dependant and we must validate its format. +For example, in spanish facturae (http://www.facturae.gob.es/Paginas/Index.aspx), where +length and format must be validated in several fields in order to send an invoice. + + +Usage +===== + +#. Add a t-length attribute on report templates fields that will truncate the field +#. Add a t-minlength attribute on report template fields that will check the min length +#. Add a t-maxlength attribute on report template fields that will check the max length + +.. 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/10.0 + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. 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. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Enric Tobella + + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/report_qweb_parameter/__init__.py b/report_qweb_parameter/__init__.py new file mode 100644 index 00000000..ec50cfc0 --- /dev/null +++ b/report_qweb_parameter/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/report_qweb_parameter/__manifest__.py b/report_qweb_parameter/__manifest__.py new file mode 100644 index 00000000..99741eb7 --- /dev/null +++ b/report_qweb_parameter/__manifest__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +# noinspection PyStatementEffect +{ + "name": "Report QWeb Parameter", + "version": "10.0.1.0.1", + "license": "AGPL-3", + "summary": """ + Add new parameters for qweb templates in order to reduce field length + and check minimal length + """, + "author": "Creu Blanca," + "Odoo Community Association (OCA)", + "website": "https://github.com/oca/reporting-engine", + "category": "Technical Settings", + "depends": [ + "report", + ], + "data": [ + ], + "demo": [ + "demo/test_report_field_length.xml" + ], + "installable": True, +} diff --git a/report_qweb_parameter/demo/test_report_field_length.xml b/report_qweb_parameter/demo/test_report_field_length.xml new file mode 100644 index 00000000..2b3d92f3 --- /dev/null +++ b/report_qweb_parameter/demo/test_report_field_length.xml @@ -0,0 +1,25 @@ + + + + + + + diff --git a/report_qweb_parameter/models/__init__.py b/report_qweb_parameter/models/__init__.py new file mode 100644 index 00000000..6b04fd25 --- /dev/null +++ b/report_qweb_parameter/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import ir_qweb diff --git a/report_qweb_parameter/models/ir_qweb.py b/report_qweb_parameter/models/ir_qweb.py new file mode 100644 index 00000000..7042198c --- /dev/null +++ b/report_qweb_parameter/models/ir_qweb.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models, _ +from odoo.exceptions import ValidationError + + +class IrQWeb(models.AbstractModel): + _inherit = 'ir.qweb' + + @staticmethod + def check_length(value, min_length=False, max_length=False): + if min_length and len(value) < min_length: + raise ValidationError( + _('Length cannot be less than %s') % str(min_length)) + if max_length and len(value) > max_length: + raise ValidationError( + _('Length cannot be more than %s') % str(max_length)) + return value + + def _compile_directive_esc(self, el, options): + min_value = el.attrib.pop('t-minlength', False) + max_value = el.attrib.pop('t-maxlength', False) + if min_value or max_value: + el.attrib['t-esc'] = 'docs.env["ir.qweb"].check_length(' + \ + el.attrib['t-esc'] + ', ' + \ + (min_value or 'False') + ', ' + \ + (max_value or 'False') + ')' + if 't-length' in el.attrib: + length = el.attrib.pop('t-length') + el.attrib['t-esc'] = '(' + el.attrib[ + 't-esc'] + ')[:' + length + ']' + return super(IrQWeb, self)._compile_directive_esc(el, options) + + def _compile_directive_raw(self, el, options): + min_value = el.attrib.pop('t-minlength', False) + max_value = el.attrib.pop('t-maxlength', False) + if min_value or max_value: + el.attrib['t-raw'] = 'docs.env["ir.qweb"].check_length(' + \ + el.attrib['t-raw'] + ', ' + \ + (min_value or 'False') + ', ' + \ + (max_value or 'False') + ')' + if 't-length' in el.attrib: + length = el.attrib.pop('t-length') + el.attrib['t-raw'] = el.attrib['t-raw'] + '[:' + length + ']' + return super(IrQWeb, self)._compile_directive_raw(el, options) diff --git a/report_qweb_parameter/static/description/icon.png b/report_qweb_parameter/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/report_qweb_parameter/static/description/icon.png differ diff --git a/report_qweb_parameter/tests/__init__.py b/report_qweb_parameter/tests/__init__.py new file mode 100644 index 00000000..e4695dbf --- /dev/null +++ b/report_qweb_parameter/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_report_qweb_parameter diff --git a/report_qweb_parameter/tests/test_report_qweb_parameter.py b/report_qweb_parameter/tests/test_report_qweb_parameter.py new file mode 100644 index 00000000..70251161 --- /dev/null +++ b/report_qweb_parameter/tests/test_report_qweb_parameter.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import xml.etree.ElementTree as ET + +from odoo.addons.base.ir.ir_qweb import QWebException +from odoo.tests import common + + +class TestReportQWebParameter(common.TransactionCase): + def test_qweb_parameter(self): + report_object = self.env['ir.actions.report.xml'] + report_name = 'report_qweb_parameter.test_report_length' + docs = self.env['res.company'].search([], limit=1) + vat = docs.vat + website = docs.website + fax = docs.fax + company_registry = docs.company_registry + docs.update({ + 'fax': '12345678901', + 'vat': '12345678901', + 'website': '1234567890', + 'company_registry': '1234567890' + }) + rep = report_object.render_report(docs.ids, report_name, False) + root = ET.fromstring( + rep[0] + ) + self.assertEqual(root[1][0][0][0].text, "1234567890") + self.assertEqual(root[1][0][0][2].text, "1234567890") + docs.update({'fax': '123456789'}) + with self.assertRaises(QWebException): + report_object.render_report(docs.ids, report_name, False) + docs.update({'fax': '1234567890', 'vat': '123456789'}) + with self.assertRaises(QWebException): + report_object.render_report(docs.ids, report_name, False) + docs.update({'vat': '1234567890', 'website': '12345678901'}) + with self.assertRaises(QWebException): + report_object.render_report(docs.ids, report_name, False) + docs.update( + {'website': '1234567890', 'company_registry': '12345678901'}) + with self.assertRaises(QWebException): + report_object.render_report(docs.ids, report_name, False) + docs.update({ + 'fax': fax, + 'vat': vat, + 'website': website, + 'company_registry': company_registry + })