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.

81 lines
3.7 KiB

6 years ago
6 years ago
  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 AgreementSection(models.Model):
  5. _name = 'agreement.section'
  6. _description = 'Agreement Sections'
  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")
  13. agreement_id = fields.Many2one('agreement', string="Agreement",
  14. ondelete="cascade")
  15. clauses_ids = fields.One2many('agreement.clause', 'section_id',
  16. string="Clauses")
  17. content = fields.Html(string="Section Content")
  18. dynamic_content = fields.Html(compute="_compute_dynamic_content",
  19. string="Dynamic Content",
  20. help='compute dynamic Content')
  21. active = fields.Boolean(string="Active", default=True,
  22. help="""If unchecked, it will allow you to hide the
  23. agreement without 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', 'sub_model_object_field_id', 'default_value')
  47. def onchange_copyvalue(self):
  48. self.sub_object_id = False
  49. self.copyvalue = False
  50. self.sub_object_id = False
  51. if self.field_id and not self.field_id.relation:
  52. self.copyvalue = "${object.%s or %s}" % \
  53. (self.field_id.name,
  54. self.default_value or '\'\'')
  55. self.sub_model_object_field_id = False
  56. if self.field_id and self.field_id.relation:
  57. self.sub_object_id = self.env['ir.model'].search(
  58. [('model', '=', self.field_id.relation)])[0]
  59. if self.sub_model_object_field_id:
  60. self.copyvalue = "${object.%s.%s or %s}" %\
  61. (self.field_id.name,
  62. self.sub_model_object_field_id.name,
  63. self.default_value or '\'\'')
  64. # compute the dynamic content for mako expression
  65. @api.multi
  66. def _compute_dynamic_content(self):
  67. MailTemplates = self.env['mail.template']
  68. for section in self:
  69. lang = section.agreement_id and \
  70. section.agreement_id.partner_id.lang or 'en_US'
  71. content = MailTemplates.with_context(
  72. lang=lang).render_template(
  73. section.content, 'agreement.section', section.id)
  74. section.dynamic_content = content