-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert-exported-csv-for-helios.py
67 lines (54 loc) · 1.92 KB
/
convert-exported-csv-for-helios.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
"""Script to convert voter data for Helios Voting.
This script expects a CSV generated from an Excel/Google Docs export based
off of CivicCRM data.
The format of the CSV is vaguely: firstname,lastname,email
The desired output for Helios is: uniqueid,email,name
"""
import argparse
import csv
import uuid
def generate_unique_id():
"""Create a UUID for our voter."""
return uuid.uuid4().hex
def create_parser():
"""Create our argument parser."""
parser = argparse.ArgumentParser()
parser.add_argument(
'--from', dest='source',
type=argparse.FileType('r'),
help='CSV file based on CivicCRM data',
)
parser.add_argument(
'--to', dest='destination',
type=argparse.FileType('w'),
help='CSV file based on CivicCRM data',
)
return parser
def convert(from_source, to_destination):
"""Handle conversion of the data."""
civicrm_reader = csv.reader(from_source, delimiter=',')
helios_writer = csv.writer(to_destination, delimiter=',')
for row in civicrm_reader:
row = [col.strip() for col in row]
email = row.pop()
if ',' in row[-1]:
row[-1] = row[-1].split(',')[0]
if '@' in row[-1]:
row[-1] = ''
userid = generate_unique_id()
# NOTE(sigmavirus24): We strip here because if the name is ' ' we want
# to be able to easily check for an empty name. In that case, we want
# to give the voter a name to ensure they receive a ballot.
# See also:
# https://github.com/benadida/helios-server/issues/197#issuecomment-396616993
name = ' '.join(row).strip()
if name == '':
name = f'PSF Voter {userid}'
helios_writer.writerow([userid, email, name])
def main():
"""Run the conversion."""
parser = create_parser()
args = parser.parse_args()
convert(args.source, args.destination)
if __name__ == '__main__':
main()