This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
/
uploadStudents.py
287 lines (218 loc) · 11.2 KB
/
uploadStudents.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/env python
'''Upload the student list to parse.com for the Into to R course Socratic Swirl exercises.
See: https://www.parse.com/docs/rest/guide/#objects-creating-objects
'''
course1keys = {'test' : {'app' : '<parse.com application ID>',
'api' : '<parse.com API key>',
'master' : '<parse.com master key>'},
'prod' : {'app' : '<parse.com application ID>',
'api' : '<parse.com API key>',
'master' : '<parse.com API key>'}}
course2keys = {'test' : {'app' : '<parse.com application ID>',
'api' : '<parse.com API key>',
'master' : '<parse.com API key>'},
'prod' : {'app' : '<parse.com application ID>',
'api' : '<parse.com API key>',
'master' : '<parse.com API key>'}}
keys = {'course1' : course1keys,
'course2' : course2keys}
import json
import httplib
import urllib
def getStudentList(filename):
students = []
f = open(filename)
for lineCt, line in enumerate(f):
line = line[:-1]
if line.startswith('"') or not '\t' in line:
print('The file should be tab-separated and not quoted.')
return []
fields = line.split('\t')
email = fields[2]
email = email.lower()
precept = fields[3]
if not precept.startswith('Precept'):
precept = 'Precept ' + precept
d = {'last' : fields[0],
'first' : fields[1],
'email' : email,
'precept' : precept,
}
students.append(d)
f.close()
return students
def listDBStudents(connection, quiet=False):
params = urllib.urlencode({"limit": 1000})
connection.request('GET',
'/1/classes/StudentList?%s' % params, '',
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],})
response = json.loads(connection.getresponse().read())
results = response['results']
print('Found %d students in database.' % len(results))
results.sort(key=lambda x: (x['last'], x['first']))
if not quiet:
fieldNames = ['last', 'first', 'email', 'precept', 'objectId']
for result in results:
resultStr = ' '.join(['%-25s' % result[f] for f in fieldNames])
print(resultStr)
studentDBDict = dict([(s['email'], s) for s in results])
return studentDBDict
def createDBSchema(connection):
schema = {"className":"StudentList",
"fields": {"last":{"type":"String"},
"first":{"type":"String"},
"email":{"type":"String"},
"precept":{"type":"String"},
}
}
connection.request('POST',
'/1/schemas/StudentList',
json.dumps(schema),
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],
"Content-Type": "application/json"})
result = json.loads(connection.getresponse().read())
print(result)
def deleteStudents(connection, students):
for student in students:
connection.request('DELETE',
'/1/classes/StudentList/%s' % student['objectId'], '',
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],})
response = json.loads(connection.getresponse().read())
print(response)
def deleteAllStudents(connection):
params = urllib.urlencode({"limit": 1000})
connection.request('GET',
'/1/classes/StudentList?$%s' % params, '',
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],})
response = json.loads(connection.getresponse().read())
results = response['results']
print('Found %d students in database.' % len(results))
ans = raw_input('Delete ALL students from database -- are you sure? [y/N]: ')
if ans != 'y':
print('Nothing deleted.')
return
for result in results:
connection.request('DELETE',
'/1/classes/StudentList/%s' % result['objectId'], '',
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],})
response = json.loads(connection.getresponse().read())
print(response)
def postStudents(connection, students):
for student in students:
js = json.dumps(student)
print(js)
connection.request('POST',
'/1/classes/StudentList',
json.dumps(student),
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],
"X-Parse-REST-API-Key": keys[args.dbname][args.instance]['api'],
"Content-Type": "application/json"})
result = json.loads(connection.getresponse().read())
print(result)
def updateStudents(connection, students):
for student in students:
params = urllib.urlencode({"where":json.dumps({"email": student['email']})})
connection.request('GET',
'/1/classes/StudentList?%s' % params, '',
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],})
response = json.loads(connection.getresponse().read())
results = response['results']
if len(results) == 0:
print('Could not find student: %s' % (student['email']))
elif len(results) > 1:
print('Found %d students "%s"' % (len(results), student['email']))
else:
print('Found student %s: %s' % (student['email'], results[0]))
data = json.dumps({"precept": student['precept']})
connection.request('PUT',
'/1/classes/StudentList/%s' % results[0]['objectId'],
data,
{"X-Parse-Application-Id": keys[args.dbname][args.instance]['app'],
"X-Parse-Master-Key": keys[args.dbname][args.instance]['master'],
"X-Parse-REST-API-Key": keys[args.dbname][args.instance]['api'],
"Content-Type": "application/json"})
result = json.loads(connection.getresponse().read())
print(result)
def main(args):
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
if args.list:
listDBStudents(connection)
return
if args.create:
createDBSchema(connection)
return
if args.deleteall:
deleteAllStudents(connection)
return
students = getStudentList(args.filename)
if not students:
print('Did not find any students in %s, nothing done.' % args.filename)
return
print('Found %d students in file.' % len(students))
if args.add or args.update or args.delete:
studentFileDict = dict([(s['email'], s) for s in students])
studentDBDict = listDBStudents(connection, quiet=True)
newStudents = sorted(set(studentFileDict.keys()).difference(set(studentDBDict.keys())))
oldStudents = sorted((set(studentDBDict.keys()).difference(set(studentFileDict.keys()))))
sameStudents = sorted(set(studentFileDict.keys()).intersection(set(studentDBDict.keys())))
newStudentList = [studentFileDict[s] for s in newStudents]
oldStudentList = [studentDBDict[s] for s in oldStudents]
sameStudentList = [studentFileDict[s] for s in sameStudents]
fieldNames = ['last', 'first', 'email', 'precept']
print('New students...')
for s in newStudents:
resultStr = ' '.join(['%-25s' % studentFileDict[s][f] for f in fieldNames])
print(resultStr)
print('Old students...')
for s in oldStudents:
resultStr = ' '.join(['%-25s' % studentDBDict[s][f] for f in fieldNames])
print(resultStr)
print('Students in both file and database...')
for s in sameStudents:
resultStr = ' '.join(['%-25s' % studentFileDict[s][f] for f in fieldNames])
print(resultStr)
print('Students with changed precept...')
for s in sameStudents:
if studentFileDict[s]['precept'] != studentDBDict[s]['precept']:
print('Student: %s old precept: %s new precept: %s' % (s, studentDBDict[s]['precept'], studentFileDict[s]['precept']))
msgList = []
if args.add:
msgList.append('add')
if args.delete:
msgList.append('delete')
if args.update:
msgList.append('update')
ans = raw_input('Ok to %s students? [y/N]: ' % '/'.join(msgList))
if ans != 'y':
print('Nothing done.')
return
if args.add:
postStudents(connection, newStudentList)
if args.delete:
deleteStudents(connection, oldStudentList)
if args.update:
updateStudents(connection, sameStudentList)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='%s' % __doc__)
parser.add_argument('-l', '--list', action='store_true', help='List the students and exit. (default: False)')
parser.add_argument('-c', '--create', action='store_true', help='Create the StudentList schema and exit. (default: False)')
parser.add_argument('--deleteall', action='store_true', help='Delete ALL the existing students from the database and exit. (default: False)')
parser.add_argument('-f', '--filename', help='Tab-separated file of students.')
parser.add_argument('-a', '--add', action='store_true', help='Add new students -- those in the file but not in the database. (default: False)')
parser.add_argument('-d', '--delete', action='store_true', help='Delete students who are in the database but not in the file. (default: False)')
parser.add_argument('-u', '--update', action='store_true', help='Using the email address as the identifier, update the database. (default: False)')
parser.add_argument('-n', '--dbname', required=True, choices=['course1', 'course2'], help='Which database.')
parser.add_argument('-i', '--instance', required=True, choices=['test', 'prod'], help='Which instance.')
args = parser.parse_args()
if (args.add or args.delete or args.update) and not args.filename:
parser.error('--filename required with --add, --delete, or --update.')
main(args)