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.

35 lines
1.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Camptocamp
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from odoo import models, api
  5. class MailTemplate(models.Model):
  6. _inherit = 'mail.template'
  7. @api.multi
  8. def generate_recipients(self, results, res_ids):
  9. """
  10. Modify the code that set the default recipient in an email to the first
  11. partner having an email set following the parents tree.
  12. If no parent with an email is found,
  13. it reverts to the popup asking to set an email for the current partner
  14. """
  15. results = super(MailTemplate, self).generate_recipients(
  16. results,
  17. res_ids
  18. )
  19. for res_id, values in results.iteritems():
  20. partner_ids = values.get('partner_ids', [])
  21. partners_with_emails = []
  22. for partner in self.env['res.partner'].sudo().browse(partner_ids):
  23. current = partner
  24. while current:
  25. if current.email:
  26. break
  27. current = current.parent_id
  28. partners_with_emails.append(current.id or partner.id)
  29. results[res_id]['partner_ids'] = partners_with_emails
  30. return results