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.

77 lines
3.2 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')
  48. def onchange_sub_object_id(self):
  49. if self.field_id:
  50. self.sub_object_id = self.env['ir.model'].search(
  51. [('model', '=', self.field_id.relation)])[0]
  52. @api.onchange('sub_model_object_field_id', 'default_value')
  53. def onchange_copyvalue(self):
  54. if self.sub_model_object_field_id or self.default_value:
  55. self.copyvalue = "${object.%s.%s or %s}" % \
  56. (self.field_id.name,
  57. self.sub_model_object_field_id.name,
  58. self.default_value or '\'\'')
  59. # compute the dynamic content for mako expression
  60. @api.multi
  61. def _compute_dynamic_content(self):
  62. MailTemplates = self.env['mail.template']
  63. for recital in self:
  64. lang = recital.agreement_id and \
  65. recital.agreement_id.partner_id.lang or 'en_US'
  66. content = MailTemplates.with_context(
  67. lang=lang).render_template(
  68. recital.content, 'agreement.recital', recital.id)
  69. recital.dynamic_content = content