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.

133 lines
4.3 KiB

  1. # Copyright 2018 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
  3. import html
  4. import logging
  5. import time
  6. from base64 import b64decode
  7. from odoo.tools import mail, misc
  8. logger = logging.getLogger(__name__)
  9. try:
  10. from genshi.core import Markup
  11. except ImportError:
  12. logger.debug("Cannot import py3o.template")
  13. def format_multiline_value(value):
  14. if value:
  15. return Markup(
  16. html.escape(value)
  17. .replace("\n", "<text:line-break/>")
  18. .replace("\t", "<text:s/><text:s/><text:s/><text:s/>")
  19. )
  20. return ""
  21. def display_address(address_record, without_company=False):
  22. return address_record.display_address(without_company=without_company)
  23. class Py3oParserContext(object):
  24. def __init__(self, env):
  25. self._env = env
  26. self.localcontext = {
  27. "user": self._env.user,
  28. "lang": self._env.lang,
  29. # Odoo default format methods
  30. "o_format_lang": self._format_lang,
  31. # prefixes with o_ to avoid nameclash with default method provided
  32. # by py3o.template
  33. "o_format_date": self._format_date,
  34. # give access to the time lib
  35. "time": time,
  36. # keeps methods from report_sxw to ease migration
  37. "display_address": display_address,
  38. "formatLang": self._old_format_lang,
  39. "format_multiline_value": format_multiline_value,
  40. "html_sanitize": mail.html2plaintext,
  41. "b64decode": b64decode,
  42. }
  43. def _format_lang(
  44. self,
  45. value,
  46. lang_code=False,
  47. digits=None,
  48. grouping=True,
  49. monetary=False,
  50. dp=False,
  51. currency_obj=False,
  52. no_break_space=True,
  53. ):
  54. env = self._env
  55. if lang_code:
  56. context = dict(env.context, lang=lang_code)
  57. env = env(context=context)
  58. formatted_value = misc.formatLang(
  59. env,
  60. value,
  61. digits=digits,
  62. grouping=grouping,
  63. monetary=monetary,
  64. dp=dp,
  65. currency_obj=currency_obj,
  66. )
  67. if currency_obj and currency_obj.symbol and no_break_space:
  68. parts = []
  69. if currency_obj.position == "after":
  70. parts = formatted_value.rsplit(" ", 1)
  71. elif currency_obj and currency_obj.position == "before":
  72. parts = formatted_value.split(" ", 1)
  73. if parts:
  74. formatted_value = "\N{NO-BREAK SPACE}".join(parts)
  75. return formatted_value
  76. def _format_date(self, value, lang_code=False, date_format=False):
  77. return misc.format_date(
  78. self._env, value, lang_code=lang_code, date_format=date_format
  79. )
  80. def _old_format_lang(
  81. self,
  82. value,
  83. digits=None,
  84. date=False,
  85. date_time=False,
  86. grouping=True,
  87. monetary=False,
  88. dp=False,
  89. currency_obj=False,
  90. ):
  91. """
  92. :param value: The value to format
  93. :param digits: Number of digits to display by default
  94. :param date: True if value must be formatted as a date (default False)
  95. :param date_time: True if value must be formatted as a datetime
  96. (default False)
  97. :param grouping: If value is float and grouping is True, the value will
  98. be formatted with the appropriate separators between
  99. figures according to the current lang specifications
  100. :param monetary: If value is float and monetary is True and grouping is
  101. True the value will be formatted according to the
  102. monetary format defined for the current lang
  103. :param dp: Decimal precision
  104. :param currency_obj: If provided the currency symbol will be added to
  105. value at position defined by the currency object
  106. :return: The formatted value
  107. """
  108. if not date and not date_time:
  109. return self._format_lang(
  110. value,
  111. digits=digits,
  112. grouping=grouping,
  113. monetary=monetary,
  114. dp=dp,
  115. currency_obj=currency_obj,
  116. no_break_space=True,
  117. )
  118. return self._format_date(self._env, value)