Browse Source

[IMP] report_qweb_parameter: black, isort, prettier

myc-14.0-py3o
Carlos Roca 4 years ago
committed by Francisco Ivan Anton Prieto
parent
commit
9e94359619
  1. 1
      report_qweb_parameter/__init__.py
  2. 12
      report_qweb_parameter/__manifest__.py
  3. 37
      report_qweb_parameter/demo/test_report_field_length.xml
  4. 1
      report_qweb_parameter/models/__init__.py
  5. 58
      report_qweb_parameter/models/ir_qweb.py
  6. 1
      report_qweb_parameter/tests/__init__.py
  7. 33
      report_qweb_parameter/tests/test_report_qweb_parameter.py

1
report_qweb_parameter/__init__.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import models

12
report_qweb_parameter/__manifest__.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
@ -10,15 +9,10 @@
Add new parameters for qweb templates in order to reduce field length
and check minimal length
""",
"author": "Creu Blanca,"
"Odoo Community Association (OCA)",
"author": "Creu Blanca," "Odoo Community Association (OCA)",
"website": "https://github.com/oca/reporting-engine",
"category": "Technical Settings",
"depends": [
"web",
],
"demo": [
"demo/test_report_field_length.xml"
],
"depends": ["web"],
"demo": ["demo/test_report_field_length.xml"],
"installable": True,
}

37
report_qweb_parameter/demo/test_report_field_length.xml

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<report
id="test_report_length_report_id"
model="res.company"
@ -8,18 +7,34 @@
report_type="qweb-html"
name="report_qweb_parameter.test_report_length"
/>
<template id="test_report_length">
<data>
<li name="esc_length" t-minlength="10" t-length="10"
t-esc="docs[0].street" t-if="docs[0].street"/>
<li name="esc_maxlength" t-maxlength="10"
t-esc="docs[0].website" t-if="docs[0].website"/>
<li name="raw_length" t-minlength="10" t-length="10"
t-raw="docs[0].vat" t-if="docs[0].vat"/>
<li name="raw_maxlength" t-maxlength="10"
<li
name="esc_length"
t-minlength="10"
t-length="10"
t-esc="docs[0].street"
t-if="docs[0].street"
/>
<li
name="esc_maxlength"
t-maxlength="10"
t-esc="docs[0].website"
t-if="docs[0].website"
/>
<li
name="raw_length"
t-minlength="10"
t-length="10"
t-raw="docs[0].vat"
t-if="docs[0].vat"
/>
<li
name="raw_maxlength"
t-maxlength="10"
t-raw="docs[0].company_registry"
t-if="docs[0].company_registry"/>
t-if="docs[0].company_registry"
/>
</data>
</template>
</odoo>

1
report_qweb_parameter/models/__init__.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import ir_qweb

58
report_qweb_parameter/models/ir_qweb.py

@ -1,47 +1,53 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models, _
from odoo import _, models
from odoo.exceptions import ValidationError
class IrQWeb(models.AbstractModel):
_inherit = 'ir.qweb'
_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))
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))
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)
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 + ']'
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)
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 + ']'
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)

1
report_qweb_parameter/tests/__init__.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import test_report_qweb_parameter

33
report_qweb_parameter/tests/test_report_qweb_parameter.py

@ -1,39 +1,40 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import xml.etree.ElementTree as ET
from odoo.addons.base.models.qweb import QWebException
from odoo.tests import common
from odoo.addons.base.models.qweb import QWebException
class TestReportQWebParameter(common.TransactionCase):
def test_qweb_parameter(self):
report_name = 'report_qweb_parameter.test_report_length'
report_obj = self.env['ir.actions.report']
report_name = "report_qweb_parameter.test_report_length"
report_obj = self.env["ir.actions.report"]
report_object = report_obj._get_report_from_name(report_name)
docs = self.env['res.company'].create({
'name': 'Test company',
'street': '12345678901',
'vat': '12345678901',
'company_registry': '1234567890'
})
docs.website = '1234567890' # for avoding that Odoo adds http://
docs = self.env["res.company"].create(
{
"name": "Test company",
"street": "12345678901",
"vat": "12345678901",
"company_registry": "1234567890",
}
)
docs.website = "1234567890" # for avoding that Odoo adds http://
rep = report_object.render(docs.ids, False)
root = ET.fromstring(rep[0])
self.assertEqual(root[0].text, "1234567890")
self.assertEqual(root[2].text, "1234567890")
docs.update({'street': '123456789'})
docs.update({"street": "123456789"})
with self.assertRaises(QWebException):
report_object.render(docs.ids, False)
docs.update({'street': '1234567890', 'vat': '123456789'})
docs.update({"street": "1234567890", "vat": "123456789"})
with self.assertRaises(QWebException):
report_object.render(docs.ids, False)
docs.update({'vat': '1234567890', 'website': '12345678901'})
docs.update({"vat": "1234567890", "website": "12345678901"})
with self.assertRaises(QWebException):
report_object.render(docs.ids, False)
docs.update(
{'website': '1234567890', 'company_registry': '12345678901'})
docs.update({"website": "1234567890", "company_registry": "12345678901"})
with self.assertRaises(QWebException):
report_object.render(docs.ids, False)
Loading…
Cancel
Save