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.

101 lines
3.6 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 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(
  14. string="Sequence",
  15. default=10)
  16. content = fields.Html(string="Content")
  17. dynamic_content = fields.Html(
  18. compute="_compute_dynamic_content",
  19. string="Dynamic Content",
  20. help="compute dynamic Content",
  21. )
  22. agreement_id = fields.Many2one(
  23. "agreement",
  24. string="Agreement",
  25. ondelete="cascade")
  26. active = fields.Boolean(
  27. string="Active",
  28. default=True,
  29. help="If unchecked, it will allow you to hide this recital without "
  30. "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.%s or %s}" % \
  69. (self.field_id.name,
  70. self.default_value or '\'\'')
  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)])[0]
  75. if self.sub_model_object_field_id:
  76. self.copyvalue = "${object.%s.%s or %s}" %\
  77. (self.field_id.name,
  78. self.sub_model_object_field_id.name,
  79. self.default_value or '\'\'')
  80. # compute the dynamic content for mako expression
  81. @api.multi
  82. def _compute_dynamic_content(self):
  83. MailTemplates = self.env["mail.template"]
  84. for recital in self:
  85. lang = (
  86. recital.agreement_id
  87. and recital.agreement_id.partner_id.lang
  88. or "en_US"
  89. )
  90. content = MailTemplates.with_context(lang=lang)._render_template(
  91. recital.content, "agreement.recital", recital.id
  92. )
  93. recital.dynamic_content = content