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.

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