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.

102 lines
3.7 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. )
  13. sequence = fields.Integer(string="Sequence")
  14. agreement_id = fields.Many2one(
  15. "agreement", string="Agreement", ondelete="cascade"
  16. )
  17. clauses_ids = fields.One2many(
  18. "agreement.clause", "section_id", string="Clauses"
  19. )
  20. content = fields.Html(string="Section Content")
  21. dynamic_content = fields.Html(
  22. compute="_compute_dynamic_content",
  23. string="Dynamic Content",
  24. help="compute dynamic Content",
  25. )
  26. active = fields.Boolean(
  27. string="Active",
  28. default=True,
  29. help="""If unchecked, it will allow you to hide the
  30. agreement without removing it.""",
  31. )
  32. # Dynamic field editor
  33. field_id = fields.Many2one(
  34. "ir.model.fields",
  35. string="Field",
  36. help="""Select target field from the related document model. If it is a
  37. relationship field you will be able to select a target field at the
  38. destination of the relationship.""",
  39. )
  40. sub_object_id = fields.Many2one(
  41. "ir.model",
  42. string="Sub-model",
  43. help="""When a relationship field is selected as first field, this
  44. field shows the document model the relationship goes to.""",
  45. )
  46. sub_model_object_field_id = fields.Many2one(
  47. "ir.model.fields",
  48. string="Sub-field",
  49. help="""When a relationship field is selected as first field, this
  50. field lets you select the target field within the destination document
  51. model (sub-model).""",
  52. )
  53. default_value = fields.Char(
  54. string="Default Value",
  55. help="Optional value to use if the target field is empty.",
  56. )
  57. copyvalue = fields.Char(
  58. string="Placeholder Expression",
  59. help="""Final placeholder expression, to be copy-pasted in the desired
  60. template field.""",
  61. )
  62. @api.onchange("field_id", "sub_model_object_field_id", "default_value")
  63. def onchange_copyvalue(self):
  64. self.sub_object_id = False
  65. self.copyvalue = False
  66. self.sub_object_id = False
  67. if self.field_id and not self.field_id.relation:
  68. self.copyvalue = "${{object.{} or {}}}".format(
  69. self.field_id.name, self.default_value or "''"
  70. )
  71. self.sub_model_object_field_id = False
  72. if self.field_id and self.field_id.relation:
  73. self.sub_object_id = self.env["ir.model"].search(
  74. [("model", "=", self.field_id.relation)]
  75. )[0]
  76. if self.sub_model_object_field_id:
  77. self.copyvalue = "${{object.{}.{} or {}}}".format(
  78. self.field_id.name,
  79. self.sub_model_object_field_id.name,
  80. self.default_value or "''",
  81. )
  82. # compute the dynamic content for mako expression
  83. @api.multi
  84. def _compute_dynamic_content(self):
  85. MailTemplates = self.env["mail.template"]
  86. for section in self:
  87. lang = (
  88. section.agreement_id
  89. and section.agreement_id.partner_id.lang
  90. or "en_US"
  91. )
  92. content = MailTemplates.with_context(lang=lang)._render_template(
  93. section.content, "agreement.section", section.id
  94. )
  95. section.dynamic_content = content