Skip to content

Commit d1048ec

Browse files
[ENG-11698] Add bulk email sending with SendGrid to send_campaign_batch (#11806)
1 parent aae133d commit d1048ec

1 file changed

Lines changed: 34 additions & 29 deletions

File tree

osf/email/notification_campaign.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from celery import chord
88
from django.utils import timezone
99
from osf.models.notification_campaign import NotificationCampaign, NotificationCampaignRecipient, NotificationCampaignStatus, NotificationCampaignRecipientStatus
10+
from osf.email import send_email_with_send_grid
1011

1112
logger = logging.getLogger(__name__)
1213

@@ -180,39 +181,43 @@ def send_campaign_batch(context, recipients_ids, notification_type_name='blank',
180181
}
181182
success_count = 0
182183
failure_count = 0
184+
if campaign.metadata.get('sendgrid_bulk', False):
185+
recipient_emails = list(recipients_qs.values_list('username', flat=True))
186+
send_email_with_send_grid(to_addr=recipient_emails, notification_type=notification_type, context=context)
187+
success_count = len(recipient_emails)
188+
else:
189+
for recipient in recipients_qs:
190+
recipient_record = existing.get(recipient.id)
183191

184-
for recipient in recipients_qs:
185-
recipient_record = existing.get(recipient.id)
186-
187-
if recipient_record is None:
188-
recipient_record = NotificationCampaignRecipient(
189-
campaign_id=campaign_id,
190-
user=recipient,
191-
)
192-
operation = 'to_create'
193-
else:
194-
operation = 'to_update'
195-
196-
try:
197-
notification_type.emit(
198-
user=recipient,
199-
event_context=context,
200-
save=False, # Too many write operations
201-
)
192+
if recipient_record is None:
193+
recipient_record = NotificationCampaignRecipient(
194+
campaign_id=campaign_id,
195+
user=recipient,
196+
)
197+
operation = 'to_create'
198+
else:
199+
operation = 'to_update'
200+
201+
try:
202+
notification_type.emit(
203+
user=recipient,
204+
event_context=context,
205+
save=False, # Too many write operations
206+
)
202207

203-
recipient_record.status = NotificationCampaignRecipientStatus.SENT
204-
recipient_record.error_message = None
205-
recipient_records[operation].append(recipient_record)
206-
success_count += 1
208+
recipient_record.status = NotificationCampaignRecipientStatus.SENT
209+
recipient_record.error_message = None
210+
recipient_records[operation].append(recipient_record)
211+
success_count += 1
207212

208-
except Exception as exc:
209-
logger.error(exc) # TODO update error
210-
recipient_record.status = NotificationCampaignRecipientStatus.FAILED
211-
recipient_record.error_message = str(exc)
212-
recipient_records[operation].append(recipient_record)
213+
except Exception as exc:
214+
logger.error(exc) # TODO update error
215+
recipient_record.status = NotificationCampaignRecipientStatus.FAILED
216+
recipient_record.error_message = str(exc)
217+
recipient_records[operation].append(recipient_record)
213218

214-
failure_count += 1
215-
pass
219+
failure_count += 1
220+
pass
216221

217222
NotificationCampaignRecipient.objects.bulk_create(recipient_records['to_create'])
218223
NotificationCampaignRecipient.objects.bulk_update(recipient_records['to_update'], ['status', 'error_message'])

0 commit comments

Comments
 (0)