def tryTestUser(): with closing(get_connection()) as cx: msg = EmailMultiAlternatives(settings.FORUM_DIGEST_EMAIL_SUBJECT, "test Edx Sender", settings.FORUM_DIGEST_EMAIL_SENDER, ["*****@*****.**"]) html = "<p>TEST</p>" msgs = [] msg.attach_alternative(html, "text/html") msgs.append(msg) cx.send_messages(msgs)
def generate_and_send_digests(users, from_dt, to_dt): """ This task generates and sends forum digest emails to multiple users in a single background operation. `users` is an iterable of dictionaries, as returned by the edx user_api (required keys are "id", "name", "username", and "email"). `from_dt` and `to_dt` are datetime objects representing the start and end of the time window for which to generate a digest. """ users_by_id = dict((str(u['id']), u) for u in users) with closing(get_connection()) as cx: msgs = [] for user_id, digest in generate_digest_content(users_by_id.keys(), from_dt, to_dt): user = users_by_id[user_id] # format the digest text, html = render_digest( user, digest, settings.FORUM_DIGEST_EMAIL_TITLE, settings.FORUM_DIGEST_EMAIL_DESCRIPTION) # send the message through our mailer msg = EmailMultiAlternatives( settings.FORUM_DIGEST_EMAIL_SUBJECT, text, '@'.join( [settings.FORUM_DIGEST_EMAIL_SENDER, settings.EMAIL_DOMAIN]), [user['email']] ) msg.attach_alternative(html, "text/html") msgs.append(msg) if not msgs: return try: cx.send_messages(msgs) except SESMaxSendingRateExceededError as e: # we've tripped the per-second send rate limit. we generally # rely on the django_ses auto throttle to prevent this, # but in case we creep over, we can re-queue and re-try this task # - if and only if none of the messages in our batch were # sent yet. # this implementation is also non-ideal in that the data will be # fetched from the comments service again in the event of a retry. if not any((getattr(msg, 'extra_headers', {}).get('status') == 200 for msg in msgs)): raise generate_and_send_digests.retry(exc=e) else: # raise right away, since we don't support partial retry raise
def generate_and_send_digests(users, from_dt, to_dt): """ This task generates and sends forum digest emails to multiple users in a single background operation. `users` is an iterable of dictionaries, as returned by the edx user_api (required keys are "id", "name", "username", and "email"). `from_dt` and `to_dt` are datetime objects representing the start and end of the time window for which to generate a digest. """ users_by_id = dict((str(u['id']), u) for u in users) with closing(get_connection()) as cx: msgs = [] for user_id, digest in generate_digest_content(users_by_id.keys(), from_dt, to_dt): user = users_by_id[user_id] # format the digest text, html = render_digest(user, digest, settings.FORUM_DIGEST_EMAIL_TITLE, settings.FORUM_DIGEST_EMAIL_DESCRIPTION) # send the message through our mailer msg = EmailMultiAlternatives(settings.FORUM_DIGEST_EMAIL_SUBJECT, text, settings.FORUM_DIGEST_EMAIL_SENDER, [user['email']]) msg.attach_alternative(html, "text/html") msgs.append(msg) if not msgs: return try: cx.send_messages(msgs) except SESMaxSendingRateExceededError as e: # we've tripped the per-second send rate limit. we generally # rely on the django_ses auto throttle to prevent this, # but in case we creep over, we can re-queue and re-try this task # - if and only if none of the messages in our batch were # sent yet. # this implementation is also non-ideal in that the data will be # fetched from the comments service again in the event of a retry. if not any((getattr(msg, 'extra_headers', {}).get('status') == 200 for msg in msgs)): raise generate_and_send_digests.retry(exc=e) else: # raise right away, since we don't support partial retry raise
def generate_and_send_digests(users, from_dt, to_dt): """ This task generates and sends forum digest emails to multiple users in a single background operation. `users` is an iterable of dictionaries, as returned by the edx user_api (required keys are "id", "name", "email", "preferences", and "course_info"). `from_dt` and `to_dt` are datetime objects representing the start and end of the time window for which to generate a digest. """ logger.info("DIGEST TASK UPLOAD") users_by_id = dict((str(u['id']), u) for u in users) msgs = [] try: with closing(get_connection()) as cx: for user_id, digest in generate_digest_content( users_by_id, from_dt, to_dt): user = users_by_id[user_id] # format the digest text, html = render_digest( user, digest, settings.FORUM_DIGEST_EMAIL_TITLE, settings.FORUM_DIGEST_EMAIL_DESCRIPTION) # send the message through our mailer msg = EmailMultiAlternatives( settings.FORUM_DIGEST_EMAIL_SUBJECT, text, settings.FORUM_DIGEST_EMAIL_SENDER, [user['email']]) msg.attach_alternative(html, "text/html") msgs.append(msg) if msgs: cx.send_messages(msgs) if settings.DEAD_MANS_SNITCH_URL: requests.post(settings.DEAD_MANS_SNITCH_URL) except (CommentsServiceException, SESMaxSendingRateExceededError) as e: # only retry if no messages were successfully sent yet. if not any((getattr(msg, 'extra_headers', {}).get('status') == 200 for msg in msgs)): raise generate_and_send_digests.retry(exc=e) else: # raise right away, since we don't support partial retry raise
def generate_and_send_digests(users, from_dt, to_dt, language=None): """ This task generates and sends forum digest emails to multiple users in a single background operation. `users` is an iterable of dictionaries, as returned by the edx user_api (required keys are "id", "name", "email", "preferences", and "course_info"). `from_dt` and `to_dt` are datetime objects representing the start and end of the time window for which to generate a digest. """ settings.LANGUAGE_CODE = language or settings.LANGUAGE_CODE or DEFAULT_LANGUAGE users_by_id = dict((str(u['id']), u) for u in users) msgs = [] try: with closing(get_connection()) as cx: for user_id, digest in generate_digest_content(users_by_id, from_dt, to_dt): user = users_by_id[user_id] # format the digest text, html = render_digest( user, digest, settings.FORUM_DIGEST_EMAIL_TITLE, settings.FORUM_DIGEST_EMAIL_DESCRIPTION) # send the message through our mailer msg = EmailMultiAlternatives( settings.FORUM_DIGEST_EMAIL_SUBJECT, text, settings.FORUM_DIGEST_EMAIL_SENDER, [user['email']] ) msg.attach_alternative(html, "text/html") msgs.append(msg) if msgs: cx.send_messages(msgs) if settings.DEAD_MANS_SNITCH_URL: requests.post(settings.DEAD_MANS_SNITCH_URL) except (CommentsServiceException, SESMaxSendingRateExceededError) as e: # only retry if no messages were successfully sent yet. if not any((getattr(msg, 'extra_headers', {}).get('status') == 200 for msg in msgs)): raise generate_and_send_digests.retry(exc=e) else: # raise right away, since we don't support partial retry raise