-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanup-contrib
executable file
·79 lines (61 loc) · 2.82 KB
/
cleanup-contrib
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
#!/usr/bin/python
from __future__ import (absolute_import, division, print_function)
import argparse
from email import charset
from email.header import Header
from email.mime.text import MIMEText
import smtplib
from jinja2 import Environment, FileSystemLoader
import psycopg2
import psycopg2.extras
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
charset.add_charset('utf-8', charset.SHORTEST, charset.QP)
def clean_contrib(db, dryrun):
cur = db.cursor()
if dryrun:
cur.execute('SELECT memid, name, email FROM members WHERE iscontrib ' +
'AND expirydate IS NULL AND lastactive < ' +
'(SELECT MAX(DATE(period_start)) FROM vote_election)')
for row in cur.fetchall():
print("Would downgrade " + row['name'] + " to non-contributing")
else:
cur.execute('UPDATE members SET iscontrib = false WHERE ' +
'iscontrib AND expirydate IS NULL AND lastactive < ' +
'(SELECT MAX(DATE(period_start)) FROM vote_election)')
db.commit()
def send_ping(db, dryrun):
env = Environment(loader=FileSystemLoader('templates/'))
cur = db.cursor()
cur.execute('SELECT memid, name, email FROM members WHERE iscontrib AND ' +
'expirydate IS NULL AND lastactive < ' +
'(SELECT MAX(DATE(period_start)) FROM vote_election)')
for row in cur.fetchall():
print("Sending ping to " + row['name'])
template = env.get_template('activity-ping-email.txt')
msg = MIMEText(template.render(name=row['name'], ),
'plain', 'utf-8')
msg['Subject'] = Header('SPI activity ping for ' + row['name'],
'utf-8')
msg['From'] = 'SPI Membership Committee <[email protected]>'
msg['To'] = row['email']
if not dryrun:
smtp = smtplib.SMTP('localhost')
smtp.sendmail('[email protected]',
[row['email']], msg.as_string())
smtp.quit()
parser = argparse.ArgumentParser(description='Deal with cleaning up ' +
'inactive SPI contributing members.')
parser.add_argument('action', choices=['clean', 'ping'],
help="Clean or only ping inactive members.")
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 = psycopg2.connect(database="spiapp",
cursor_factory=psycopg2.extras.DictCursor)
db.set_client_encoding('UTF8')
if args.action == 'clean':
clean_contrib(db, args.dryrun)
elif args.action == 'ping':
send_ping(db, args.dryrun)