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.

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