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.

76 lines
3.3 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(string="Title", required=True,
  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", default=True,
  22. help="If unchecked, it will allow you to hide this appendix without "
  23. "removing it.")
  24. # Dynamic field editor
  25. field_id = fields.Many2one(
  26. 'ir.model.fields', string="Field",
  27. help="""Select target field from the related document model. If it is a
  28. relationship field you will be able to select a target field at the
  29. destination of the relationship.""")
  30. sub_object_id = fields.Many2one(
  31. 'ir.model', string="Sub-model",
  32. help="""When a relationship field is selected as first field, this
  33. field shows the document model the relationship goes to.""")
  34. sub_model_object_field_id = fields.Many2one(
  35. 'ir.model.fields', string="Sub-field",
  36. help="""When a relationship field is selected as first field, this
  37. field lets you select the target field within the destination document
  38. model (sub-model).""")
  39. default_value = fields.Char(
  40. string="Default Value",
  41. help="Optional value to use if the target field is empty.")
  42. copyvalue = fields.Char(
  43. string="Placeholder Expression",
  44. help="""Final placeholder expression, to be copy-pasted in the desired
  45. template field.""")
  46. @api.onchange('field_id')
  47. def onchange_sub_object_id(self):
  48. if self.field_id and self.field_id.relation:
  49. self.sub_object_id = self.env['ir.model'].search(
  50. [('model', '=', self.field_id.relation)])[0]
  51. @api.onchange('sub_model_object_field_id', 'default_value')
  52. def onchange_copyvalue(self):
  53. if self.sub_model_object_field_id or self.default_value:
  54. self.copyvalue = "${object.%s.%s or %s}" % \
  55. (self.field_id.name,
  56. self.sub_model_object_field_id.name,
  57. self.default_value or '\'\'')
  58. # compute the dynamic content for mako expression
  59. @api.multi
  60. def _compute_dynamic_content(self):
  61. MailTemplates = self.env['mail.template']
  62. for appendix in self:
  63. lang = appendix.agreement_id and \
  64. appendix.agreement_id.partner_id.lang or 'en_US'
  65. content = MailTemplates.with_context(
  66. lang=lang).render_template(
  67. appendix.content, 'agreement.appendix', appendix.id)
  68. appendix.dynamic_content = content