OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.0 KiB

  1. # Copyright 2017 Creu Blanca
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import _, models
  4. from odoo.exceptions import ValidationError
  5. class IrQWeb(models.AbstractModel):
  6. _inherit = "ir.qweb"
  7. @staticmethod
  8. def check_length(value, min_length=False, max_length=False):
  9. if min_length and len(value) < min_length:
  10. raise ValidationError(_("Length cannot be less than %s") % str(min_length))
  11. if max_length and len(value) > max_length:
  12. raise ValidationError(_("Length cannot be more than %s") % str(max_length))
  13. return value
  14. def _compile_directive_esc(self, el, options):
  15. min_value = el.attrib.pop("t-minlength", False)
  16. max_value = el.attrib.pop("t-maxlength", False)
  17. if min_value or max_value:
  18. el.attrib["t-esc"] = (
  19. 'docs.env["ir.qweb"].check_length('
  20. + el.attrib["t-esc"]
  21. + ", "
  22. + (min_value or "False")
  23. + ", "
  24. + (max_value or "False")
  25. + ")"
  26. )
  27. if "t-length" in el.attrib:
  28. tlength = el.attrib.pop("t-length")
  29. el.attrib["t-esc"] = "(" + el.attrib["t-esc"] + ")[:" + tlength + "]"
  30. return super()._compile_directive_esc(el, options)
  31. def _compile_directive_raw(self, el, options):
  32. min_value = el.attrib.pop("t-minlength", False)
  33. max_value = el.attrib.pop("t-maxlength", False)
  34. if min_value or max_value:
  35. el.attrib["t-raw"] = (
  36. 'docs.env["ir.qweb"].check_length('
  37. + el.attrib["t-raw"]
  38. + ", "
  39. + (min_value or "False")
  40. + ", "
  41. + (max_value or "False")
  42. + ")"
  43. )
  44. if "t-length" in el.attrib:
  45. tlength = el.attrib.pop("t-length")
  46. el.attrib["t-raw"] = el.attrib["t-raw"] + "[:" + tlength + "]"
  47. return super()._compile_directive_raw(el, options)