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.4 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')
  47. def onchange_sub_object_id(self):
  48. if self.field_id:
  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 section in self:
  63. lang = section.agreement_id and \
  64. section.agreement_id.partner_id.lang or 'en_US'
  65. content = MailTemplates.with_context(
  66. lang=lang).render_template(
  67. section.content, 'agreement.section', section.id)
  68. section.dynamic_content = content