-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.py
133 lines (111 loc) · 4.65 KB
/
cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""This module provides endpoint logic for cron jobs that send team update
reminder emails and digest emails.
"""
import datetime
import functools
import logging
import webapp2
from google.appengine.api import mail
import model
class CronUpdateHandler(webapp2.RequestHandler):
@classmethod
def get_reply_address(cls, urlsafe):
"""Return update email reply address given supplied urlsafe string."""
return 'PIF <update+%[email protected]>' % urlsafe
@classmethod
def get_update_message(cls, team, to, sender, date):
"""Return unsent update EmailMessage."""
day = "{:%b %d, %Y}".format(date)
header = "Just reply with a few brief bullets starting with *. "
header += "Finish with [DONE] if there is extraneous or quoted "
header += "text at the end of the e-mail reply."
fields = dict(
sender=sender,
to=to,
reply_to=sender,
subject='[PIF-update] Send %s updates - %s' % (team.upper(), day),
body=header)
return mail.EmailMessage(**fields)
@classmethod
def process_subscriber_update(cls, date, subscriber):
"""Create new SubscriberUpdate and send update email to subscriber."""
subscriber_update = model.SubscriberUpdate.get_or_insert(
name=subscriber.name, mail=subscriber.mail, team=subscriber.team,
date=date)
sender = cls.get_reply_address(subscriber_update.key.urlsafe())
message = cls.get_update_message(
subscriber.team, subscriber.mail, sender, date)
message.send()
subscriber_update.sent = True
subscriber_update.put()
@classmethod
def process_update(cls, team, date):
"""Create new Update for supplied team and send team update emails."""
update = model.Update.get_or_insert(team, date=date)
f = functools.partial(cls.process_subscriber_update, update.date)
map(f, model.Subscriber.subscribed(team))
def update(self, team):
"""Sends update reminder emails to all subscribers."""
self.process_update(team, datetime.datetime.now())
class CronDigestHandler(webapp2.RequestHandler):
@classmethod
def get_digest_message(cls, team, digest, date, to):
"""Sends update reminder email to subscriber."""
day = "{:%b %d, %Y}".format(date)
reply_to = 'PIF <[email protected]>'
fields = dict(
sender=reply_to,
to=to,
reply_to=reply_to,
subject='[PIF-update] %s team updates - %s' % (team.upper(), day),
body=digest)
return mail.EmailMessage(**fields)
@classmethod
def get_subscriber_updates(cls, team, date):
"""Return list of SubscriberUpdate for team and date with messages."""
return [x for x in
model.SubscriberUpdate.get_updates(date, team)
if x.message]
@classmethod
def get_update(cls, x):
"""Return formatted update string for supplied SubscriberUpdate x."""
update = x.to_dict()
update['message'] = update['message'].encode('utf8')
return '{name} <{mail}>\n{message}\n\n'.format(**update)
@classmethod
def process_digest(cls, team, test=None):
update = model.Update.latest(team)
if not update:
logging.info('No Update to process for digest')
return
digest = ''.join(
map(cls.get_update, cls.get_subscriber_updates(team, update.date)))
if test:
return digest
if not digest:
logging.info('No subscriber updates to process for digest')
return
for subscriber in model.Subscriber.subscribed(team):
subscriber_digest = model.SubscriberDigest.get_or_insert(
mail=subscriber.mail, team=team, date=update.date)
if not subscriber_digest.sent:
message = cls.get_digest_message(
team, digest, update.date, subscriber.mail)
message.send()
subscriber_digest.sent = True
subscriber_digest.put()
return digest
def digest(self, team):
test = self.request.get('test')
if test:
digest = self.process_digest(team, test=test)
self.response.out.write(digest)
else:
self.process_digest(team)
routes = [
webapp2.Route('/cron/update/<team:.*>', handler=CronUpdateHandler,
handler_method='update'),
webapp2.Route('/cron/digest/<team:.*>', handler=CronDigestHandler,
handler_method='digest'),
]
handlers = webapp2.WSGIApplication(routes, debug=True)