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.

71 lines
2.5 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 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(
  10. string="Title",
  11. help="The title is displayed on the PDF." "The name is not.")
  12. sequence = fields.Integer(string="Sequence")
  13. agreement_id = fields.Many2one(
  14. "agreement",
  15. string="Agreement",
  16. ondelete="cascade")
  17. section_id = fields.Many2one(
  18. "agreement.section",
  19. string="Section",
  20. ondelete="cascade")
  21. content = fields.Html(string="Clause Content")
  22. dynamic_content = fields.Html(
  23. compute="_compute_dynamic_content",
  24. string="Dynamic Content",
  25. help="compute dynamic Content")
  26. active = fields.Boolean(
  27. string="Active",
  28. default=True,
  29. help="If unchecked, it will allow you to hide the agreement without "
  30. "removing it.")
  31. # Dynamic field editor
  32. field_domain = fields.Char(string='Field Expression',
  33. default='[["active", "=", True]]')
  34. default_value = fields.Char(
  35. string="Default Value",
  36. help="Optional value to use if the target field is empty.")
  37. copyvalue = fields.Char(
  38. string="Placeholder Expression",
  39. help="""Final placeholder expression, to be copy-pasted in the desired
  40. template field.""")
  41. @api.onchange("field_domain", "default_value")
  42. def onchange_copyvalue(self):
  43. self.copyvalue = False
  44. if self.field_domain:
  45. string_list = self.field_domain.split(",")
  46. if string_list:
  47. field_domain = string_list[0][3:-1]
  48. self.copyvalue = "${{object.{} or {}}}".format(
  49. field_domain,
  50. self.default_value or "''")
  51. # compute the dynamic content for mako expression
  52. @api.multi
  53. def _compute_dynamic_content(self):
  54. MailTemplates = self.env["mail.template"]
  55. for clause in self:
  56. lang = (
  57. clause.agreement_id
  58. and clause.agreement_id.partner_id.lang
  59. or "en_US"
  60. )
  61. content = MailTemplates.with_context(lang=lang)._render_template(
  62. clause.content, "agreement.clause", clause.id
  63. )
  64. clause.dynamic_content = content