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.

50 lines
2.3 KiB

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