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.

73 lines
2.5 KiB

  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 AgreementAppendix(models.Model):
  5. _name = "agreement.appendix"
  6. _description = "Agreement Appendices"
  7. _order = "sequence"
  8. name = fields.Char(string="Name", required=True)
  9. title = fields.Char(
  10. string="Title",
  11. required=True,
  12. help="The title is displayed on the PDF." "The name is not.",
  13. )
  14. sequence = fields.Integer(
  15. string="Sequence",
  16. default=10)
  17. content = fields.Html(string="Content")
  18. dynamic_content = fields.Html(
  19. compute="_compute_dynamic_content",
  20. string="Dynamic Content",
  21. help="compute dynamic Content",
  22. )
  23. agreement_id = fields.Many2one(
  24. "agreement",
  25. string="Agreement",
  26. ondelete="cascade")
  27. active = fields.Boolean(
  28. string="Active",
  29. default=True,
  30. help="If unchecked, it will allow you to hide this appendix without "
  31. "removing it.",
  32. )
  33. # Dynamic field editor
  34. field_domain = fields.Char(string='Field Expression',
  35. default='[["active", "=", True]]')
  36. default_value = fields.Char(
  37. string="Default Value",
  38. help="Optional value to use if the target field is empty.")
  39. copyvalue = fields.Char(
  40. string="Placeholder Expression",
  41. help="""Final placeholder expression, to be copy-pasted in the desired
  42. template field.""")
  43. @api.onchange("field_domain", "default_value")
  44. def onchange_copyvalue(self):
  45. self.copyvalue = False
  46. if self.field_domain:
  47. string_list = self.field_domain.split(",")
  48. if string_list:
  49. field_domain = string_list[0][3:-1]
  50. self.copyvalue = "${{object.{} or {}}}".format(
  51. field_domain,
  52. self.default_value or "''")
  53. # compute the dynamic content for mako expression
  54. @api.multi
  55. def _compute_dynamic_content(self):
  56. MailTemplates = self.env["mail.template"]
  57. for appendix in self:
  58. lang = (
  59. appendix.agreement_id
  60. and appendix.agreement_id.partner_id.lang
  61. or "en_US"
  62. )
  63. content = MailTemplates.with_context(lang=lang)._render_template(
  64. appendix.content, "agreement.appendix", appendix.id
  65. )
  66. appendix.dynamic_content = content