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.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, models
  3. class MailTemplate(models.Model):
  4. _inherit = "mail.template"
  5. @api.multi
  6. def send_mail_with_multiple_attachments(self, res_id,
  7. additional_attachments,
  8. force_send=False,
  9. raise_exception=False):
  10. """Generates a new mail message for the given template and record,
  11. and schedules it for delivery through the ``mail``
  12. module's scheduler.
  13. :param int res_id: id of the record to render the template with
  14. (model is taken from the template)
  15. :param bool force_send: if True, the generated mail.message is
  16. immediately sent after being created, as if the scheduler
  17. was executed for this message only.
  18. :returns: id of the mail.message that was created
  19. """
  20. self.ensure_one()
  21. Mail = self.env['mail.mail']
  22. Attachment = self.env['ir.attachment'] # TDE FIXME: should remove dfeault_type from context
  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 ?