[IMP] report_qweb_parameter: black, isort, prettier
parent
6273966524
commit
76ee98ee18
|
@ -1,4 +1,3 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright 2017 Creu Blanca
|
# Copyright 2017 Creu Blanca
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# 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
|
Add new parameters for qweb templates in order to reduce field length
|
||||||
and check minimal length
|
and check minimal length
|
||||||
""",
|
""",
|
||||||
"author": "Creu Blanca,"
|
"author": "Creu Blanca," "Odoo Community Association (OCA)",
|
||||||
"Odoo Community Association (OCA)",
|
|
||||||
"website": "https://github.com/oca/reporting-engine",
|
"website": "https://github.com/oca/reporting-engine",
|
||||||
"category": "Technical Settings",
|
"category": "Technical Settings",
|
||||||
"depends": [
|
"depends": ["web"],
|
||||||
"web",
|
"demo": ["demo/test_report_field_length.xml"],
|
||||||
],
|
|
||||||
"demo": [
|
|
||||||
"demo/test_report_field_length.xml"
|
|
||||||
],
|
|
||||||
"installable": True,
|
"installable": True,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<odoo>
|
<odoo>
|
||||||
|
|
||||||
<report
|
<report
|
||||||
id="test_report_length_report_id"
|
id="test_report_length_report_id"
|
||||||
model="res.company"
|
model="res.company"
|
||||||
|
@ -8,18 +7,34 @@
|
||||||
report_type="qweb-html"
|
report_type="qweb-html"
|
||||||
name="report_qweb_parameter.test_report_length"
|
name="report_qweb_parameter.test_report_length"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<template id="test_report_length">
|
<template id="test_report_length">
|
||||||
<data>
|
<data>
|
||||||
<li name="esc_length" t-minlength="10" t-length="10"
|
<li
|
||||||
t-esc="docs[0].street" t-if="docs[0].street"/>
|
name="esc_length"
|
||||||
<li name="esc_maxlength" t-maxlength="10"
|
t-minlength="10"
|
||||||
t-esc="docs[0].website" t-if="docs[0].website"/>
|
t-length="10"
|
||||||
<li name="raw_length" t-minlength="10" t-length="10"
|
t-esc="docs[0].street"
|
||||||
t-raw="docs[0].vat" t-if="docs[0].vat"/>
|
t-if="docs[0].street"
|
||||||
<li name="raw_maxlength" t-maxlength="10"
|
/>
|
||||||
|
<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-raw="docs[0].company_registry"
|
||||||
t-if="docs[0].company_registry"/>
|
t-if="docs[0].company_registry"
|
||||||
|
/>
|
||||||
</data>
|
</data>
|
||||||
</template>
|
</template>
|
||||||
</odoo>
|
</odoo>
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
from . import ir_qweb
|
from . import ir_qweb
|
||||||
|
|
|
@ -1,47 +1,53 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright 2017 Creu Blanca
|
# Copyright 2017 Creu Blanca
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# 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
|
from odoo.exceptions import ValidationError
|
||||||
|
|
||||||
|
|
||||||
class IrQWeb(models.AbstractModel):
|
class IrQWeb(models.AbstractModel):
|
||||||
_inherit = 'ir.qweb'
|
_inherit = "ir.qweb"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def check_length(value, min_length=False, max_length=False):
|
def check_length(value, min_length=False, max_length=False):
|
||||||
if min_length and len(value) < min_length:
|
if min_length and len(value) < min_length:
|
||||||
raise ValidationError(
|
raise ValidationError(_("Length cannot be less than %s") % str(min_length))
|
||||||
_('Length cannot be less than %s') % str(min_length))
|
|
||||||
if max_length and len(value) > max_length:
|
if max_length and len(value) > max_length:
|
||||||
raise ValidationError(
|
raise ValidationError(_("Length cannot be more than %s") % str(max_length))
|
||||||
_('Length cannot be more than %s') % str(max_length))
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def _compile_directive_esc(self, el, options):
|
def _compile_directive_esc(self, el, options):
|
||||||
min_value = el.attrib.pop('t-minlength', False)
|
min_value = el.attrib.pop("t-minlength", False)
|
||||||
max_value = el.attrib.pop('t-maxlength', False)
|
max_value = el.attrib.pop("t-maxlength", False)
|
||||||
if min_value or max_value:
|
if min_value or max_value:
|
||||||
el.attrib['t-esc'] = 'docs.env["ir.qweb"].check_length(' + \
|
el.attrib["t-esc"] = (
|
||||||
el.attrib['t-esc'] + ', ' + \
|
'docs.env["ir.qweb"].check_length('
|
||||||
(min_value or 'False') + ', ' + \
|
+ el.attrib["t-esc"]
|
||||||
(max_value or 'False') + ')'
|
+ ", "
|
||||||
if 't-length' in el.attrib:
|
+ (min_value or "False")
|
||||||
length = el.attrib.pop('t-length')
|
+ ", "
|
||||||
el.attrib['t-esc'] = '(' + el.attrib[
|
+ (max_value or "False")
|
||||||
't-esc'] + ')[:' + length + ']'
|
+ ")"
|
||||||
|
)
|
||||||
|
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)
|
return super(IrQWeb, self)._compile_directive_esc(el, options)
|
||||||
|
|
||||||
def _compile_directive_raw(self, el, options):
|
def _compile_directive_raw(self, el, options):
|
||||||
min_value = el.attrib.pop('t-minlength', False)
|
min_value = el.attrib.pop("t-minlength", False)
|
||||||
max_value = el.attrib.pop('t-maxlength', False)
|
max_value = el.attrib.pop("t-maxlength", False)
|
||||||
if min_value or max_value:
|
if min_value or max_value:
|
||||||
el.attrib['t-raw'] = 'docs.env["ir.qweb"].check_length(' + \
|
el.attrib["t-raw"] = (
|
||||||
el.attrib['t-raw'] + ', ' + \
|
'docs.env["ir.qweb"].check_length('
|
||||||
(min_value or 'False') + ', ' + \
|
+ el.attrib["t-raw"]
|
||||||
(max_value or 'False') + ')'
|
+ ", "
|
||||||
if 't-length' in el.attrib:
|
+ (min_value or "False")
|
||||||
length = el.attrib.pop('t-length')
|
+ ", "
|
||||||
el.attrib['t-raw'] = el.attrib['t-raw'] + '[:' + length + ']'
|
+ (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)
|
return super(IrQWeb, self)._compile_directive_raw(el, options)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
from . import test_report_qweb_parameter
|
from . import test_report_qweb_parameter
|
||||||
|
|
|
@ -1,39 +1,40 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright 2017 Creu Blanca
|
# Copyright 2017 Creu Blanca
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
from odoo.addons.base.models.qweb import QWebException
|
|
||||||
from odoo.tests import common
|
from odoo.tests import common
|
||||||
|
|
||||||
|
from odoo.addons.base.models.qweb import QWebException
|
||||||
|
|
||||||
|
|
||||||
class TestReportQWebParameter(common.TransactionCase):
|
class TestReportQWebParameter(common.TransactionCase):
|
||||||
def test_qweb_parameter(self):
|
def test_qweb_parameter(self):
|
||||||
report_name = 'report_qweb_parameter.test_report_length'
|
report_name = "report_qweb_parameter.test_report_length"
|
||||||
report_obj = self.env['ir.actions.report']
|
report_obj = self.env["ir.actions.report"]
|
||||||
report_object = report_obj._get_report_from_name(report_name)
|
report_object = report_obj._get_report_from_name(report_name)
|
||||||
docs = self.env['res.company'].create({
|
docs = self.env["res.company"].create(
|
||||||
'name': 'Test company',
|
{
|
||||||
'street': '12345678901',
|
"name": "Test company",
|
||||||
'vat': '12345678901',
|
"street": "12345678901",
|
||||||
'company_registry': '1234567890'
|
"vat": "12345678901",
|
||||||
})
|
"company_registry": "1234567890",
|
||||||
docs.website = '1234567890' # for avoding that Odoo adds http://
|
}
|
||||||
|
)
|
||||||
|
docs.website = "1234567890" # for avoding that Odoo adds http://
|
||||||
rep = report_object.render(docs.ids, False)
|
rep = report_object.render(docs.ids, False)
|
||||||
root = ET.fromstring(rep[0])
|
root = ET.fromstring(rep[0])
|
||||||
self.assertEqual(root[0].text, "1234567890")
|
self.assertEqual(root[0].text, "1234567890")
|
||||||
self.assertEqual(root[2].text, "1234567890")
|
self.assertEqual(root[2].text, "1234567890")
|
||||||
docs.update({'street': '123456789'})
|
docs.update({"street": "123456789"})
|
||||||
with self.assertRaises(QWebException):
|
with self.assertRaises(QWebException):
|
||||||
report_object.render(docs.ids, False)
|
report_object.render(docs.ids, False)
|
||||||
docs.update({'street': '1234567890', 'vat': '123456789'})
|
docs.update({"street": "1234567890", "vat": "123456789"})
|
||||||
with self.assertRaises(QWebException):
|
with self.assertRaises(QWebException):
|
||||||
report_object.render(docs.ids, False)
|
report_object.render(docs.ids, False)
|
||||||
docs.update({'vat': '1234567890', 'website': '12345678901'})
|
docs.update({"vat": "1234567890", "website": "12345678901"})
|
||||||
with self.assertRaises(QWebException):
|
with self.assertRaises(QWebException):
|
||||||
report_object.render(docs.ids, False)
|
report_object.render(docs.ids, False)
|
||||||
docs.update(
|
docs.update({"website": "1234567890", "company_registry": "12345678901"})
|
||||||
{'website': '1234567890', 'company_registry': '12345678901'})
|
|
||||||
with self.assertRaises(QWebException):
|
with self.assertRaises(QWebException):
|
||||||
report_object.render(docs.ids, False)
|
report_object.render(docs.ids, False)
|
||||||
|
|
Loading…
Reference in New Issue