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.

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