-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
169 lines (120 loc) · 4.94 KB
/
handler.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
import json
import logging
from lib.quizes import set_quiz_status_db, get_quiz_status_db
from lib.classes import get_class_enrollment_db, set_class_enrollment_db, log_class_part_view_db, get_set_class_complete
from lib.certificate import generate_certificate
from lib.emails import email_welcome_messages, email_congrats_messages, email_reminder_messages
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def send_emails(event, context):
email_welcome_messages()
email_congrats_messages()
email_reminder_messages()
def gen_class_certificate(event, context):
userId = event['requestContext']['authorizer']['jwt']['claims']['sub']
requestData = json.loads(event["body"])
className = requestData['className']
quiz_status = get_quiz_status_db(userId, className)
if ( len(quiz_status['passed']) > 0 and len(quiz_status['failed']) == 0 and len(quiz_status['untried']) == 0 ):
# all quizes passed
classInfo = get_set_class_complete(userId, className)
cert = generate_certificate(userId, classInfo['display_name'], '%s-%s-%s' % (classInfo['passed_year'], classInfo['passed_month'], classInfo['passed_day']), classInfo['cert_number'], classInfo['cert_hash'], classInfo['course_name'])
body = {
"message": "certificate generated",
"url": cert
}
statusCode = 200
else:
statusCode = 404
body = {
"message": "certificate not able to be generated"
}
headers = { "Content-type": "application/json", "Access-Control-Allow-Origin": "*" }
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": headers
}
return response
def get_class_enrollment(event, context):
user = event['requestContext']['authorizer']['jwt']['claims']['sub']
className = event['queryStringParameters']['className']
enrollment = get_class_enrollment_db(user, className)
logger.info("enrollment retrieved for user %s in class %s: %s" % (user, className, enrollment))
body = {
"message": "enrollment retrieved",
"enrolled": enrollment
}
headers = { "Content-type": "application/json", "Access-Control-Allow-Origin": "*" }
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": headers
}
return response
def set_class_enrollment(event, context):
user = event['requestContext']['authorizer']['jwt']['claims']['sub']
requestData = json.loads(event["body"])
className = requestData['className']
firstName = requestData['firstName']
lastName = requestData['lastName']
enrollment = set_class_enrollment_db(user, className, firstName, lastName)
logger.info("enrollment indicated for user %s in class %s" % (user, className))
body = {
"message": "enrollment indicated",
"enrolled": True
}
headers = { "Content-type": "application/json", "Access-Control-Allow-Origin": "*" }
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": headers
}
# Add marketo call to note enrollment of user in a course
return response
def log_training_view(event, context):
user = event['requestContext']['authorizer']['jwt']['claims']['sub']
requestData = json.loads(event["body"])
className = requestData['className']
partName = requestData['partName']
log_class_part_view_db(user, className, partName)
body = {
"message": "view indicated"
}
headers = { "Content-type": "application/json", "Access-Control-Allow-Origin": "*" }
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": headers
}
return response
def get_quiz_status(event, context):
user = event['requestContext']['authorizer']['jwt']['claims']['sub']
className = event['queryStringParameters']['className']
quizStatus = get_quiz_status_db(user, className)
body = {
"message": "Quiz status retrieved",
"quizStatus": quizStatus
}
headers = { "Content-type": "application/json", "Access-Control-Allow-Origin": "*" }
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": headers
}
return response
def set_quiz_status(event, context):
user = event['requestContext']['authorizer']['jwt']['claims']['sub']
requestData = json.loads(event["body"])
set_quiz_status_db(user, requestData["className"], {"passed": requestData["passed"], "failed": requestData["failed"]})
logger.info("quiz status indicated for user %s in class %s with passed %s and failed %s" % (user, requestData["className"], json.dumps(requestData["passed"]), json.dumps(requestData["failed"])))
body = {
"message": "Updated quiz status"
}
headers = { "Content-type": "application/json", "Access-Control-Allow-Origin": "*" }
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": headers
}
return response