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.

55 lines
2.3 KiB

  1. from odoo import api, models
  2. class MailTemplate(models.Model):
  3. _inherit = "mail.template"
  4. @api.multi
  5. def send_mail_with_multiple_attachments(self, res_id,
  6. additional_attachments,
  7. force_send=False,
  8. raise_exception=False):
  9. """Generates a new mail message for the given template and record,
  10. and schedules it for delivery through the ``mail``
  11. module's scheduler.
  12. :param int res_id: id of the record to render the template with
  13. (model is taken from the template)
  14. :param bool force_send: if True, the generated mail.message is
  15. immediately sent after being created, as if the scheduler
  16. was executed for this message only.
  17. :returns: id of the mail.message that was created
  18. """
  19. self.ensure_one()
  20. Mail = self.env['mail.mail']
  21. # TDE FIXME: should remove dfeault_type from context
  22. Attachment = self.env['ir.attachment']
  23. # create a mail_mail based on values, without attachments
  24. values = self.generate_email(res_id)
  25. values['recipient_ids'] = [(4, pid) for pid in values.get('partner_ids', list())]
  26. attachment_ids = values.pop('attachment_ids', [])
  27. attachments = values.pop('attachments', [])
  28. # add a protection against void email_from
  29. if 'email_from' in values and not values.get('email_from'):
  30. values.pop('email_from')
  31. mail = Mail.create(values)
  32. # manage attachments
  33. attachments.extend(additional_attachments)
  34. for attachment in attachments:
  35. attachment_data = {
  36. 'name': attachment[0],
  37. 'datas_fname': attachment[0],
  38. 'datas': attachment[1],
  39. 'res_model': 'mail.message',
  40. 'res_id': mail.mail_message_id.id,
  41. }
  42. attachment_ids.append(Attachment.create(attachment_data).id)
  43. if attachment_ids:
  44. values['attachment_ids'] = [(6, 0, attachment_ids)]
  45. mail.write({'attachment_ids': [(6, 0, attachment_ids)]})
  46. if force_send:
  47. mail.send(raise_exception=raise_exception)
  48. return mail.id # TDE CLEANME: return mail + api.returns ?