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.

103 lines
3.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import api, fields, models, _
  5. from openerp.exceptions import Warning as UserError
  6. class MailMassMailing(models.Model):
  7. _inherit = 'mail.mass_mailing'
  8. state = fields.Selection(selection_add=[
  9. ('sending', "Sending"),
  10. ])
  11. pending_count = fields.Integer(
  12. string="Pending", compute='_compute_pending_count')
  13. mass_mailing_sending_ids = fields.One2many(
  14. comodel_name='mail.mass_mailing.sending', readonly=True,
  15. inverse_name='mass_mailing_id', string="Sending tasks")
  16. @api.model
  17. def read_group(self, domain, fields, groupby, **kwargs):
  18. # Add 'sending' state group, even if no results.
  19. # This is needed for kanban view, columns are showed always
  20. res = super(MailMassMailing, self).read_group(
  21. domain, fields, groupby, **kwargs)
  22. if groupby and groupby[0] == "state":
  23. group_domain = domain + [('state', '=', 'sending')]
  24. count = self.search_count(group_domain)
  25. res.append({
  26. '__context': {'group_by': groupby[1:]},
  27. '__domain': group_domain,
  28. 'state': ('sending', _("Sending")),
  29. 'state_count': count,
  30. })
  31. return res
  32. def _sendings_get(self):
  33. self.ensure_one()
  34. return self.env['mail.mass_mailing.sending'].search([
  35. ('mass_mailing_id', '=', self.id),
  36. ('state', 'in', ('enqueued', 'sending')),
  37. ])
  38. @api.multi
  39. def send_mail(self):
  40. if not self.env.context.get('sending_queue_enabled', False):
  41. return super(MailMassMailing, self).send_mail()
  42. for mailing in self:
  43. m_sending = self.env['mail.mass_mailing.sending']
  44. sendings = mailing._sendings_get()
  45. if sendings:
  46. raise UserError(_(
  47. "There is another sending task running. "
  48. "Please, be patient. You can see all the sending tasks in "
  49. "'Sending tasks' tab"
  50. ))
  51. res_ids = mailing.get_recipients(mailing)
  52. batch_size = m_sending.batch_size_get()
  53. if not res_ids:
  54. raise UserError(_("Please select recipients."))
  55. sending = m_sending.create({
  56. 'state': 'draft',
  57. 'mass_mailing_id': mailing.id,
  58. })
  59. sending_state = 'enqueued'
  60. if len(res_ids) < (batch_size / 2):
  61. mailing.with_context(
  62. mass_mailing_sending_id=sending.id,
  63. sending_queue_enabled=False).send_mail()
  64. sending_state = 'sending'
  65. sending.state = sending_state
  66. mailing.write({
  67. 'sent_date': fields.Datetime.now(),
  68. 'state': 'sending',
  69. })
  70. return True
  71. @api.model
  72. def get_recipients(self, mailing):
  73. sending = False
  74. sending_id = self.env.context.get('mass_mailing_sending_id', False)
  75. if sending_id:
  76. sending = self.env['mail.mass_mailing.sending'].browse(sending_id)
  77. try:
  78. res_ids = super(MailMassMailing, self).get_recipients(mailing)
  79. except UserError as e:
  80. if sending:
  81. sending._send_error(e)
  82. else:
  83. raise
  84. return []
  85. if sending:
  86. res_ids = sending.get_recipient_batch(res_ids)
  87. return res_ids
  88. @api.multi
  89. def _compute_pending_count(self):
  90. for mailing in self:
  91. sendings = mailing._sendings_get()
  92. mailing.pending_count = (
  93. sum(sendings.mapped('pending_count')) +
  94. sum(sendings.mapped('sending_count')))