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.

40 lines
1.8 KiB

  1. # Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  2. # Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # Copyright 2015 Javier Iniesta <javieria@antiun.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import UserError
  7. class PartnerMailListWizard(models.TransientModel):
  8. _name = "partner.mail.list.wizard"
  9. _description = "Create contact mailing list"
  10. mail_list_id = fields.Many2one(comodel_name="mail.mass_mailing.list",
  11. string="Mailing List")
  12. partner_ids = fields.Many2many(
  13. comodel_name="res.partner", relation="mail_list_wizard_partner",
  14. default=lambda self: self.env.context.get("active_ids"))
  15. @api.multi
  16. def add_to_mail_list(self):
  17. contact_obj = self.env['mail.mass_mailing.contact']
  18. for partner in self.partner_ids:
  19. if not partner.email:
  20. raise UserError(_("Partner '%s' has no email.") % partner.name)
  21. contact = contact_obj.search([('partner_id', '=', partner.id)])
  22. if self.mail_list_id not in contact.mapped('list_ids'):
  23. contact_vals = {
  24. 'partner_id': partner.id,
  25. 'list_ids': [[6, 0, [self.mail_list_id.id]]]
  26. }
  27. if partner.title:
  28. contact_vals['title_id'] = partner.title.id
  29. if partner.company_id:
  30. contact_vals['company_name'] = partner.company_id.name
  31. if partner.country_id:
  32. contact_vals['country_id'] = partner.country_id.id
  33. if partner.category_id:
  34. contact_vals['tag_ids'] = partner.category_id.ids
  35. contact_obj.create(contact_vals)