-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue_digests.py
91 lines (75 loc) · 3.18 KB
/
queue_digests.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
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 coding=utf-8
import logging
import time
import datetime
import random
from google.appengine.api import mail
from google.appengine.api import taskqueue
from google.appengine.api import users
from google.appengine.ext import db
import models
logging.info('Scheduled task starting...')
def construct_digest(nickname, profile_list):
digest_greeting = '''
Hi %(username)s,
Here's what your esteemed colleagues are up to this week:
'''
# format digest_greeting with user's first_name
personalized_digest_plaintext = digest_greeting % {"username": nickname}
for profile in profile_list:
colleague_summary = '''
%(colleague_nick)s (%(colleague_teams)s)
---------------------
%(colleague_tasks)s
'''
# find something to use to identify this colleague
prof_name = profile.first_name
if prof_name is None:
prof_name = profile.nickname
# list all team names this colleague belongs to
prof_team_names = ", ".join([m.team.name for m in profile.membership_set])
prof_taskweek = profile.this_weeks_tw
# TODO refactor control flow. this is whack
if prof_taskweek is not None:
prof_taskweek = prof_taskweek.optimistic
if prof_taskweek is None:
fake_tasks = [
"putting feet up all week",
"sleeping on the job each day",
"taking really really long lunch breaks",
"productivity consumed by facebook bender",
"researching 1000 best cute animal videos",
"catching up on reality TV",
"just chillin",
"secretly daytrading all day",
"'working from home' at the beach"]
prof_tasks = random.choice(fake_tasks) + "\n(no reported tasks this week!)"
else:
prof_tasks = "\n".join(prof_taskweek)
personalized_digest_plaintext = personalized_digest_plaintext +\
colleague_summary % {"colleague_nick": prof_name,
"colleague_teams": prof_team_names,
"colleague_tasks": prof_tasks}
return personalized_digest_plaintext
user_list = models.Profile.all()
for user in user_list:
if not user.weekly_email:
continue
# get a list of this user's esteemed_colleagues
esteemed_colleagues = user.esteemed_colleagues
if esteemed_colleagues is not None:
# get user's first_name
# (or use nickname if first_name isnt specified)
nick = user.first_name
if nick is None:
nick = user.nickname
# construct a personalized message body
digest_message_body = construct_digest(nick, esteemed_colleagues)
sender='SNPTZ <[email protected]>'
to=user.email
reply_to='SNPTZ <[email protected]>'
# TODO make the subject of the email include the date
subject='SNPTZ Esteemed Colleagues digest for %s' % datetime.datetime.now().strftime("%b %d")
body=digest_message_body
taskqueue.add(url='/sendmail', params={'sender': sender,'to': user.email,'reply_to': reply_to,'subject': subject,'body': digest_message_body})