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.

83 lines
3.4 KiB

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