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.

99 lines
3.5 KiB

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