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.4 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 AgreementSection(models.Model):
  5. _name = "agreement.section"
  6. _description = "Agreement Sections"
  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")
  13. agreement_id = fields.Many2one(
  14. "agreement", string="Agreement", ondelete="cascade")
  15. clauses_ids = fields.One2many(
  16. "agreement.clause", "section_id", string="Clauses", copy=True)
  17. content = fields.Html(string="Section Content")
  18. dynamic_content = fields.Html(
  19. compute="_compute_dynamic_content",
  20. string="Dynamic Content",
  21. help="compute dynamic Content")
  22. active = fields.Boolean(
  23. string="Active",
  24. default=True,
  25. help="If unchecked, it will allow you to hide the agreement without "
  26. "removing it.")
  27. # Dynamic field editor
  28. field_domain = fields.Char(string='Field Expression',
  29. default='[["active", "=", True]]')
  30. default_value = fields.Char(
  31. string="Default Value",
  32. help="Optional value to use if the target field is empty.")
  33. copyvalue = fields.Char(
  34. string="Placeholder Expression",
  35. help="""Final placeholder expression, to be copy-pasted in the desired
  36. template field.""")
  37. @api.onchange("field_domain", "default_value")
  38. def onchange_copyvalue(self):
  39. self.copyvalue = False
  40. if self.field_domain:
  41. string_list = self.field_domain.split(",")
  42. if string_list:
  43. field_domain = string_list[0][3:-1]
  44. self.copyvalue = "${{object.{} or {}}}".format(
  45. field_domain,
  46. self.default_value or "''")
  47. # compute the dynamic content for mako expression
  48. @api.multi
  49. def _compute_dynamic_content(self):
  50. MailTemplates = self.env["mail.template"]
  51. for section in self:
  52. lang = (section.agreement_id and
  53. section.agreement_id.partner_id.lang or "en_US")
  54. content = MailTemplates.with_context(lang=lang)._render_template(
  55. section.content, "agreement.section", section.id)
  56. section.dynamic_content = content