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.

90 lines
3.6 KiB

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