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.

58 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This file is part of mail_attach_existing_attachment,
  5. # an Odoo module.
  6. #
  7. # Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
  8. #
  9. # mail_attach_existing_attachment is free software:
  10. # you can redistribute it and/or modify it under the terms of the GNU
  11. # Affero General Public License as published by the Free Software
  12. # Foundation,either version 3 of the License, or (at your option) any
  13. # later version.
  14. #
  15. # mail_attach_existing_attachment is distributed
  16. # in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  17. # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  18. # PURPOSE. See the GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with mail_attach_existing_attachment.
  22. # If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ##############################################################################
  25. from odoo import models, fields, api
  26. class MailComposeMessage(models.TransientModel):
  27. _inherit = 'mail.compose.message'
  28. @api.model
  29. def default_get(self, fields_list):
  30. res = super(MailComposeMessage, self).default_get(fields_list)
  31. if res.get('res_id') and res.get('model') and \
  32. res.get('composition_mode', '') != 'mass_mail' and\
  33. not res.get('can_attach_attachment'):
  34. res['can_attach_attachment'] = True # pragma: no cover
  35. return res
  36. @api.model
  37. def _get_object_attachment_domain(self):
  38. return "[('res_model', '=', model), ('res_id', '=', res_id)]"
  39. can_attach_attachment = fields.Boolean(string='Can Attach Attachment')
  40. object_attachment_ids = fields.Many2many(
  41. comodel_name='ir.attachment',
  42. relation='mail_compose_message_ir_attachments_object_rel',
  43. column1='wizard_id', column2='attachment_id', string='Attachments',
  44. domain=lambda self: self._get_object_attachment_domain())
  45. @api.multi
  46. def get_mail_values(self, res_ids):
  47. res = super(MailComposeMessage, self).get_mail_values(res_ids)
  48. if self.object_attachment_ids.ids and self.model and len(res_ids) == 1:
  49. res[res_ids[0]].setdefault('attachment_ids', []).extend(
  50. self.object_attachment_ids.ids)
  51. return res