forked from GamesDoneQuick/donation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvolunteer.py
156 lines (136 loc) · 4.6 KB
/
volunteer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import csv
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.tokens import default_token_generator
from django.db import transaction
from django.urls import reverse
from tracker import auth
from tracker import viewutil
AuthUser = get_user_model()
_targetColumns = [
'name',
'username',
'email',
'position',
]
class DryRunException(Exception):
pass
class VolunteerInfo:
def __init__(self, firstname, lastname, username, email, is_head):
self.firstname = firstname
self.lastname = lastname
self.username = username
self.email = email
self.isHead = is_head
def parse_header_row(row):
mapping = {}
columnIndex = 0
for column in row:
column = column.strip().lower()
if column in _targetColumns:
mapping[column] = columnIndex
columnIndex += 1
return mapping
def parse_volunteer_row(row, mapping):
position = row[mapping['position']].strip().lower()
if 'head' in position:
isHead = True
else:
isHead = False
firstname, space, lastname = row[mapping['name']].strip().partition(' ')
username = row[mapping['username']].strip()
email = row[mapping['email']].strip()
return VolunteerInfo(
firstname=firstname,
lastname=lastname,
username=username,
email=email,
is_head=isHead,
)
def parse_volunteer_info_file(csvFilename):
csvFile = open(csvFilename, 'r')
csvReader = csv.reader(csvFile)
header = True
volunteers = []
mapping = {}
for row in csvReader:
if header:
header = False
mapping = parse_header_row(row)
else:
volunteers.append(parse_volunteer_row(row, mapping))
csvFile.close()
return volunteers
def send_volunteer_mail(
domain,
event,
volunteers,
template,
sender=None,
token_generator=default_token_generator,
verbosity=0,
dry_run=False,
):
if not sender:
sender = viewutil.get_default_email_from_user()
adminGroup, created = Group.objects.get_or_create(name='Bid Admin')
trackerGroup, created = Group.objects.get_or_create(name='Bid Tracker')
for volunteer in volunteers:
try:
with transaction.atomic():
user, created = AuthUser.objects.get_or_create(
email__iexact=volunteer.email,
defaults=dict(
username=volunteer.username,
first_name=volunteer.firstname,
last_name=volunteer.lastname,
email=volunteer.email,
is_active=False,
),
)
user.is_staff = True
if volunteer.isHead:
user.groups.add(adminGroup)
user.groups.remove(trackerGroup)
else:
user.groups.remove(adminGroup)
user.groups.add(trackerGroup)
user.save()
if verbosity > 0:
if created:
print(
'Created user {0} with email {1}'.format(
volunteer.username, volunteer.email
)
)
else:
print(
'Found existing user {0} with email {1}'.format(
volunteer.username, volunteer.email
)
)
context = dict(
event=event,
is_head=volunteer.isHead,
password_reset_url=domain + reverse('tracker:password_reset'),
registration_url=domain + reverse('tracker:register'),
)
if verbosity > 0:
print(
'Sending email to {0}, active = {1}, head = {2}'.format(
volunteer.username, user.is_active, volunteer.isHead
)
)
if not dry_run:
auth.send_registration_mail(
domain,
user,
template,
sender,
token_generator,
extra_context=context,
)
else:
raise DryRunException
except DryRunException:
pass # do not commit anything to the db in dry run mode