|
7 | 7 | from celery import chord |
8 | 8 | from django.utils import timezone |
9 | 9 | from osf.models.notification_campaign import NotificationCampaign, NotificationCampaignRecipient, NotificationCampaignStatus, NotificationCampaignRecipientStatus |
| 10 | +from osf.email import send_email_with_send_grid |
10 | 11 |
|
11 | 12 | logger = logging.getLogger(__name__) |
12 | 13 |
|
@@ -180,39 +181,43 @@ def send_campaign_batch(context, recipients_ids, notification_type_name='blank', |
180 | 181 | } |
181 | 182 | success_count = 0 |
182 | 183 | 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) |
183 | 191 |
|
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 | + ) |
202 | 207 |
|
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 |
207 | 212 |
|
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) |
213 | 218 |
|
214 | | - failure_count += 1 |
215 | | - pass |
| 219 | + failure_count += 1 |
| 220 | + pass |
216 | 221 |
|
217 | 222 | NotificationCampaignRecipient.objects.bulk_create(recipient_records['to_create']) |
218 | 223 | NotificationCampaignRecipient.objects.bulk_update(recipient_records['to_update'], ['status', 'error_message']) |
|
0 commit comments