def handle(self, *args, **options): YESTERDAY = datetime.timedelta(hours=24) # If it's been less than 24 hours since our last big email, # stop here and don't send any emails. if ((self.this_run_covers_things_up_until - self.this_run_covers_things_since) < YESTERDAY): logger.warning( "Not sending emails; emails were last sent within the last " "24 hours.") return # If we made it this far in the function, it's been 24 hours or more # since our last email. Let's make a note that the time range of this # email extends until the timestamp stored in # self.this_run_covers_things_up_until Timestamp.update_timestamp_for_string( Command.TIMESTAMP_KEY, override_time=self.this_run_covers_things_up_until) # ^^ This entry in the database will be used for calculating the time # range starting point of the next email # Now let's send some emails! :-) people_who_want_email = Person.objects.filter( email_me_re_projects=True) count = 0 for person in people_who_want_email: if not person.user.email: logger.warning("Uh, the person has no email address: %s" % person.user.username) continue # if the user has no email address, we skip the user. message_in_plain_text, message_in_html = self.get_projects_email_for( person) if message_in_html: count += 1 # TODO: fix the logging message logger.info("Emailing %s their project activity." % person.user.email) email = EmailMultiAlternatives( subject="News about your OpenHatch projects", body=message_in_plain_text, from_email= "\"OpenHatch Mail-Bot\" <*****@*****.**>", headers={ 'X-Notion': "I'm feeling a lot less evil now.", }, to=[person.user.email]) email.attach_alternative(message_in_html, "text/html") email.send() logger.info("Emailed", count)
def handle(self, *args, **options): YESTERDAY = datetime.timedelta(hours=24) # If it's been less than 24 hours since our last big email, # stop here and don't send any emails. if ((self.this_run_covers_things_up_until - self.this_run_covers_things_since) < YESTERDAY): logger.warning( "Not sending emails; emails were last sent within the last " "24 hours.") return # If we made it this far in the function, it's been 24 hours or more # since our last email. Let's make a note that the time range of this # email extends until the timestamp stored in # self.this_run_covers_things_up_until Timestamp.update_timestamp_for_string(Command.TIMESTAMP_KEY, override_time=self.this_run_covers_things_up_until) # ^^ This entry in the database will be used for calculating the time # range starting point of the next email # Now let's send some emails! :-) people_who_want_email = Person.objects.filter( email_me_re_projects=True) count = 0 for person in people_who_want_email: if not person.user.email: logger.warning("Uh, the person has no email address: %s" % person.user.username) continue # if the user has no email address, we skip the user. message_in_plain_text, message_in_html = self.get_projects_email_for( person) if message_in_html: count += 1 # TODO: fix the logging message logger.info("Emailing %s their project activity." % person.user.email) email = EmailMultiAlternatives( subject="News about your OpenHatch projects", body=message_in_plain_text, from_email="\"OpenHatch Mail-Bot\" <*****@*****.**>", headers={ 'X-Notion': "I'm feeling a lot less evil now.", }, to=[person.user.email]) email.attach_alternative(message_in_html, "text/html") email.send() logger.debug("Emailed %d", count)
def test_snapshot_timestamp(self): # data capture, woo fake_stdout = StringIO() # Create local constants that refer to values we will insert and check TIMESTAMP_KEY_TO_USE = 'birthday of Asheesh with arbitrary time' TIMESTAMP_DATE_TO_USE = datetime.datetime(1985, 10, 20, 3, 21, 20) # make fake Timestamp t = Timestamp() t.key = TIMESTAMP_KEY_TO_USE t.timestamp = TIMESTAMP_DATE_TO_USE t.save() # snapshot fake timestamp into fake stdout command = mysite.customs.management.commands.snapshot_public_data.Command( ) command.handle(output=fake_stdout) # now, delete the timestamp... t.delete() # let's see if we can re-import the timestamp for obj in django.core.serializers.deserialize('json', fake_stdout.getvalue()): obj.save() # testing to see if there are ANY self.assertTrue(Timestamp.objects.all()) # testing to see if ours is there reincarnated_t = mysite.base.models.Timestamp.objects.get( key=TIMESTAMP_KEY_TO_USE) self.assertEquals(reincarnated_t.timestamp, TIMESTAMP_DATE_TO_USE)
def send_weekly_exit_code(covers_things_since=None): if covers_things_since is None: covers_things_since = Timestamp.get_timestamp_for_string("What's the endpoint of the time range of the last email we sent out?") covers_things_until = datetime.datetime.utcnow() EIGHT_DAYS = datetime.timedelta(days=8) if ((covers_things_until - EIGHT_DAYS) < covers_things_since): print "OK - Last email sent less than 8 days ago" return 0 else: print "CRITICAL - Last email sent more than 8 days ago" return 2
def send_weekly_exit_code(covers_things_since=None): if covers_things_since is None: covers_things_since = Timestamp.get_timestamp_for_string( "What's the endpoint of the time range of the last email we sent out?") covers_things_until = datetime.datetime.utcnow() EIGHT_DAYS = datetime.timedelta(days=8) if ((covers_things_until - EIGHT_DAYS) < covers_things_since): logger.info("OK - Last email sent less than 8 days ago") return 0 else: logger.warn("CRITICAL - Last email sent more than 8 days ago") return 2
def get_time_range_endpoint_of_last_email(): return Timestamp.get_timestamp_for_string(Command.TIMESTAMP_KEY)