mail_template.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. from odoo import models
  3. class MailTemplate(models.Model):
  4. _inherit = "mail.template"
  5. def _get_edi_attachments(self, document):
  6. """
  7. Will either return the information about the attachment of the edi document for adding the attachment in the
  8. mail, or the attachment id to be linked to the 'send & print' wizard.
  9. Can be overridden where e.g. a zip-file needs to be sent with the individual files instead of the entire zip
  10. IMPORTANT:
  11. * If the attachment's id is returned, no new attachment will be created, the existing one on the move is linked
  12. to the wizard (see _onchange_template_id in mail.compose.message).
  13. * If the attachment's content is returned, a new one is created and linked to the wizard. Thus, when sending
  14. the mail (clicking on 'send & print' in the wizard), a new attachment is added to the move (see
  15. _action_send_mail in mail.compose.message).
  16. :param document: an edi document
  17. :return: dict:
  18. {'attachments': tuple with the name and base64 content of the attachment}
  19. OR
  20. {'attachment_ids': list containing the id of the attachment}
  21. """
  22. attachment_sudo = document.sudo().attachment_id
  23. if not attachment_sudo:
  24. return {}
  25. if not (attachment_sudo.res_model and attachment_sudo.res_id):
  26. # do not return system attachment not linked to a record
  27. return {}
  28. if len(self._context.get('active_ids', [])) > 1:
  29. # In mass mail mode 'attachments_ids' is removed from template values
  30. # as they should not be rendered
  31. return {'attachments': [(attachment_sudo.name, attachment_sudo.datas)]}
  32. return {'attachment_ids': [attachment_sudo.id]}
  33. def generate_email(self, res_ids, fields):
  34. res = super().generate_email(res_ids, fields)
  35. multi_mode = True
  36. if isinstance(res_ids, int):
  37. res_ids = [res_ids]
  38. multi_mode = False
  39. if self.model not in ['account.move', 'account.payment']:
  40. return res
  41. records = self.env[self.model].browse(res_ids)
  42. for record in records:
  43. record_data = (res[record.id] if multi_mode else res)
  44. for doc in record.edi_document_ids:
  45. record_data.setdefault('attachments', [])
  46. attachments = self._get_edi_attachments(doc)
  47. record_data['attachment_ids'] += attachments.get('attachment_ids', [])
  48. record_data['attachments'] += attachments.get('attachments', [])
  49. return res