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.

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