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.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2017 Compassion CH (http://www.compassion.ch)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import models, fields, api
  5. class EmailTemplate(models.Model):
  6. _inherit = 'mail.template'
  7. ##########################################################################
  8. # FIELDS #
  9. ##########################################################################
  10. substitution_ids = fields.One2many(
  11. 'sendgrid.substitution', 'email_template_id', 'Substitutions')
  12. sendgrid_template_ids = fields.One2many(
  13. 'sendgrid.email.lang.template', 'email_template_id',
  14. 'Sendgrid Templates')
  15. sendgrid_localized_template = fields.Many2one(
  16. 'sendgrid.template', compute='_compute_localized_template')
  17. def _compute_localized_template(self):
  18. lang = self.env.context.get('lang', 'en_US')
  19. for template in self:
  20. lang_template = template.sendgrid_template_ids.filtered(
  21. lambda t: t.lang == lang)
  22. if lang_template and len(lang_template) == 1:
  23. template.sendgrid_localized_template = \
  24. lang_template.sendgrid_template_id
  25. @api.multi
  26. def update_substitutions(self):
  27. self.ensure_one()
  28. new_substitutions = list()
  29. for language_template in self.sendgrid_template_ids:
  30. sendgrid_template = language_template.sendgrid_template_id
  31. lang = language_template.lang
  32. substitutions = self.substitution_ids.filtered(
  33. lambda s: s.lang == lang)
  34. keywords = sendgrid_template.get_keywords()
  35. # Add new keywords from the sendgrid template
  36. for key in keywords:
  37. if key not in substitutions.mapped('key'):
  38. substitution_vals = {
  39. 'key': key,
  40. 'lang': lang,
  41. 'email_template_id': self.id
  42. }
  43. new_substitutions.append((0, 0, substitution_vals))
  44. return self.write({'substitution_ids': new_substitutions})
  45. @api.multi
  46. def render_substitutions(self, res_ids):
  47. """
  48. :param res_ids: resource ids for rendering the template
  49. Returns values for substitutions in a mail.message creation
  50. :return:
  51. Values for mail creation (for each resource id given)
  52. {res_id: list of substitutions values [0, 0 {substitution_vals}]}
  53. """
  54. self.ensure_one()
  55. if isinstance(res_ids, (int, long)):
  56. res_ids = [res_ids]
  57. substitutions = self.substitution_ids.filtered(
  58. lambda s: s.lang == self.env.context.get('lang', 'en_US'))
  59. substitution_vals = {res_id: list() for res_id in res_ids}
  60. for substitution in substitutions:
  61. values = self.render_template(
  62. substitution.value, self.model, res_ids)
  63. for res_id in res_ids:
  64. substitution_vals[res_id].append((0, 0, {
  65. 'key': substitution.key,
  66. 'value': values[res_id]
  67. }))
  68. return substitution_vals