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.

91 lines
3.5 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. remaining = self
  30. for contact in self:
  31. _vals = vals.copy()
  32. # If removing partner, search again by email
  33. if _vals.get('partner_id') is False:
  34. _vals = contact._set_partner(_vals)
  35. _vals = contact._set_name_email(_vals)
  36. # If changed, write it separately
  37. if vals != _vals:
  38. remaining -= contact
  39. super(MailMassMailingContact, contact).write(_vals)
  40. # Normal batch write for contacts that need no customization
  41. return super(MailMassMailingContact, remaining).write(vals)
  42. def _prepare_partner(self, vals, mailing_list):
  43. vals = {
  44. 'name': vals.get('name') or vals.get('email'),
  45. 'email': vals.get('email', False),
  46. }
  47. if mailing_list.partner_category:
  48. vals['category_id'] = [(4, mailing_list.partner_category.id, 0)]
  49. return vals
  50. def _set_partner(self, vals):
  51. email = vals.get('email', self.email)
  52. if not email: # pragma: no cover
  53. return vals
  54. m_mailing = self.env['mail.mass_mailing.list']
  55. m_partner = self.env['res.partner']
  56. list_id = vals.get('list_id', self.list_id.id)
  57. mailing_list = m_mailing.browse(list_id, prefetch=self._prefetch)
  58. # Look for a partner with that email
  59. email = email.strip()
  60. partner = m_partner.search([('email', '=ilike', email)], limit=1)
  61. # Different partner found
  62. if partner and partner != self.partner_id:
  63. vals['partner_id'] = partner.id
  64. elif mailing_list.partner_mandatory:
  65. # Create partner
  66. partner = m_partner.sudo().create(
  67. self._prepare_partner(vals, mailing_list))
  68. vals['partner_id'] = partner.id
  69. return vals
  70. def _set_name_email(self, vals):
  71. partner_id = vals.get('partner_id', self.partner_id.id)
  72. if not partner_id:
  73. return vals
  74. partner = self.env['res.partner'].browse(
  75. partner_id, prefetch=self._prefetch)
  76. if vals.get("email", self.email) != partner.email:
  77. vals['email'] = partner.email
  78. if vals.get("name", self.name) != partner.name:
  79. vals['name'] = partner.name
  80. return vals