def email_healthchecks(results): has_high = any([result.severity == SEVERITY_HIGH for result in results]) # The subject should indicate very very obviously whether the sky is # falling or not. subject = '[hb health] %s (%s)' % ( ('RED ALERT' if has_high else 'fine'), datetime.now().strftime('%Y-%m-%d %H:%M') ) # We do the entire email body in HTML because some output will want to # preserve whitespace and use a fixed-width font. Further, this lets # us make it super easy to spot SEVERITY_HIGH situations. html_body = render_to_string('heartbeat/email/heartbeat_health.html', { 'severity_name': SEVERITY, 'results': results }) recipients = get_recipients(MAILINGLIST) if recipients: send_mail( subject=subject, message='This email is in HTML.', from_email=settings.SERVER_EMAIL, recipient_list=recipients, html_message=html_body ) else: # FIXME: log this? is that a good idea? log.info('No recipients for "%s"\n%s\n%s' % ( MAILINGLIST, subject, html_body))
def hb_healthcheck(request): """View for viewing healthchecks and kicking off a healthcheck email""" ml_recipients = get_recipients(MAILINGLIST) results = run_healthchecks() # If they did a POST, it means they want to email the results to the # mailing list. if request.method == 'POST': email_healthchecks(results) return render(request, 'analytics/analyzer/hb_healthcheck.html', { 'results': results, 'MAILINGLIST': MAILINGLIST, 'ml_recipients': ml_recipients, 'severity_name': SEVERITY })
def hb_healthcheck(request): """View for viewing healthchecks and kicking off a healthcheck email""" ml_recipients = get_recipients(MAILINGLIST) results = run_healthchecks() # If they did a POST, it means they want to email the results to the # mailing list. if request.method == 'POST': email_healthchecks(results) return render( request, 'analytics/analyzer/hb_healthcheck.html', { 'results': results, 'MAILINGLIST': MAILINGLIST, 'ml_recipients': ml_recipients, 'severity_name': SEVERITY })
def balance_good_to_continue(self, balance, threshold): """Checks whether balance is good to continue If it's not, this sends some mail and returns False. We check against a threshold that's high enough that we're pretty sure the next job we create will not exceed the credits in the account. Pretty sure if we exceed the credits in the account, it'll return a non-ok opstat and that'll throw an exception and everything will be ok data-consistency-wise. """ # FIXME: This should email a different group than admin, # but I'm (ab)using the admin group for now because I know # they're set up right. if balance < threshold: recipients = get_recipients('gengo_balance') subject = 'Gengo account balance {0} < {1}'.format( balance, threshold) body = wrap_with_paragraphs(dedent("""\ Dagnabit! Send more money or the translations get it! Don't try no funny business, neither! Love, Fjord McGengo """)) if recipients: send_mail( subject=subject, message=body, recipient_list=recipients, from_email=settings.SERVER_EMAIL ) else: log.info('No recipients for "%s"\n%s\n%s' % ( 'gengo_balance', subject, body)) return False return True
def run_daily_activities(self): # If gengosystem is disabled, we don't want to do anything. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD if threshold < balance < (2 * threshold): recipients = get_recipients('gengo_balance') subject = 'Warning: Gengo account balance {0} < {1}'.format( balance, 2 * threshold) body = wrap_with_paragraphs(dedent("""\ Dear mom, Translations are the fab. Running low on funds. Send more money when you get a chance. Love, Fjord McGengo """)) if recipients: send_mail( subject=subject, message=body, recipient_list=recipients, from_email=settings.SERVER_EMAIL ) else: log.info('No recipients for "%s"\n%s\n%s' % ( 'gengo_balance', subject, body))
def balance_good_to_continue(self, balance, threshold): """Checks whether balance is good to continue If it's not, this sends some mail and returns False. We check against a threshold that's high enough that we're pretty sure the next job we create will not exceed the credits in the account. Pretty sure if we exceed the credits in the account, it'll return a non-ok opstat and that'll throw an exception and everything will be ok data-consistency-wise. """ # FIXME: This should email a different group than admin, # but I'm (ab)using the admin group for now because I know # they're set up right. if balance < threshold: recipients = get_recipients('gengo_balance') subject = 'Gengo account balance {0} < {1}'.format( balance, threshold) body = wrap_with_paragraphs( dedent("""\ Dagnabit! Send more money or the translations get it! Don't try no funny business, neither! Love, Fjord McGengo """)) if recipients: send_mail(subject=subject, message=body, recipient_list=recipients, from_email=settings.SERVER_EMAIL) else: log.info('No recipients for "%s"\n%s\n%s' % ('gengo_balance', subject, body)) return False return True
def run_daily_activities(self): # If gengosystem is disabled, we don't want to do anything. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD if threshold < balance < (2 * threshold): recipients = get_recipients('gengo_balance') subject = 'Warning: Gengo account balance {0} < {1}'.format( balance, 2 * threshold) body = wrap_with_paragraphs( dedent("""\ Dear mom, Translations are the fab. Running low on funds. Send more money when you get a chance. Love, Fjord McGengo """)) if recipients: send_mail(subject=subject, message=body, recipient_list=recipients, from_email=settings.SERVER_EMAIL) else: log.info('No recipients for "%s"\n%s\n%s' % ('gengo_balance', subject, body))
def test_doesnt_exist(self): """Getting recipients from a non-existent mailing list is an empty list """ assert get_recipients('foo') == []
def test_has_members(self): ml = MailingListFactory(members=u'[email protected]\[email protected]') assert ( get_recipients(ml.name) == ['*****@*****.**', '*****@*****.**'] )
def test_no_members(self): ml = MailingListFactory(members=u'') assert get_recipients(ml.name) == []