Skip to content

Commit

Permalink
Restructured the email-sending script
Browse files Browse the repository at this point in the history
  • Loading branch information
thenoviceoof committed Mar 4, 2012
1 parent 5f99f81 commit 26fc5f3
Showing 1 changed file with 88 additions and 53 deletions.
141 changes: 88 additions & 53 deletions admin.py
Expand Up @@ -12,65 +12,100 @@
import datetime, time
import logging

from models import Flyer, Job
from models import Flyer, Job, EmailToClub
from lib import *
from config import TIMEZONE

class CurrentTimeZone(datetime.tzinfo):
def utcoffset(self, dt):
return TIMEZONE
def dst(self, dt):
return timedelta(0)
def tzname(self, dt):
return "US/Eastern"

class Email(BaseHandler):
def get(self):
# is it a week day?

# if so, get all the active jobs
# check if the jobs are past their event date
# send the emails: bin jobs by email, send

# if it's sunday, deprecate the jobs, repopulate


jobq = Job.all()
# get both init and downloaded states
jobs = list(jobq.filter("done =", False))
# update the flyer list
flyers =
emails = set([j.email for j in jobs])

# ???
if len(emails) > BOUND:
# figure out which emails to include b/c they're new
init_flyers = [j.flyer for j in jobs if not(j.flyer.last_sent_date)]
init_jobs = [j for j in jobs if not(j.flyer.last_sent_date)]
emails = set([j.email for for j in init_jobs])
if len(emails) > BOUND:
# and now calculate on the other emails
flyers = [j.flyer for j in jobs if j.flyer.last_sent_date]
# as of now, use a simple longest w/o update first
flyers = sorted(flyers, key=attrgetter('last_sent_date'))
current_date = datetime.datetime.now(CurrentTimeZone)
day = current_date.weekday() # starts 0=monday... 6=sunday
if day < 5:
# weekday
# get all the active jobs
job_query = Job.all()
job_query.filter("active =", True)
jobs = job_query.fetch(2000)
# check if the jobs are past their event date
flyers = set([j.flyer for j in jobs])
for flyer in flyers:
tmp_email = set([j.email for j in flyer.jobs])
if len(emails.union(tmp_email)) > BOUND:
break
emails = emails.union(tmp_email)

# email sending pre-computation
domain = "http://%s.appspot.com" % get_application_id()
fromaddr = "noreply@%s.appspotmail.com" % get_application_id()
date = time.strftime("%Y/%m/%d")

for email in emails:
js = [j for j in jobs if j.email == email]
msg = mail.EmailMessage(sender="Flyer Guy <%s>" % fromaddr,
to=email.email)
msg.subject = "[Flyer] Reminder (%s)" % date
msg.html = template.render("templates/email.html",
{"jobs": js,
"domain": domain,
"email": email.email})
try:
msg.send()
except apiproxy_errors.OverQuotaError, (message,):
# Log the error.
logging.error("Could not send email")
logging.error(message)
self.response.out.write("Sent emails")
if current_date > flyer.event_date:
flyer.active = False
flyer.put()
for job in jobs:
if not(job.flyer.active):
job.active = False
job.put()
jobs = [j for j in jobs if j.active]
# send the emails: bin jobs by email, send
emails = set([j.email for j in jobs])

# !!!
# email sending pre-computation
domain = "http://%s.appspot.com" % get_application_id()
fromaddr = "noreply@%s.appspotmail.com" % get_application_id()
date = time.strftime("%Y/%m/%d")

# send the emails
for email in emails:
js = [j for j in jobs if j.email == email]
msg = mail.EmailMessage(sender="Flyer Guy <%s>" % fromaddr,
to=email.email)
msg.subject = "[Flyer] Reminder (%s)" % date
msg.html = template.render("templates/email.html",
{"jobs": js,
"domain": domain,
"email": email.email})
try:
msg.send()
except apiproxy_errors.OverQuotaError, (message,):
# Log the error.
logging.error("Could not send email")
logging.error(message)
self.response.out.write("Sent emails")

elif day == 6:
# it's sunday
# check if the flyers are
flyer_query = Flyer.all()
flyer_query.filter("active =", True)
flyers = flyer.query.fetch(200)
for flyer in flyers:
if current_date > flyer.event_date:
flyer.active = False
flyer.put()
# deprecate the jobs
job_query = Job.all()
job_query.filter("active =", True)
jobs = job_query.fetch(2000)
for job in jobs:
job.active = False
job.put()
jobs = [j for j in jobs if j.flyer.active]
# repopulate all the jobs
for job in jobs:
# generate a key
job_obj, made = None, None
while not(job_obj) or not(made):
# randomly generate a key
job_key = generate_random_hash(str(email))
job_obj, made = get_or_make(Job, job_key)
if made:
job_obj.id = job_key
job_obj.flyer = job.flyer
job_obj.email = job.email
job_obj.renewal = job.renewal + 1
job_obj.put()
self.response.out.write("Renewed jobs")

# !!! semesterly cleaning
# clean out the old jobs (month (31 days) or older)
Expand Down

0 comments on commit 26fc5f3

Please sign in to comment.