Carlos Roca
4 years ago
committed by
Francisco Ivan Anton Prieto
7 changed files with 83 additions and 70 deletions
-
1report_qweb_parameter/__init__.py
-
12report_qweb_parameter/__manifest__.py
-
37report_qweb_parameter/demo/test_report_field_length.xml
-
1report_qweb_parameter/models/__init__.py
-
58report_qweb_parameter/models/ir_qweb.py
-
1report_qweb_parameter/tests/__init__.py
-
33report_qweb_parameter/tests/test_report_qweb_parameter.py
@ -1,4 +1,3 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
|
|||
from . import models |
@ -1,4 +1,3 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
|
|||
from . import ir_qweb |
@ -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,4 +1,3 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
|
|||
from . import test_report_qweb_parameter |
@ -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) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue