This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importschools.py
executable file
·155 lines (116 loc) · 4.34 KB
/
importschools.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
#!/usr/bin/env python
# By Alex Monk, based on importusers.py by Jeremy Morse
import argparse
import os
import re
import sys
import yaml
import c_teams
import mailer
import sr
parser = argparse.ArgumentParser()
parser.add_argument("teamsdir", help="Working dir for teams.git")
parser.add_argument(
"--no-emails",
action='store_true',
help="Don't send emails to the team-leaders"
)
args = parser.parse_args()
teams_dir = args.teamsdir
# Test whether teams_dir exists
try:
os.stat(teams_dir)
except OSError:
print >>sys.stderr, "Couldn't stat \"{0}\"".format(teams_dir)
sys.exit(1)
# Suck a list of teams out of teams_dir
team_yaml = []
def add_to_team_yaml(_, dirname, fnames):
if dirname == teams_dir:
for fname in fnames:
team_yaml.append(fname)
os.path.walk(teams_dir, add_to_team_yaml, None)
team_yaml = [x for x in team_yaml if re.match(r"^[A-Z]+\.yaml$", x) != None]
# Filter team records for those who actually posess a team this year.
def is_taking_part_yaml(fname):
with open(os.path.join(teams_dir, fname)) as file:
data = yaml.safe_load(file)
if not data["teams"]:
return False
return True
# On failure
print >>sys.stderr, "Couldn't open {0}".format(fname)
sys.exit(1)
team_yaml = [x for x in team_yaml if is_taking_part_yaml(x)]
# Define a function for reading all relevant team data from a yaml file, and
# sanity checking it.
def read_team_data(fname):
with open(os.path.join(teams_dir, fname)) as fobj:
y = yaml.safe_load(fobj)
if 'contacts' not in y or len(y['contacts']) == 0:
print >>sys.stderr, "No contacts record for {0}".format(fname)
sys.exit(1)
the_contact = y['contacts'][0] # Pick the first contact
if 'email' not in the_contact or 'name' not in the_contact:
print >>sys.stderr,"Incomplete contact record for {0}".format(fname)
sys.exit(1)
if 'teams' not in y or len(y['teams']) == 0:
print >>sys.stderr, "No teams record for {0}".format(fname)
sys.exit(1)
if 'name' not in y:
print >>sys.stderr, "No school name record for {0}".format(fname)
sys.exit(1)
teams = []
for teamname in y['teams']:
assert(isinstance(teamname, basestring))
teams.append(teamname)
# First team name gets used as the college name too...
if re.match("^[A-Z]+$", teams[0]) == None:
print >>sys.stderr, "Team name \"{0}\" is not the conventional format".format(teams[0])
sys.exit(1)
return (the_contact, teams[0], teams, y['name'])
# Grab a useful group
teachers = sr.group('teachers')
if not teachers.in_db:
print >>sys.stderr, "Group {0} doesn't exist".format('teachers')
sys.exit(1)
# Iterate through teams, fetch data, and create accounts.
count = 0
skipped = 0
for team_dot_yaml in team_yaml:
team_leader, college_tla, teams, team_name = read_team_data(team_dot_yaml)
# Does the desired college / team already exist?
college = c_teams.get_college(college_tla)
teamGroups = []
for team in teams:
teamGroup = c_teams.get_team(team)
teamGroups.append(teamGroup)
if college.in_db or len([x for x in teamGroups if x.in_db]) != 0:
print >>sys.stderr, "College {0} or associated teams already in db, skipping import".format(college_tla)
skipped += 1
continue
print >>sys.stderr, "Creating groups + account for {0}".format(college_tla)
# Split at most once -- assume additional name parts go in the last name
first_name, last_name = team_leader['name'].split(' ', 1)
newname = sr.new_username(college_tla, first_name, last_name)
u = sr.users.user(newname)
u.cname = first_name
u.sname = last_name
u.email = team_leader['email']
u.save()
u.set_lang('english')
u.save()
college.user_add(u)
college.desc = team_name
college.save()
for team in teamGroups:
team.user_add(u)
team.save()
teachers.user_add(u)
teachers.save()
print "User {0} created".format(newname)
if not args.no_emails:
mailer.send_template("teacher_welcome", u, { "PASSWORD": u.init_passwd } )
print "User {0} mailed".format(newname)
count += 1
print >>sys.stderr, "Created {0} teams and skipped {1} more".format(count, skipped)