-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
453 lines (375 loc) · 15.5 KB
/
app.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
from flask import Flask, render_template, request, redirect, url_for, flash, session
from twilio import twiml
from twilio.twiml.messaging_response import Message, MessagingResponse
from twilio.rest import Client
import random
import string
import pymongo
import boto3
from boto3.dynamodb.conditions import Key, Attr
import NLP.nlp_algorithms as nlp
app = Flask(__name__)
app.secret_key="super secret key"
dynamodb = boto3.resource('dynamodb', aws_access_key_id="", aws_secret_access_key="", region_name='us-east-1')
client = boto3.client('dynamodb', aws_access_key_id="", aws_secret_access_key="", region_name='us-east-1')
from boto3.dynamodb.conditions import Key, Attr
class Classes:
def __init__(self, name, bg, blobfill):
self.name = name
self.bg = bg
self.blobfill = blobfill
#class_list = [Classes('English 1A', '#67d7ce', '#b5faf6'), Classes('English 1B', '#91cc49', '#d2f68b'),
# Classes('English 1C', '#2cb2d6', '#71e9fa'), Classes('English 1D', '#7cb36e', '#b8ebac')]
# Getting email of the teacher
class Lesson:
def __init__(self, name, className, bg, blobfill, code, questions, responses):
self.name = name
self.className = className
self.bg = bg
self.blobfill = blobfill
self.code = code
self.questions = questions
self.responses = responses
lesson_list = [] # Create empty lesson list
class_list = [] # Create empty class list
@app.route('/')
def signin():
return render_template('signup.html')
@app.route('/home')
def index():
if 'name' in session:
name = session['name']
if 'email' in session:
email = session['email']
return render_template('index.html', class_list=class_list, name=name)
if __name__== '__main__':
app.run(debug=True)
# ---------------------------------------------------------------------
# user clicks on '+' add class button in index.html -> move to addClass.html
@app.route('/add_class_page')
def add_class_page():
if 'name' in session:
name = session['name']
return render_template('addClass.html', name=name)
# adds a new class to the list of classes in index.html
@app.route('/add_class', methods = ['POST', 'GET'])
def add_class():
# getting data from form
name = request.form['class-name-input']
primary_color = request.form['pri-class-color-input']
secondary_color = request.form['sec-class-color-input']
# insertion into table
table = dynamodb.Table('classes')
if 'email' in session:
email = session['email']
table.put_item(
Item={
'class': name,
'primary_color': primary_color,
'secondary_color': secondary_color,
'email': email
}
)
class_list.append(Classes(name, primary_color, secondary_color))
return redirect(url_for('index', class_list=class_list))
# user clicks on a class icon in index.html -> moves to that class's dashboard
@app.route('/dashboard')
def dashboard():
if 'name' in session:
name = session['name']
className = request.args.get('className')
color = request.args.get('color')
#------------------ newly added: passing survey responses into aws comprehend
# getting all lesson code and feedbacks for a user
if 'email' in session:
email = session['email']
# Get the latest code from the class code, and then pull that data for the feedback displayed
table = dynamodb.Table('classes')
response = table.get_item(
Key={
'email': email,
'class': className
}
)
items = response['Item']
print(items)
if 'latest_code' in items:
latest_code = items['latest_code'][0]
# Now that we have the latest code, we can pass the feedback into the algorithm
# get the feedback given the latest code
table = dynamodb.Table('lessons')
response = table.get_item(
Key={
'email': email,
'code': latest_code
}
)
items = response['Item']
feedback = items['feedback'] # this is the feedback of the latest code. Will throw this into NLP algorithm
sentiment = []
recommend=[]
for f in feedback:
sentiment.append(f[1])
completely, mostly, slightly, dont = 0, 0, 0, 0
for s in sentiment:
if s == 'Completely understand':
completely += 1
if s == 'Mostly understand':
mostly += 1
if s == 'Slightly understand':
slightly += 1
if s == "Don't understand":
dont += 1
data = [completely, mostly, slightly, dont]
print(data)
# remove all sentiment from feedback being passed into NLP algorithm
# ===========================================================================
feedback_no_sent = []
for f in feedback:
feedback_no_sent.append(f[0])
#feedback = feedback_no_sent
# ===========================================================================
# make sure the feedback list is not empty
if feedback:
# make sure the list of negative feedback is not empty
neg_feedback = []
for i in feedback_no_sent: # changed to feedback_no_sent to remove the sentiment strings
if nlp.feedbackSent(i) == -1:
neg_feedback.append(i)
if neg_feedback:
recommend = nlp.getRecommendation(feedback_no_sent) # changed to feedback_no_sent to remove the sentiment strings
print(feedback)
print(recommend)
return render_template('dashboard.html', className=className, color=color, name=name, latestcode=latest_code, chartData=data, recommendations=recommend)
else:
# Probably display error message if code is null
latest_code = "NULL"
recommend=[]
return render_template('dashboard.html', className=className, color=color, name=name, latestcode=latest_code, chartData=[], recommendations=recommend)
@app.route('/signup', methods=['POST'])
def signup():
if request.method == 'POST':
name = request.form['name-input']
email = request.form['email-input']
password = request.form['password-input']
table = dynamodb.Table('users')
table.put_item(
Item={
'name': name,
'email': email,
'password': password
}
)
return render_template('login.html')
return render_template('signup.html')
@app.route('/login')
def login():
# Reset class login to empty so that its not stored if you log out and then login
return render_template('login.html')
@app.route('/check', methods=['POST'])
def check():
if request.method=='POST':
email = request.form['email-input']
password = request.form['password-input']
table = dynamodb.Table('users')
response = table.query(
KeyConditionExpression=Key('email').eq(email)
)
print("HELLO")
print(response)
items = response['Items']
if not items:
msg = "Login Unsuccessful. Double check your information"
return render_template("login.html", msg = msg)
name = items[0]['name']
session['name'] = name
session['email'] = email
print(items[0]['password'])
if password == items[0]['password']:
# Get classes for that teacher
class_table = dynamodb.Table('classes')
response = class_table.query(
KeyConditionExpression=Key('email').eq(email)
)
classes = response['Items']
if not class_list:
for _classes in classes:
class_list.append(Classes(_classes['class'], _classes['primary_color'], _classes['secondary_color']))
# Get lessons for that teacher
lesson_table = dynamodb.Table('lessons')
response = lesson_table.query(
KeyConditionExpression=Key('email').eq(email)
)
lessons = response['Items']
print(lessons)
if lessons: # check if list is empty, skip this if it is
if not lesson_list:
for _lessons in lessons:
lesson_list.append(Lesson(_lessons['lesson_name'], _lessons['class_name'], _lessons['primary_color'],_lessons['secondary_color'],_lessons['code'],_lessons['question'], _lessons['feedback']))
# Return the home page with the teacher name, and classes
return render_template("index.html", name = name, class_list=class_list)
return render_template("login.html")
# ---------------------------------------------------------------------
# a dictionary that keeps track of every survey ever created
# key: the code
# value: a list of size 3, index 0 = the lesson name, index 1 = the survey question, and index 2 = list of student responses
@app.route('/lessons')
def lessons():
return render_template('lessonList.html', lesson_list=lesson_list)
@app.route('/lessonPage', methods= ['GET'])
def lessonPage():
# WE NEED TO GET LESSON INFO FROM DYNAMODB, NOT FROM ARGUMENTS PASSED IN FROM create_survey()
code = request.args.get('code')
lesson = request.args.get('lessonName')
q = request.args.get('question')
className = request.args.get('className')
# feedback = request.args.get('feedback')
if 'email' in session: # grab email from session
email = session['email']
table = dynamodb.Table('lessons')
response = table.get_item(
Key={
'email': email,
'code': code
}
)
items = response['Item']
feedback = items['feedback']
print(feedback)
return render_template('lesson.html', code=code, lessonName=lesson, question=q, className=className, feedback=feedback)
@app.route('/add_survey', methods = ['GET', 'POST'])
def add_survey():
className = request.args.get('className')
return render_template('createSurvey.html', className=className)
# the user is at createSurvey.html and then submits a form -> moves to newly created lesson.html
@app.route("/create_survey", methods=['GET', 'POST'])
def create_survey():
q = request.form['survey-question']
lesson = request.form['survey-title']
className = request.args.get('className')
code = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(6))
# add code to database
if 'email' in session: # grab email from session
email = session['email']
# getting colors from class
table = dynamodb.Table('classes')
response = table.get_item(
Key={
'email': email,
'class': className
}
)
items = response['Item']
pri_color = items['primary_color']
sec_color = items['secondary_color']
table = dynamodb.Table('lessons')
feedback = []
table.put_item(
Item={
'email': email,
'code': code, # add the class name later
'question': q,
'feedback': feedback,
'class': className,
'lesson_name': lesson,
'class_name': className,
'primary_color': pri_color,
'secondary_color': sec_color
}
)
# Add color from the correct class
# get item from table classes (the item is the class)
# from that item, pull the primary and secondary colors, and put them into lesson_list (the next line)
lesson_list.append(Lesson(lesson, className, pri_color, sec_color, code, q, feedback))
table = dynamodb.Table('classes')
result = table.update_item(
Key={
'email': email,
'class': className
},
UpdateExpression="SET latest_code = :i",
ExpressionAttributeValues={
':i': [code],
},
ReturnValues="UPDATED_NEW"
)
return redirect(url_for('lessonPage', code=code, lessonName=lesson, question=q, className=className, feedback=feedback))
# link to the code inputting page for students
@app.route("/goto_code_page")
def goto_code_page():
return render_template('code.html')
# student is at the code.html and inputs code for the survey -> moves to survey.html
@app.route("/enter_code", methods=['GET', 'POST'])
def enter_code():
code = request.form['code']
# So the code is from the form, but the lesson name and the question should be from the database
# Get lessonName and question from database
table = dynamodb.Table('lessons')
response = table.scan(
FilterExpression=Attr('code').eq(code)
)
items = response['Items']
lessonName = items[0]['lesson_name']
question = items[0]['question']
#print(items)
#print(lessonName)
#print(question)
return render_template('survey.html', code=code, lessonName=lessonName, question=question)
# student submits their response in survey.html -> moves to submitted.html
@app.route("/add_response", methods=['GET', 'POST'])
def add_response():
response = [request.form['feedback'], request.form['sentiment']]
code = request.args.get('lessonCode')
# add response to the list attribute of the correct lesson
if 'email' in session: # grab email from session
email = session['email']
table = dynamodb.Table('lessons')
result = table.update_item(
Key={
'email': email,
'code': code
},
UpdateExpression="SET feedback = list_append(feedback, :i)",
ExpressionAttributeValues={
':i': [response],
},
ReturnValues="UPDATED_NEW"
)
# --------------------------
#surveys[code][2].append(response)
#print(surveys)
return render_template('submitted.html')
# ---------------------------------------------------------------------
@app.route("/send_sms", methods=['GET', 'POST'])
def send_sms():
if request.method == 'POST':
msg = request.form['survey-question']
else:
msg = request.args.get('survey-question')
# account_sid, auth_token, and from_ values are from your free twilio account
# for security reasons, I (Jiin) can't leave my account sid, auth token, and twilio number on here :( So you'll have to make your own free twilio account. It's super simple to do:
# 1. create an account here: https://www.twilio.com/try-twilio?_ga=2.182390127.916037802.1606343073-1380573367.1606343073
# 2. Find the account SID and auth token here and generate a Twilio phone number here as well: https://www.twilio.com/console
# after testing, make sure you remove any info related to your account before pushing to the repo
account_sid = "ACCOUNT-SID"
auth_token = "AUTH-TOKEN"
client = Client(account_sid, auth_token)
client.messages.create(
to="TWILIO-NUMBER", # This is the number that the message will be sent to. Change it to your phone number to test it out
from_="RECEIVER-NUMBER",
body=msg
)
# message_body = request.values.get('Body', None)
# resp = MessagingResponse()
# resp.message("thanks!")
# print(message_body)
return render_template('createSurvey.html')
@app.route("/respond_sms", methods=['GET', 'POST'])
def incoming_sms():
"""Send a dynamic reply to an incoming text message"""
# Get the message the user sent our Twilio number
message_body = request.values.get('Body', None)
resp = MessagingResponse()
resp.message("thanks!")
print(message_body)
return str(resp)