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.

81 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  3. # Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  4. # Copyright 2015 Javier Iniesta <javieria@antiun.com>
  5. # Copyright 2017 David Vidal <david.vidal@tecnativa.com>
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. from odoo import _, api, fields, models
  8. class MailMassMailingContact(models.Model):
  9. _inherit = 'mail.mass_mailing.contact'
  10. partner_id = fields.Many2one(comodel_name='res.partner', string="Partner",
  11. domain=[('email', '!=', False)])
  12. _sql_constraints = [
  13. ('partner_list_uniq', 'unique(partner_id, list_id)',
  14. _('Partner already exists in this mailing list.'))
  15. ]
  16. @api.onchange('partner_id')
  17. def _onchange_partner(self):
  18. if self.partner_id:
  19. self.name = self.partner_id.name
  20. self.email = self.partner_id.email
  21. @api.model
  22. @api.returns('self', lambda x: x.id)
  23. def create(self, vals):
  24. if not vals.get('partner_id'):
  25. vals = self._set_partner(vals)
  26. vals = self._set_name_email(vals)
  27. return super(MailMassMailingContact, self).create(vals)
  28. def write(self, vals):
  29. for contact in self:
  30. if vals.get('partner_id', None) is False:
  31. # If removing partner, search again by email
  32. vals = contact._set_partner(vals)
  33. vals = contact._set_name_email(vals)
  34. return super(MailMassMailingContact, self).write(vals)
  35. def _prepare_partner(self, vals, mailing_list):
  36. vals = {
  37. 'name': vals.get('name') or vals.get('email'),
  38. 'email': vals.get('email', False),
  39. }
  40. if mailing_list.partner_category:
  41. vals['category_id'] = [(4, mailing_list.partner_category.id, 0)]
  42. return vals
  43. def _set_partner(self, vals):
  44. email = vals.get('email', self.email)
  45. if not email: # pragma: no cover
  46. return vals
  47. m_mailing = self.env['mail.mass_mailing.list']
  48. m_partner = self.env['res.partner']
  49. list_id = vals.get('list_id', self.list_id.id)
  50. mailing_list = m_mailing.browse(list_id)
  51. # Look for a partner with that email
  52. email = email.strip()
  53. partners = m_partner.search([('email', '=ilike', email)], limit=1)
  54. if partners:
  55. # Partner found
  56. vals['partner_id'] = partners[0].id
  57. elif mailing_list.partner_mandatory:
  58. # Create partner
  59. partner = m_partner.sudo().create(
  60. self._prepare_partner(vals, mailing_list))
  61. vals['partner_id'] = partner.id
  62. return vals
  63. def _set_name_email(self, vals):
  64. partner_id = vals.get('partner_id', self.partner_id.id)
  65. if not partner_id:
  66. return vals
  67. partner = self.env['res.partner'].browse(partner_id)
  68. vals['email'] = partner.email
  69. vals['name'] = partner.name
  70. return vals