Skip to content

Commit

Permalink
Removed urls from emails.
Browse files Browse the repository at this point in the history
The emails kept getting discarded by App Engine, and after trying
different things it turns out that they just really don't like our links
to the page. So, removed those, and added some url parameters to the
/sendmail page to allow you to send a mail multiple times, and send an
email for an arbitrary date, with ?force=1&date=20xx-xx-xx
  • Loading branch information
Einar Egilsson committed Feb 12, 2016
1 parent afa6f16 commit dd0e2ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.2
1.2.3
14 changes: 12 additions & 2 deletions handlers/sendmail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import webapp2
import webapp2, datetime
from models.dailymail import DailyMail

class SendMailHandler(webapp2.RequestHandler):
def get(self):

DailyMail().send()
force = self.request.get('force', '0') == '1'
date = self.request.get('date', None)
if date:
try:
y,m,d = date.split('-')
date = datetime.datetime(int(y), int(m), int(d)).date()
except:
self.response.out.write('Invalid date, ignored')

DailyMail().send(False, force, date)
self.response.out.write('Ran daily mail at ' + str(datetime.datetime.now()))
38 changes: 18 additions & 20 deletions models/dailymail.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DailyMail():

def send(self, is_intro_email=False):
def send(self, is_intro_email=False, force_send=False, date=None):

try:
now = datetime.datetime.now()
Expand All @@ -22,25 +22,35 @@ def send(self, is_intro_email=False):
else:
current_time, id, name, offset = self.get_time_in_timezone(settings)

if current_time.hour != settings.email_hour:
if current_time.hour != settings.email_hour and not force_send:
logging.info('Current time for %s is %s, not sending email now, will send at %02d:00' % (name, current_time, settings.email_hour))
return


today = current_time.date()
if date and force_send:
today = date #Allow overriding this stuff
else:
today = current_time.date()

if self.check_if_intro_email_sent_today(today):
if self.check_if_intro_email_sent_today(today) and not force_send:
logging.info('Already sent the intro email today, skipping this email for now')
return

self.check_if_mail_already_sent(today)

#Check if we've already sent an email
slug = Slug.query(Slug.date == today).get()

if slug and not force_send:
msg = 'Tried to send another email on %s, already sent %s' % (date, existing_slug.slug)
log_error('Tried to send email again', msg)
raise Exception(msg)

slug_id = self.get_slug()
if not slug:
slug_id = self.get_slug()

slug = Slug(slug=slug_id, date=today)
slug = Slug(slug=slug_id, date=today)

slug.put()
slug.put()

subject = "It's %s, %s %s - How did your day go?" % (today.strftime('%A'), today.strftime("%b"), today.day)
app_id = app_identity.get_application_id()
Expand All @@ -58,8 +68,6 @@ def send(self, is_intro_email=False):
Just reply to this email with your entry.
OLD_POST
You can see your past entries here:
https://APP_ID.appspot.com/past
""".replace('APP_ID', app_id)

Expand All @@ -75,7 +83,6 @@ def send(self, is_intro_email=False):
<br>
<br>
OLD_POST
<a href="https://APP_ID.appspot.com/past">Past entries</a>
</body>
</html>
""".replace('APP_ID', app_id)
Expand Down Expand Up @@ -138,15 +145,6 @@ def get_slug(self):

return slug

def check_if_mail_already_sent(self, date):
#Check if we've already sent an email
existing_slug = Slug.query(Slug.date == date).get()

if existing_slug:
msg = 'Tried to send another email on %s, already sent %s' % (date, existing_slug.slug)
log_error('Tried to send email again', msg)
raise Exception(msg)

def check_if_intro_email_sent_today(self, date):
two_slugs = [s for s in Slug.query().fetch(2)]

Expand Down

0 comments on commit dd0e2ea

Please sign in to comment.