mail_thread.py 1.5 KB

12345678910111213141516171819202122232425262728293031
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import models
  4. class MailThread(models.AbstractModel):
  5. _inherit = 'mail.thread'
  6. def _message_post_process_attachments(self, attachments, attachment_ids, message_values):
  7. """ This method extension ensures that, when using the "Send & Print" feature, if the user
  8. adds an attachment, the latter will be linked to the record.
  9. # Task-2792146: will move to model-based method
  10. """
  11. record = self.env.context.get('attached_to')
  12. # link mail.compose.message attachments to attached_to
  13. if record and record._name == 'account.move':
  14. message_values['model'] = record._name
  15. message_values['res_id'] = record.id
  16. res = super()._message_post_process_attachments(attachments, attachment_ids, message_values)
  17. # link account.invoice.send attachments to attached_to
  18. model = message_values['model']
  19. res_id = message_values['res_id']
  20. att_ids = [att[1] for att in res.get('attachment_ids') or []]
  21. if att_ids and model == 'account.move':
  22. filtered_attachment_ids = self.env['ir.attachment'].sudo().browse(att_ids).filtered(
  23. lambda a: a.res_model in ('account.invoice.send',) and a.create_uid.id == self._uid)
  24. if filtered_attachment_ids:
  25. filtered_attachment_ids.write({'res_model': model, 'res_id': res_id})
  26. return res