-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
474 lines (399 loc) · 16 KB
/
events.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# events.py
# written by: Thomas A. Grate
# copyright (c) 2017 by Thomas A. Grate, all rights reserved.
#
# for OYD Daily Program
# class Master_Event - used to track all OYD Daily Program events
# includes the Schema for the Master Events Table - masterevents
class Master_Event(object):
""" Master_Event object:
Attributes contained in Master_Event.attrs dictionary:
sql_id - from Student object
event - values: info, newstudent, attendance, mlt-attendance, ncourse, test, drop
date - date of event
student_sql_id - student's sql id from Student table if Studen event
info_sql_id - information's sql id from Informations Table
nat_area_name - student or informatoion's national area
region_name - student or information's region
school_name - student or information's school
first_name - student or information's first name
last_name - student or information's last name
age - student or information's age
occupation - student or information's occupation
rank - student's rank (if student related event)
rank_tested - rank for which the student tested (if testing event)
pass_fail - pass or fail for rank tested (if testing event)
course_name - name of course (if mlt-attendance event)
"""
def __init__(self, event=None, date=None, student_sql_id=None, info_sql_id=None,
nat_area_name=None, region_name=None, school_name=None,
first_name=None, last_name=None,
age = None, occupation=None,
rank=None, rank_tested=None, pass_fail=None,
course_name=None):
""" Master_Event Object: __init__ Method
Parameters:
1) See help for the Object definition for all attributes.
2) all attributes can be passed to __init__ as a Parameters
to initialize the instance"""
# Dictionary of New_Students_Event atrributes
self.attrs = {'sql_id': None,
'event': event,
'date': date,
'student_sql_id': student_sql_id,
'info_sql_id': info_sql_id,
'nat_area_name': nat_area_name,
'region_name': region_name,
'school_name': school_name,
'first_name': first_name,
'last_name': last_name,
'age': age,
'occupation': occupation,
'rank': rank,
'rank_tested': rank_tested,
'pass_fail': pass_fail,
'course_name': course_name
}
# SQL Schema
# Must match the attrs (attributes) above, line for line
self.schema = ['sql_id',
'event',
'date',
'student_sql_id',
'info_sql_id',
'nat_area_name',
'region_name',
'school_name',
'first_name',
'last_name',
'age',
'occupation',
'rank',
'rank_tested',
'pass_fail',
'course_name'
]
# create the INSERT schema substituion string
self.schema_insert = ", ".join(self.schema)
# SQL Data Types for the SQL Schema
# Must match the SQL Schema above, line for line
self.types = ['integer primary key',
'text',
'datetime',
'integer',
'integer',
'text',
'text',
'text',
'text',
'text',
'integer',
'text',
'text',
'text',
'text',
'text'
]
# make the CREATE schema substituion string
# used to create the student table
self.schema_create = ''
limit = len(self.schema) - 1
i = 0
for i in range(0, limit):
addstr = self.schema[i] + ' ' + self.types[i] + ', '
self.schema_create += addstr
self.schema_create += self.schema[limit] + ' ' + self.types[limit]
# VALUE substitution string
self.schema_insert_sub = '(' + '?,' * (len(self.schema) - 1) + '?)'
def _get (self):
""" Master_Event Object: _get method (private)
Returns a tuple of the Master_Event data
Used by _sql_insert """
# assuming that dictiionaries are unordered
# retrive the data in oder as a tuple
results = []
for key in self.schema:
results.append(self.attrs[key])
return tuple(results)
def _sql_insert (self, db):
"""Master_Event Object: _sql_insert method (private)
Inserts the instance of Master Event into the database as a new row
Parameters:
db = database"""
conn = db.conn
c = db.cursor
try:
c.execute('INSERT INTO masterevents (' + self.schema_insert + \
') VALUES ' + self.schema_insert_sub, self._get())
except:
# Insert failed so return Error
return 1
# Save (commit) the changes
conn.commit()
return 0
def put (self, db):
"""Master_Event Object: put method
Inserts the instance of Master_Event into the database as a new row
Parameters:
db = Database object that contains the
connection & cursor to database"""
return self._sql_insert(db)
# class New_Students_Event - used to track new student events
# includes the Schema for the New Student Events Table - newstudents
class New_Students_Event(object):
""" New_Students_Event object:
Attributes contained in New_Students_Event.attrs dictionary:
student_sql_id - from Student object
oyd_id - Oom Yung Doe ID from Student object
signup_date - start_date from Student object"""
def __init__(self, student_sql_id=None, oyd_id=None, signup_date=None):
""" New_Students_Event Object: __init__ Method
Parameters:
1) See help for the Object definition for all attributes.
2) all attributes can be passed to __init__ as a Parameters
to initialize the instance"""
# Dictionary of New_Students_Event atrributes
self.attrs = {'sql_id': None,
'student_sql_id': student_sql_id,
'oyd_id': oyd_id,
'signup_date': signup_date
}
# SQL Schema
# Must match the attrs (attributes) above, line for line
self.schema = ['sql_id',
'student_sql_id',
'oyd_id',
'signup_date'
]
# create the INSERT schema substituion string
self.schema_insert = ", ".join(self.schema)
# SQL Data Types for the SQL Schema
# Must match the SQL Schema above, line for line
self.types = ['integer primary key',
'integer',
'integer',
'datetime'
]
# make the CREATE schema substituion string
# used to create the student table
self.schema_create = ''
limit = len(self.schema) - 1
i = 0
for i in range(0, limit):
addstr = self.schema[i] + ' ' + self.types[i] + ', '
self.schema_create += addstr
self.schema_create += self.schema[limit] + ' ' + self.types[limit]
# VALUE substitution string
self.schema_insert_sub = '(' + '?,' * (len(self.schema) - 1) + '?)'
def _get (self):
""" New_Student_Event Object: _get method (private)
Returns a tuple of the New_Student_Event data
Used by _sql_insert """
# assuming that dictiionaries are unordered
# retrive the data in oder as a tuple
results = []
for key in self.schema:
results.append(self.attrs[key])
return tuple(results)
def _sql_insert (self, db):
"""New_Students_Event Object: _sql_insert method (private)
Inserts the instance of New Student to the database as a new row
Parameters:
db = database"""
conn = db.conn
c = db.cursor
try:
c.execute('INSERT INTO newstudents (' + self.schema_insert + \
') VALUES ' + self.schema_insert_sub, self._get())
except:
# Insert failed so return Error
return 1
# Save (commit) the changes
conn.commit()
return 0
def put (self, db):
"""New_Students_Event Object: put method
Inserts the instance of New_Students_Event into the database as a new row
Parameters:
db = Database object that contains the
connection & cursor to database"""
return self._sql_insert(db)
# class Testing_Event - used to track student testing events
# includes the Schema for the Testing Events Table - testingevents
class Testing_Event(object):
""" Testing_Event object:
Attributes contained in Testing_Event.attrs dictionary:
student_sql_id - from Student object
oyd_id - Oom Yung Doe ID from Student object
test_date - date of test_date
rank_tested - Two digit rank value (1S - 7D)
pass_fail - Pass, Fail"""
def __init__(self, student_sql_id=None, oyd_id=None, test_date=None,
rank_tested=None, pass_fail=None):
""" Testing_Event Object: __init__ Method
Parameters:
1) See help for the Object definition for all attributes.
2) all attributes can be passed to __init__ as a Parameters
to initialize the instance"""
# Dictionary of New_Students_Event atrributes
self.attrs = {'sql_id': None,
'student_sql_id': student_sql_id,
'oyd_id': oyd_id,
'test_date': test_date,
'rank_tested': rank_tested,
'pass_fail': pass_fail
}
# SQL Schema
# Must match the attrs (attributes) above, line for line
self.schema = ['sql_id',
'student_sql_id',
'oyd_id',
'test_date',
'rank_tested',
'pass_fail'
]
# create the INSERT schema substituion string
self.schema_insert = ", ".join(self.schema)
# SQL Data Types for the SQL Schema
# Must match the SQL Schema above, line for line
self.types = ['integer primary key',
'integer',
'integer',
'datetime',
'text',
'text'
]
# make the CREATE schema substituion string
# used to create the student table
self.schema_create = ''
limit = len(self.schema) - 1
i = 0
for i in range(0, limit):
addstr = self.schema[i] + ' ' + self.types[i] + ', '
self.schema_create += addstr
self.schema_create += self.schema[limit] + ' ' + self.types[limit]
# VALUE substitution string
self.schema_insert_sub = '(' + '?,' * (len(self.schema) - 1) + '?)'
def _get (self):
""" Testing_Event Object: _get method (private)
Returns a tuple of the Testing_Event data
Used by _sql_insert """
# assuming that dictiionaries are unordered
# retrive the data in oder as a tuple
results = []
for key in self.schema:
results.append(self.attrs[key])
return tuple(results)
def _sql_insert (self, db):
"""Testing_Event Object: _sql_insert method (private)
Inserts the instance of Testing Event to the database as a new row
Parameters:
db = database"""
conn = db.conn
c = db.cursor
try:
c.execute('INSERT INTO testingevents (' + self.schema_insert + \
') VALUES ' + self.schema_insert_sub, self._get())
# write an event to the Master Events Tables
test_event_txt = 'test - ' + self.attrs['pass_fail']
me = Master_Event(test_event_txt, self.attrs['test_date'],
self.attrs['student_sql_id'])
me.put(db)
except:
# Insert failed so return Error
return 1
# Save (commit) the changes
conn.commit()
return 0
def put (self, db):
"""Testing_Event Object: put method
Inserts the instance of Testing_Event into the database as a new row
Parameters:
db = Database object that contains the
connection & cursor to database"""
return self._sql_insert(db)
# class Drop_Event - used to track student drop events
# includes the Schema for the Drop Events Table - dropevents
class Drop_Event(object):
""" Drop_Event object:
Attributes contained in Drop_Event.attrs dictionary:
student_sql_id - from Student object
oyd_id - Oom Yung Doe ID from Student object
drop_date - Date Dropped
reason - Reason provided by student for dropping"""
def __init__(self, student_sql_id=None, oyd_id=None, drop_date=None,
reason=None):
""" Drop_Event Object: __init__ Method
Parameters:
1) See help for the Object definition for all attributes.
2) all attributes can be passed to __init__ as a Parameters
to initialize the instance"""
# Dictionary of New_Students_Event atrributes
self.attrs = {'sql_id': None,
'student_sql_id': student_sql_id,
'oyd_id': oyd_id,
'drop_date': drop_date,
'reason': reason
}
# SQL Schema
# Must match the attrs (attributes) above, line for line
self.schema = ['sql_id',
'student_sql_id',
'oyd_id',
'drop_date',
'reason'
]
# create the INSERT schema substituion string
self.schema_insert = ", ".join(self.schema)
# SQL Data Types for the SQL Schema
# Must match the SQL Schema above, line for line
self.types = ['integer primary key',
'integer',
'integer',
'datetime',
'text'
]
# make the CREATE schema substituion string
# used to create the student table
self.schema_create = ''
limit = len(self.schema) - 1
i = 0
for i in range(0, limit):
addstr = self.schema[i] + ' ' + self.types[i] + ', '
self.schema_create += addstr
self.schema_create += self.schema[limit] + ' ' + self.types[limit]
# VALUE substitution string
self.schema_insert_sub = '(' + '?,' * (len(self.schema) - 1) + '?)'
def _get (self):
""" Drop_Event Object: _get method (private)
Returns a tuple of the New_Student_Event data
Used by _sql_insert """
# assuming that dictiionaries are unordered
# retrive the data in oder as a tuple
results = []
for key in self.schema:
results.append(self.attrs[key])
return tuple(results)
def _sql_insert (self, db):
"""Drop_Event Object: _sql_insert method (private)
Inserts the instance of Drop Event to the database as a new row
Parameters:
db = database"""
conn = db.conn
c = db.cursor
try:
c.execute('INSERT INTO dropevents (' + self.schema_insert + \
') VALUES ' + self.schema_insert_sub, self._get())
except:
# Insert failed so return Error
return 1
# Save (commit) the changes
conn.commit()
return 0
def put (self, db):
"""Drop_Event Object: put method
Inserts the instance of Drop_Event into the database as a new row
Parameters:
db = Database object that contains the
connection & cursor to database"""
return self._sql_insert(db)