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.

63 lines
2.3 KiB

5 years ago
  1. # Copyright (C) 2018 - TODAY, Pavlov Media
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class AgreementRecital(models.Model):
  5. _name = "agreement.recital"
  6. _description = "Agreement Recitals"
  7. _order = "sequence"
  8. name = fields.Char(string="Name", required=True)
  9. title = fields.Char(
  10. string="Title",
  11. help="The title is displayed on the PDF." "The name is not.")
  12. sequence = fields.Integer(string="Sequence", default=10)
  13. content = fields.Html(string="Content")
  14. dynamic_content = fields.Html(
  15. compute="_compute_dynamic_content",
  16. string="Dynamic Content",
  17. help="compute dynamic Content")
  18. agreement_id = fields.Many2one(
  19. "agreement", string="Agreement", ondelete="cascade")
  20. active = fields.Boolean(
  21. string="Active",
  22. default=True,
  23. help="If unchecked, it will allow you to hide this recital without "
  24. "removing it.")
  25. # Dynamic field editor
  26. field_domain = fields.Char(string='Field Expression',
  27. default='[["active", "=", True]]')
  28. default_value = fields.Char(
  29. string="Default Value",
  30. help="Optional value to use if the target field is empty.")
  31. copyvalue = fields.Char(
  32. string="Placeholder Expression",
  33. help="""Final placeholder expression, to be copy-pasted in the desired
  34. template field.""")
  35. @api.onchange("field_domain", "default_value")
  36. def onchange_copyvalue(self):
  37. self.copyvalue = False
  38. if self.field_domain:
  39. string_list = self.field_domain.split(",")
  40. if string_list:
  41. field_domain = string_list[0][3:-1]
  42. self.copyvalue = "${{object.{} or {}}}".format(
  43. field_domain,
  44. self.default_value or "''")
  45. # compute the dynamic content for mako expression
  46. @api.multi
  47. def _compute_dynamic_content(self):
  48. MailTemplates = self.env["mail.template"]
  49. for recital in self:
  50. lang = (
  51. recital.agreement_id
  52. and recital.agreement_id.partner_id.lang
  53. or "en_US")
  54. content = MailTemplates.with_context(lang=lang)._render_template(
  55. recital.content, "agreement.recital", recital.id)
  56. recital.dynamic_content = content