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.

96 lines
3.8 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 time
  5. import logging
  6. from base64 import b64decode
  7. from odoo.tools import misc, mail
  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(html.escape(value).replace('\n', '<text:line-break/>').
  16. replace('\t', '<text:s/><text:s/><text:s/><text:s/>'))
  17. return ""
  18. def display_address(address_record, without_company=False):
  19. return address_record.display_address(without_company=without_company)
  20. class Py3oParserContext(object):
  21. def __init__(self, env):
  22. self._env = env
  23. self.localcontext = {
  24. # Odoo default format methods
  25. 'o_format_lang': self._format_lang,
  26. # prefixes with o_ to avoid nameclash with default method provided
  27. # by py3o.template
  28. 'o_format_date': self._format_date,
  29. # give access to the time lib
  30. 'time': time,
  31. # keeps methods from report_sxw to ease migration
  32. 'display_address': display_address,
  33. 'formatLang': self._old_format_lang,
  34. 'format_multiline_value': format_multiline_value,
  35. 'html_sanitize': mail.html2plaintext,
  36. 'b64decode': b64decode,
  37. }
  38. def _format_lang(self, _env, value, digits=None, grouping=True,
  39. monetary=False, dp=False, currency_obj=False,
  40. no_break_space=True):
  41. formatted_value = misc.formatLang(
  42. _env, value, digits=digits, grouping=grouping,
  43. monetary=monetary, dp=dp, currency_obj=currency_obj)
  44. if currency_obj and currency_obj.symbol and no_break_space:
  45. parts = []
  46. if currency_obj.position == 'after':
  47. parts = formatted_value.rsplit(" ", 1)
  48. elif currency_obj and currency_obj.position == 'before':
  49. parts = formatted_value.split(" ", 1)
  50. if parts:
  51. formatted_value = "\N{NO-BREAK SPACE}".join(parts)
  52. return formatted_value
  53. def _format_date(self, value, lang_code=False, date_format=False):
  54. return misc.format_date(
  55. self._env, value, lang_code=lang_code, date_format=date_format)
  56. def _old_format_lang(self, value, digits=None, date=False, date_time=False,
  57. grouping=True, monetary=False, dp=False,
  58. currency_obj=False):
  59. """
  60. :param value: The value to format
  61. :param digits: Number of digits to display by default
  62. :param date: True if value must be formatted as a date (default False)
  63. :param date_time: True if value must be formatted as a datetime
  64. (default False)
  65. :param grouping: If value is float and grouping is True, the value will
  66. be formatted with the appropriate separators between
  67. figures according to the current lang specifications
  68. :param monetary: If value is float and monetary is True and grouping is
  69. True the value will be formatted according to the
  70. monetary format defined for the current lang
  71. :param dp: Decimal precision
  72. :param currency_obj: If provided the currency symbol will be added to
  73. value at position defined by the currency object
  74. :return: The formatted value
  75. """
  76. if not date and not date_time:
  77. return self._format_lang(
  78. self._env, value, digits=digits, grouping=grouping,
  79. monetary=monetary, dp=dp, currency_obj=currency_obj,
  80. no_break_space=True)
  81. return self._format_date(self._env, value)