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.

44 lines
1.6 KiB

  1. # Copyright 2018 Onestein
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class MailTemplate(models.Model):
  5. _inherit = 'mail.template'
  6. ir_attachment_language_ids = fields.One2many(
  7. string='Language Dependent Attachments',
  8. comodel_name='ir.attachment.language',
  9. inverse_name='mail_template_id',
  10. )
  11. @api.multi
  12. def generate_email(self, res_ids, fields=None):
  13. self.ensure_one()
  14. multi = True
  15. if isinstance(res_ids, int):
  16. res_ids = [res_ids]
  17. multi = False
  18. res = super().generate_email(
  19. res_ids, fields
  20. )
  21. attached = []
  22. for res_id in res.keys():
  23. mail = res[res_id]
  24. partner_ids = 'partner_ids' in mail and \
  25. mail['partner_ids'] or False
  26. if not partner_ids:
  27. continue
  28. for partner in self.env['res.partner'].browse(partner_ids):
  29. for lang_attach in self.ir_attachment_language_ids.filtered(
  30. lambda a: a.lang == partner.lang):
  31. if lang_attach.attachment_id.id in attached:
  32. continue
  33. if not res[res_id].get('attachments'):
  34. res[res_id]['attachments'] = []
  35. res[res_id]['attachments'].append((
  36. lang_attach.attachment_id.name,
  37. lang_attach.attachment_id.datas))
  38. attached.append(lang_attach.id)
  39. return multi and res or res[res_ids[0]]