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.

87 lines
3.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 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_id = fields.Many2one(
  29. "ir.model.fields",
  30. string="Field",
  31. help="""Select target field from the related document model. If it is a
  32. relationship field you will be able to select a target field at the
  33. destination of the relationship.""")
  34. sub_object_id = fields.Many2one(
  35. "ir.model",
  36. string="Sub-model",
  37. help="""When a relationship field is selected as first field, this
  38. field shows the document model the relationship goes to.""")
  39. sub_model_object_field_id = fields.Many2one(
  40. "ir.model.fields",
  41. string="Sub-field",
  42. help="""When a relationship field is selected as first field, this
  43. field lets you select the target field within the destination document
  44. model (sub-model).""")
  45. default_value = fields.Char(
  46. string="Default Value",
  47. help="Optional value to use if the target field is empty.")
  48. copyvalue = fields.Char(
  49. string="Placeholder Expression",
  50. help="""Final placeholder expression, to be copy-pasted in the desired
  51. template field.""")
  52. @api.onchange("field_id", "sub_model_object_field_id", "default_value")
  53. def onchange_copyvalue(self):
  54. self.sub_object_id = False
  55. self.copyvalue = False
  56. self.sub_object_id = False
  57. if self.field_id and not self.field_id.relation:
  58. self.copyvalue = "${{object.{} or {}}}".format(
  59. self.field_id.name, self.default_value or "''")
  60. self.sub_model_object_field_id = False
  61. if self.field_id and self.field_id.relation:
  62. self.sub_object_id = self.env["ir.model"].search(
  63. [("model", "=", self.field_id.relation)]
  64. )[0]
  65. if self.sub_model_object_field_id:
  66. self.copyvalue = "${{object.{}.{} or {}}}".format(
  67. self.field_id.name,
  68. self.sub_model_object_field_id.name,
  69. self.default_value or "''",
  70. )
  71. # compute the dynamic content for mako expression
  72. @api.multi
  73. def _compute_dynamic_content(self):
  74. MailTemplates = self.env["mail.template"]
  75. for section in self:
  76. lang = (section.agreement_id and
  77. section.agreement_id.partner_id.lang or "en_US")
  78. content = MailTemplates.with_context(lang=lang)._render_template(
  79. section.content, "agreement.section", section.id)
  80. section.dynamic_content = content