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.

82 lines
2.9 KiB

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