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

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