mailing_contact_to_list.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, _
  4. class MailingContactToList(models.TransientModel):
  5. _name = "mailing.contact.to.list"
  6. _description = "Add Contacts to Mailing List"
  7. contact_ids = fields.Many2many('mailing.contact', string='Contacts')
  8. mailing_list_id = fields.Many2one('mailing.list', string='Mailing List', required=True)
  9. def action_add_contacts(self):
  10. """ Simply add contacts to the mailing list and close wizard. """
  11. return self._add_contacts_to_mailing_list({'type': 'ir.actions.act_window_close'})
  12. def action_add_contacts_and_send_mailing(self):
  13. """ Add contacts to the mailing list and redirect to a new mailing on
  14. this list. """
  15. self.ensure_one()
  16. action = self.env["ir.actions.actions"]._for_xml_id("mass_mailing.mailing_mailing_action_mail")
  17. action['views'] = [[False, "form"]]
  18. action['target'] = 'current'
  19. action['context'] = {
  20. 'default_contact_list_ids': [self.mailing_list_id.id]
  21. }
  22. return self._add_contacts_to_mailing_list(action)
  23. def _add_contacts_to_mailing_list(self, action):
  24. self.ensure_one()
  25. previous_count = len(self.mailing_list_id.contact_ids)
  26. self.mailing_list_id.write({
  27. 'contact_ids': [
  28. (4, contact.id)
  29. for contact in self.contact_ids
  30. if contact not in self.mailing_list_id.contact_ids]
  31. })
  32. return {
  33. 'type': 'ir.actions.client',
  34. 'tag': 'display_notification',
  35. 'params': {
  36. 'type': 'info',
  37. 'message': _("%s Mailing Contacts have been added. ",
  38. len(self.mailing_list_id.contact_ids) - previous_count
  39. ),
  40. 'sticky': False,
  41. 'next': action,
  42. }
  43. }