| 1 |
from odoo import _, api, exceptions, fields, models |
| 2 |
from odoo.fields import Domain |
| 3 |
|
| 4 |
|
| 5 |
class MailingMailing(models.Model): |
| 6 |
_inherit = 'mailing.mailing' |
| 7 |
|
| 8 |
mailing_model_id = fields.Many2one(compute="_compute_mailing_model_id", store=True, readonly=False) |
| 9 |
card_requires_sync_count = fields.Integer(compute="_compute_card_requires_sync_count") |
| 10 |
card_campaign_id = fields.Many2one('card.campaign', index='btree_not_null') |
| 11 |
|
| 12 |
@api.constrains('card_campaign_id', 'mailing_domain', 'mailing_model_id') |
| 13 |
def _check_mailing_domain(self): |
| 14 |
for mailing in self: |
| 15 |
if mailing.card_campaign_id: |
| 16 |
if mailing.sudo().mailing_model_id.model != mailing.card_campaign_id.res_model: |
| 17 |
raise exceptions.ValidationError(_( |
| 18 |
"Card Campaign Mailing should target model %(model_name)s", |
| 19 |
model_name=self.env['ir.model']._get(mailing.card_campaign_id.res_model).display_name |
| 20 |
)) |
| 21 |
|
| 22 |
@api.depends('card_campaign_id') |
| 23 |
def _compute_mailing_model_id(self): |
| 24 |
for mailing in self.filtered('card_campaign_id'): |
| 25 |
mailing.mailing_model_id = self.env['ir.model']._get_id(mailing.card_campaign_id.res_model) |
| 26 |
|
| 27 |
@api.depends('card_campaign_id') |
| 28 |
def _compute_card_requires_sync_count(self): |
| 29 |
"""Check if there's any missing or outdated card.""" |
| 30 |
self.card_requires_sync_count = 0 |
| 31 |
# no point in updating sent mailings |
| 32 |
card_mailings = self.filtered(lambda mailing: mailing.card_campaign_id and mailing.state == 'draft') |
| 33 |
for mailing in card_mailings: |
| 34 |
recipients = self.env[mailing.mailing_model_real].search(self._parse_mailing_domain()) |
| 35 |
out_of_date_count = self.env['card.card'].search_count([ |
| 36 |
('campaign_id', '=', mailing.card_campaign_id.id), |
| 37 |
('res_id', 'in', recipients.ids), |
| 38 |
('requires_sync', '=', False) |
| 39 |
]) |
| 40 |
mailing.card_requires_sync_count = len(recipients) - out_of_date_count |
| 41 |
|
| 42 |
def action_put_in_queue(self): |
| 43 |
"""Detect mismatches before scheduling.""" |
| 44 |
for mailing in self.filtered('card_campaign_id'): |
| 45 |
if mailing.card_requires_sync_count: |
| 46 |
raise exceptions.UserError(_( |
| 47 |
'You should update all the cards for %(mailing)s before scheduling a mailing.', |
| 48 |
mailing=mailing.display_name |
| 49 |
)) |
| 50 |
super().action_put_in_queue() |
| 51 |
|
| 52 |
def action_send_mail(self, res_ids=None): |
| 53 |
for mailing in self.filtered('card_campaign_id'): |
| 54 |
if mailing.card_requires_sync_count: |
| 55 |
raise exceptions.UserError(_( |
| 56 |
'You should update all the cards for %(mailing)s before scheduling a mailing.', |
| 57 |
mailing=mailing.display_name |
| 58 |
)) |
| 59 |
return super().action_send_mail(res_ids) |
| 60 |
|
| 61 |
def action_update_cards(self): |
| 62 |
"""Update the cards in batches, commiting after each batch.""" |
| 63 |
for campaign in self.filtered(lambda mailing: mailing.state == 'draft').card_campaign_id: |
| 64 |
campaign._update_cards(self._parse_mailing_domain(), auto_commit=True) |
| 65 |
return { |
| 66 |
'type': 'ir.actions.act_window', |
| 67 |
'res_model': 'mailing.mailing', |
| 68 |
'res_id': self[0].id, |
| 69 |
'view_mode': 'form', |
| 70 |
'target': 'current', |
| 71 |
} |
| 72 |
|
| 73 |
def _get_recipients_domain(self): |
| 74 |
"""Domain with an additional condition that the card must exist for the records.""" |
| 75 |
domain = Domain(super()._get_recipients_domain()) |
| 76 |
if self.card_campaign_id: |
| 77 |
res_ids = self.env['card.card'].search_fetch([('campaign_id', '=', self.card_campaign_id.id)], ['res_id']).mapped('res_id') |
| 78 |
domain &= Domain('id', 'in', res_ids) |
| 79 |
return domain |