gooderp18绿色标准版
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.

77 line
2.3KB

  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import dateutil.relativedelta as relativedelta
  4. import functools
  5. import re
  6. from markupsafe import Markup
  7. from werkzeug import urls
  8. from odoo.tools import safe_eval
  9. INLINE_TEMPLATE_REGEX = re.compile(r"\{\{(.+?)(\|\|\|\s*(.*?))?\}\}")
  10. def relativedelta_proxy(*args, **kwargs):
  11. # dateutil.relativedelta is an old-style class and cannot be directly
  12. # instanciated wihtin a jinja2 expression, so a lambda "proxy" is
  13. # is needed, apparently
  14. return relativedelta.relativedelta(*args, **kwargs)
  15. template_env_globals = {
  16. 'str': str,
  17. 'quote': urls.url_quote,
  18. 'urlencode': urls.url_encode,
  19. 'datetime': safe_eval.datetime,
  20. 'len': len,
  21. 'abs': abs,
  22. 'min': min,
  23. 'max': max,
  24. 'sum': sum,
  25. 'filter': filter,
  26. 'reduce': functools.reduce,
  27. 'map': map,
  28. 'relativedelta': relativedelta.relativedelta,
  29. 'round': round,
  30. 'hasattr': hasattr,
  31. }
  32. def parse_inline_template(text):
  33. groups = []
  34. current_literal_index = 0
  35. for match in INLINE_TEMPLATE_REGEX.finditer(text):
  36. literal = text[current_literal_index:match.start()]
  37. expression = match.group(1)
  38. default = match.group(3)
  39. groups.append((literal, expression.strip(), default or ''))
  40. current_literal_index = match.end()
  41. # string past last regex match
  42. literal = text[current_literal_index:]
  43. if literal:
  44. groups.append((literal, '', ''))
  45. return groups
  46. def convert_inline_template_to_qweb(template):
  47. template_instructions = parse_inline_template(template or '')
  48. preview_markup = []
  49. for string, expression, default in template_instructions:
  50. if expression:
  51. preview_markup.append(Markup('{}<t t-out="{}">{}</t>').format(string, expression, default))
  52. else:
  53. preview_markup.append(string)
  54. return Markup('').join(preview_markup)
  55. def render_inline_template(template_instructions, variables):
  56. results = []
  57. for string, expression, default in template_instructions:
  58. results.append(string)
  59. if expression:
  60. result = safe_eval.safe_eval(expression, variables) or default
  61. if result:
  62. results.append(str(result))
  63. return ''.join(results)
上海开阖软件有限公司 沪ICP备12045867号-1