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.

47 lines
2.0 KiB

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