-
Notifications
You must be signed in to change notification settings - Fork 1
/
email-voters
executable file
·68 lines (53 loc) · 2.35 KB
/
email-voters
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
#!/usr/bin/python
from __future__ import (absolute_import, division, print_function)
import argparse
from datetime import datetime, timedelta
from email import charset
from email.header import Header
from email.mime.text import MIMEText
import smtplib
import SPI
from jinja2 import Environment, FileSystemLoader
config = SPI.Config()
charset.add_charset('utf-8', charset.SHORTEST, charset.QP)
def send_email(db, user, vote, new):
env = Environment(loader=FileSystemLoader(config.get('BASE_DIR') +
'/members.git/templates/'))
if new:
template = env.get_template('vote-begin.txt')
subject = Header('SPI vote open: %s' % vote.title)
else:
template = env.get_template('vote-mid.txt')
subject = Header('SPI vote reminder: %s' % vote.title)
msg = MIMEText(template.render(user=user, vote=vote), 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = 'SPI Membership Committee <[email protected]>'
msg['To'] = user.email
print("Emailing voter %s" % user.name.encode('utf-8'))
if not args.dryrun:
smtp = smtplib.SMTP(config.get('SMTP_SERVER'))
smtp.sendmail('[email protected]',
[user.email], msg.as_string())
smtp.quit()
def inform_voters(db, voteid, new=False):
vote = db.get_vote(voteid)
voters = [x.user.memid for x in db.get_membervotes(vote)]
for cm in db.get_applications_by_type('cm'):
if cm.user.memid not in voters:
send_email(db, cm.user, vote, new)
parser = argparse.ArgumentParser(description='Email contributing members ' +
'about open votes')
parser.add_argument('--dry-run', dest='dryrun',
help="Just show what would happen, don't take any action",
action='store_const', const=True, default=False)
args = parser.parse_args()
db = SPI.MemberDB(config.get('DB_TYPE'), config.get('DB_NAME'))
votes = db.get_votes(active=True)
for vote in votes:
now = datetime.utcnow()
mid = vote.start + timedelta(seconds=(vote.end -
vote.start).total_seconds() / 2)
if now-timedelta(hours=24) < vote.start <= now:
inform_voters(db, vote.voteid, new=True)
elif now-timedelta(hours=24) < mid <= now:
inform_voters(db, vote.voteid, new=False)