-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
461 lines (366 loc) · 15.6 KB
/
server.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
"""Finglish Dictionary """
from jinja2 import StrictUndefined
from sqlalchemy import func, and_, update
from flask import (Flask, render_template, redirect, request, flash, session, jsonify)
from flask_debugtoolbar import DebugToolbarExtension
from model import User, Word, Vocabulary, connect_to_db, db
import random
import API_Call as api_call
app = Flask(__name__)
# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"
# Normally, if you use an undefined variable in Jinja2, it fails
# silently. This is horrible. Fix this so that, instead, it raises an
# error.
# app.jinja_env.undefined = StrictUndefined
@app.route('/')
def index():
"""homepage"""
#if the user is logged in , show their homepage
# session might have other stuff in it so it is safe to just check
#to see the user id in the session keys
print("This is a print test number 2")
if 'current_user_id' in session.keys():
user_id = session['current_user_id']
return render_template('homepage.html', user_id=user_id)
else:
return render_template('homepage.html')
@app.route('/dictionary')
def all_words():
"""view all words """
return render_template("words_list.html")
@app.route('/dictionary', methods=["POST"])
def search_word():
"""search for a word """
english = request.form.get("english")
print("post word",english)
searched_word = db.session.query(Word).filter(Word.english == english).first()
print("query word", searched_word)
return render_template("words_list.html", searched_word=searched_word)
@app.route('/pronouciation/<farsi>')
def pronunciation(farsi):
"""getting the pronunciation for each word """
# Do api_call(farsi_word)
# Return simple json of {'url': '{farvo_url}'}
json_payload = { 'url': api_call.word_url(farsi)}
return jsonify(json_payload)
@app.route('/users')
def all_users():
"""view all users """
if session:
users = User.query.all()
return render_template("user_list.html", users=users )
else:
return redirect('/')
# word.word_id is not defined and misleading syntax for python
# change that with the word_id to create a new variable out of the number
#that route returns
@app.route('/registration')
def registeration_form():
"""Loading registration page"""
return render_template("registration.html")
@app.route('/registration', methods=["POST"])
def registeration_process():
"""handeling registration"""
first_name = request.form.get('first_name')
last_name = request.form.get('last_name')
email = request.form.get('email')
password = request.form.get('password')
age = request.form.get('age')
country = request.form.get('country')
print(first_name,last_name)
# Can probably optimize this by querying by ID or email
if db.session.query(User).filter(User.email == email).all():
flash("This email has already been registered to an existing user, please log in.")
return render_template("log_in.html")
else:
new_user = User(first_name=first_name,last_name=last_name,
email=email, password=password, age=age, country=country)
db.session.add(new_user)
db.session.commit()
session['current_user_id'] = new_user.id
#generate 1 lesson for users when they register
lesson_generator(new_user.id,1)
return redirect("/users/{}".format(new_user.id))
@app.route('/log_in')
def view_login():
"""render log in page"""
return render_template("log_in.html")
@app.route('/log_in', methods=["POST"])
def login_process():
"""handeling login process"""
email_address = request.form.get('email')
password = request.form.get('password')
#check if the entered password match with the password form the existing
#username
current_user = db.session.query(User).filter(User.email == email_address).one()
app.logger.info(current_user)
if current_user.password == password:
#putting a session on the user id to be able to log it out, while logged in
session['current_user_id'] = current_user.id
app.logger.info(str(session['current_user_id']))
# flashing_welcome =flash("welcome {} you are logged in".format(current_user.first_name))
return redirect (f'/users/{current_user.id}')
else:
# flashing_invalid_password=flash ("invalid password, please try again")
return redirect("/")
@app.route('/log_out', methods=["POST"])
def logout_process():
""" give the option of logging out if the user is logged in"""
if 'current_user_id' in session.keys():
session.clear()
# flash("see you next time")
return redirect("/")
@app.route('/profile/<user_id>')
def user_info(user_id):
"""show user info"""
user = db.session.query(User).filter(User.id == user_id).first()
first_name = user.first_name
last_name = user.last_name
country = user.country
age = user.age
return render_template("user_info.html", country=country, first_name= first_name,
last_name=last_name, age=age, user_id=user_id )
def lesson_generator(user_id, lesson_num):
# getting all the words, removing the words that the user already knows and getting 10 random ones
lesson = db.session.query(Word)\
.outerjoin(Vocabulary, and_(Word.id == Vocabulary.word_id, Vocabulary.user_id == user_id))\
.filter(Vocabulary.word_id == None)\
.order_by(func.random())\
.limit(11)\
.all()
print("words", lesson)
#create an emtpy list outside of the for loop and append the vocabs as you
# as you commit to the vocabyoulary list
vocab_list = []
for word in lesson:
user_vocab = Vocabulary(user_id=user_id, word_id=word.id,
lesson_num = lesson_num)
vocab_list.append(user_vocab)
db.session.add(user_vocab)
db.session.commit()
@app.route("/users/<user_id>", methods=["GET","Post"])
def show_user_homepage(user_id):
"""show the list of the lessons"""
english = request.form.get("english")
print("post word",english)
searched_word = db.session.query(Word).filter(Word.english == english).first()
print("query word", searched_word)
user_id = session['current_user_id']
user = User.query.get(user_id)
user_lesson_num_tuple = db.session\
.query(func.max(Vocabulary.lesson_num))\
.filter(Vocabulary.user_id == user_id)\
.first()
user_lesson_num = user_lesson_num_tuple[0]
print("user lesson num", user_lesson_num)
if request.method == "GET":
return render_template("user_homepage.html", user= user,
user_lesson_num = user_lesson_num )
else:
return render_template("words_list.html",
searched_word=searched_word)
@app.route("/users/<user_id>/lesson/<int:lesson_num>")
def show_lesson_page(user_id,lesson_num):
"""show lesson vocabs """
user_id = session["current_user_id"]
# print (user_id)
lesson_vocabs_query= db.session\
.query(Vocabulary)\
.filter(Vocabulary.user_id == user_id,Vocabulary.lesson_num == lesson_num)\
.order_by(Vocabulary.vocab_id)\
.all()
first_word = lesson_vocabs_query[0].word_id
session['answer_dict'] = {}
return render_template("user_lesson.html",
lesson_num = lesson_num,
user_id=user_id, word_id=first_word)
@app.route("/users/<user_id>/lesson/<int:lesson_num>/overview")
def show_lesson_vocabs(user_id,lesson_num):
"""show lesson vocabs """
# user_id = session["current_user_id"]
# print (user_id)
lesson_vocabs_query= db.session\
.query(Vocabulary)\
.filter(Vocabulary.user_id == user_id,Vocabulary.lesson_num == lesson_num)\
.order_by(Vocabulary.vocab_id)\
.all()
first_word = lesson_vocabs_query[0].word_id
next_page = lesson_num+1
return render_template("lesson_overview.html",
lesson_vocabs_query= lesson_vocabs_query,
lesson_num = lesson_num,
user_id=user_id,next_page=next_page, word_id=first_word)
@app.route("/users/<user_id>/lesson/<lesson_num>/flashcards/<word_id>", methods=["GET"])
def flashcards(user_id,lesson_num,word_id):
"""show the words in a lesson """
lesson_vocabs_query= db.session\
.query(Vocabulary)\
.filter(Vocabulary.user_id == user_id,Vocabulary.lesson_num == lesson_num)\
.order_by(Vocabulary.vocab_id)\
.all()
word_query = db.session.query(Word)\
.filter(Word.id==word_id)\
.first()
current_word_id = word_query.id
print (current_word_id)
# first_vocab = lesson_vocabs_query[0]
# first_vocab_word_id = first_vocab.word.id
next_word = None
back_word = None
for i in range(len(lesson_vocabs_query)):
# print("query item",lesson_vocabs_query[i])
if i != (len(lesson_vocabs_query) - 1):
if lesson_vocabs_query[i].word_id ==current_word_id:
next_word = lesson_vocabs_query[i+1].word_id
if i> 0:
back_word = lesson_vocabs_query[i-1].word_id
# print("-----------")
# print(back_word)
return render_template("flashcards.html",user_id=user_id,
lesson_num=lesson_num,word_id=word_id,
next_word=next_word, back_word=back_word, word_query=word_query)
@app.route("/users/<user_id>/lesson/<lesson_num>/quiz/<word_id>", methods=["GET", "POST"])
def validate_answers(user_id,lesson_num,word_id):
"""validating answers and reporting a feedback """
lesson_vocabs_query= db.session\
.query(Vocabulary)\
.filter(Vocabulary.user_id == user_id,Vocabulary.lesson_num == lesson_num)\
.order_by(Vocabulary.vocab_id)\
.all()
word_query = db.session.query(Word)\
.filter(Word.id==word_id)\
.first()
current_word_id = word_query.id
first_word = lesson_vocabs_query[0]
# print("-------")
# print("firstwordid",first_word.word_id)
# print("firstwordfarsai",first_word.word.farsi
next_word = None
back_word = None
for i in range(len(lesson_vocabs_query)):
# print("query item",lesson_vocabs_query[i])
if i != (len(lesson_vocabs_query) - 1):
if lesson_vocabs_query[i].word_id == current_word_id:
next_word = lesson_vocabs_query[i+1].word_id
answer = request.form.get("answer")
previous_word_id = request.form.get("previous_word_id")
print("____")
print("answer",answer)
print("word_id",word_id)
print("previous word id",previous_word_id)
print("i",i)
if answer == "correct" and previous_word_id:
session['answer_dict'][previous_word_id] = 1
# flash(f"correct answer is been saved {previous_word_id}")
if answer == "incorrect" and previous_word_id:
session['answer_dict'][previous_word_id] = 0
# flash("incorrect answer is been saved",current_word_id)
if i> 0:
back_word =lesson_vocabs_query[i-1].word_id
# print("-----------")
# print(back_word)
if request.method == "GET":
# print("get request")
# print(back_word)
return render_template("flashcard_quiz.html", user_id=user_id,
lesson_vocabs_query=lesson_vocabs_query,lesson_num=lesson_num, word_query=word_query,
word_id=word_id, next_word=next_word, back_word=back_word)
else:
return redirect(f"/users/{user_id}/lesson/{lesson_num}/quiz/{word_id}")
@app.route("/users/<user_id>/lesson/<lesson_num>/quiz/<word_id>/result", methods=["Post"])
def showlesson_result(user_id,lesson_num, word_id):
"""show result of the flashcard"""
print("-----------------")
answer = request.form.get("answer")
previous_word_id = request.form.get("previous_word_id")
if answer == "correct" and previous_word_id:
session['answer_dict'][previous_word_id] = 1
# flash("correct answer is been saved")
if answer == "incorrect" and previous_word_id:
session['answer_dict'][previous_word_id] = 0
# flash("incorrect answer is been saved")
result_sum = sum(session['answer_dict'].values())
result_total = len(session['answer_dict'])
#calculating the first vocab so fr try again ,to go back to beggining
#of the lesson
first_word= db.session\
.query(Vocabulary)\
.filter(Vocabulary.user_id == user_id,Vocabulary.lesson_num == lesson_num)\
.order_by(Vocabulary.vocab_id)\
.first()
# make a dic of answers where word_id are keys
# empty the answer dic before making it again per lesson
if 'answer_dict' in session:
for word_id in session['answer_dict']:
if session['answer_dict'][word_id]== 1:
correct_vocab = db.session\
.query(Vocabulary)\
.filter(and_(Vocabulary.user_id == user_id, Vocabulary.word_id == word_id))\
.first()
correct_vocab.correct_count += 1
db.session.commit()
session.pop('answer_dict')
return render_template("lesson_result.html",
lesson_num=lesson_num, user_id=user_id,result_sum=result_sum,
result_total=result_total, word_id=word_id,
first_word=first_word)
# TODO: make this a POST ONLY
#add click handeler to the
@app.route("/update_seen_count/<word_id>", methods=["POST", "GET"])
def update_seen_count(word_id):
"""update seen count per word"""
user_id = session['current_user_id']
print('here: user_id={}, word_id={}'.format(user_id, word_id))
seen_vocab = db.session\
.query(Vocabulary)\
.filter(and_(Vocabulary.user_id == user_id, Vocabulary.word_id == word_id))\
.first()
print("____________")
print(seen_vocab.seen_count)
seen_vocab.seen_count += 1
# how do i update this?
db.session.commit()
return ""
@app.route("/request_new_lesson/<user_id>", methods=["POST", "GET"])
def request_new_lesson(user_id):
user_id = session['current_user_id']
user = User.query.get(user_id)
print("user",user)
#find the max lesson number and add one to it to generate a lesson
# return a tuple
user_lesson_num_tuple = db.session\
.query(func.max(Vocabulary.lesson_num))\
.filter(Vocabulary.user_id == user_id)\
.first()
# print("typeeeeeee",type(user_lesson_num[0]))
user_lesson_num = user_lesson_num_tuple[0]
new_lesson_num = user_lesson_num +1
lesson_generator(user_id,new_lesson_num)
return redirect(f"/profile/{user_id}")
@app.route("/add_new_word/<user_id>", methods=["POST"])
def add_new_word(user_id):
user_id = session['current_user_id']
new_word_english = request.form.get('word')
new_word_phenetic = request.form.get('phenetic')
new_word_translation = request.form.get('translation')
if db.session.query(Word).filter(Word.english == new_word_english).all() :
flash("This word has already exist.")
return render_template("words.html")
else:
new_word = Word(english = new_word_english.lower(), farsi_phonetic = new_word_phenetic, farsi = new_word_translation)
db.session.add(new_word)
db.session.commit()
# @app.route ("weighted_word/lessons", methods=["POST", "GET"])
# def lesson_generator_ww(user_id, lesson_num):
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
app.debug = True
# make sure templates, etc. are not cached in debug mode
app.jinja_env.auto_reload = app.debug
connect_to_db(app)
print("connected to db")
# Use the DebugToolbar
DebugToolbarExtension(app)
app.run(port=5000)