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