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.

50 lines
1.7 KiB

  1. # Copyright 2018 Road-Support - Roel Adriaans
  2. import re
  3. from odoo import api, fields, models
  4. from odoo.tools.misc import format_date
  5. class AccountAnalyticAccount(models.Model):
  6. _inherit = 'account.analytic.account'
  7. def _format_date(self, date, partner_lang, parse):
  8. try:
  9. res = format_date(self.env, date, partner_lang, parse)
  10. return res
  11. except (AttributeError, ValueError) as e:
  12. # At the moment we catch exceptions silent, and return
  13. # an empty string.
  14. # Should we raise an error, or create a new mail.activity?
  15. return ""
  16. @api.model
  17. def _insert_markers(self, line, date_format):
  18. date_from = fields.Date.from_string(line.date_from)
  19. date_to = fields.Date.from_string(line.date_to)
  20. from_regex = r"#START\((.*?)\)#"
  21. to_regex = r"#END\((.*?)\)#"
  22. name = line.name
  23. from_result = re.findall(from_regex, name)
  24. to_result = re.findall(to_regex, name)
  25. partner_lang = line.analytic_account_id.partner_id.lang
  26. if from_result and len(from_result[0]) > 1:
  27. from_string = self._format_date(date_from, partner_lang,
  28. from_result[0])
  29. name = re.sub(from_regex, from_string, name)
  30. else:
  31. # Original behaviour
  32. name = name.replace('#START#', date_from.strftime(date_format))
  33. if to_result and len(to_result[0]) > 1:
  34. to_string = self._format_date(date_to, partner_lang, to_result[0])
  35. name = re.sub(to_regex, to_string, name)
  36. else:
  37. # Original behaviour
  38. name = name.replace('#END#', date_to.strftime(date_format))
  39. return name