forked from BenMotz/cubetoolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unretire.py
79 lines (54 loc) · 1.78 KB
/
unretire.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
import csv
from toolkit.members.models import Volunteer
FILENAME = "volunteerlist.csv"
def load_data(filename):
data = []
with open(filename, "rt") as f:
reader = csv.reader(f)
for row in reader:
if len(row) == 3:
data.append(row)
return data
def try_get_volunteer_by_name(name):
try:
return Volunteer.objects.get(member__name__iexact=name)
except Volunteer.DoesNotExist:
return None
def try_get_volunteer_by_email(email):
try:
return Volunteer.objects.get(member__email__iexact=email)
except Volunteer.MultipleObjectsReturned:
print "Multiple volunteers with the same email address: {0}".format(email)
except Volunteer.DoesNotExist:
pass
return None
def get_volunteer_objects(volunteers):
results = []
errors = []
for vol_rec in volunteers:
src_name = vol_rec[0] +" " + vol_rec[1]
src_email = vol_rec[2]
vol = try_get_volunteer_by_name(src_name)
if not vol and src_email:
vol = try_get_volunteer_by_email(src_email)
if vol:
results.append(vol)
else:
errors.append(vol_rec)
return results, errors
def unretire(volunteers):
for v in volunteers:
v.active = True
v.save()
def main():
print "Trying to read {0}".format(FILENAME)
volunteer_list = load_data(FILENAME)
print "Attempting to unretire {0} volunteers".format(len(volunteer_list))
volunteers, errors = get_volunteer_objects(volunteer_list)
if errors:
print "\nAborting: couldn't match the following entries in the source file to volunteer records:"
print "\n".join([str(e) for e in errors])
exit(1)
unretire(volunteers)
if __name__ == "__main__":
main()