-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·474 lines (390 loc) · 14.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
from flask import (
request,
render_template,
redirect,
session,
url_for,
make_response,
jsonify,
)
from flask_classful import FlaskView, route
from flask_swagger import swagger
from bson.objectid import ObjectId
from classes.mongo import Database
from classes.server import Server
from classes.admin import FlaskAdmin
from classes.static_vars import StaticVars
from classes.swagger import APIDocUI, APIDoc
import json
import bcrypt
import os
from random import shuffle
from dotenv import load_dotenv
# load variables
load_dotenv()
server = Server()
app = server.app
db = Database(".env")
admin = FlaskAdmin(app, db)
CAN_REGISTER = os.getenv("CAN_REGISTER")
DEVELOPMENT_MODE = os.getenv("DEVELOPMENT_MODE")
@app.route("/", methods=["POST", "GET"])
def starting_url():
return redirect(url_for("AuthView:login_auth"))
@app.route("/spec")
def spec():
swag = swagger(app)
api_doc = APIDoc(swag)
return jsonify(api_doc.get_swag())
api_doc_ui = APIDocUI()
app.register_blueprint(api_doc_ui.get_blueprint())
def login_required(func):
def wrapper(*args, **kwargs):
if "email" not in session:
return redirect(url_for("AuthView:login_auth"))
return func(*args, **kwargs)
return wrapper
class AuthView(FlaskView):
default_methods = ["GET", "POST"]
@route("/register", methods=["GET", "POST"])
def register_auth(self):
"""
Create a new user
---
tags:
- users
definitions:
- schema:
id: Group
properties:
name:
type: string
description: the group's name
parameters:
- in: body
name: body
schema:
id: User
required:
- email
- name
properties:
email:
type: string
description: email for user
name:
type: string
description: name for user
address:
description: address for user
schema:
id: Address
properties:
street:
type: string
state:
type: string
country:
type: string
postalcode:
type: string
groups:
type: array
description: list of groups
items:
$ref: "#/definitions/Group"
responses:
201:
description: User created
"""
if not CAN_REGISTER == "True":
message = "Currently, registration is not allowed. Contact: [email protected]"
return render_template("auth/login.html", message=message)
message = "Please register"
server.is_authenticated_check()
if "email" in session:
return redirect(url_for("MainView:index"))
if request.method == "POST":
first_name = request.form.get("first_name")
last_name = request.form.get("last_name")
username = request.form.get("username")
email = request.form.get("email")
password1 = request.form.get("password1")
password2 = request.form.get("password2")
username_found = db.users.find_one({"username": username})
email_found = db.users.find_one({"email": email})
if username_found:
message = "There already is a user by that username"
return render_template("auth/register.html", message=message)
if email_found:
message = "This email already exists in database"
return render_template("auth/register.html", message=message)
if password1 != password2:
message = "Passwords should match!"
return render_template("auth/register.html", message=message)
else:
hashed = bcrypt.hashpw(password2.encode("utf-8"), bcrypt.gensalt())
user_input = {
"first_name": first_name,
"last_name": last_name,
"username": username,
"email": email,
"password": hashed,
}
db.users.insert_one(user_input)
user_data = db.users.find_one({"email": email})
self.create_user_session(user_data)
server.is_authenticated_check()
return render_template("index.html")
return render_template("auth/register.html")
def create_user_session(self, data):
for key, value in data.items():
if key == "_id":
key = "user_id"
value = str(value)
if key not in ["csrf_token", "word_ids", "password"]:
session[key] = value
@route("/login", methods=["GET", "POST"])
def login_auth(self):
message = ""
server.is_authenticated_check()
if "email" in session:
return redirect(url_for("MainView:index"))
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
user_data = db.users.find_one(
{"$or": [{"email": username}, {"username": username}]}
)
if user_data:
passwordcheck = user_data["password"]
if bcrypt.checkpw(password.encode("utf-8"), passwordcheck):
self.create_user_session(user_data)
server.is_authenticated_check()
return redirect(url_for("MainView:index"))
else:
message = "Wrong password"
return render_template("auth/login.html", message=message)
else:
message = "Email not found"
return render_template("auth/login.html", message=message)
return render_template("auth/login.html", message=message)
@route("/logout", methods=["GET", "POST"])
def logout_auth(self):
session.clear()
server.is_authenticated_check()
return redirect(url_for("AuthView:login_auth"))
@route("/update_password", methods=["GET", "POST"])
@login_required
def update_password_auth(self):
if request.method == "POST":
old_password = request.form.get("old_password")
password = request.form.get("password1")
user_dict = db.get_current_user()
password_match = bcrypt.checkpw(
old_password.encode("utf-8"), user_dict["password"]
)
if password_match:
new_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
db.update_password(new_password)
return render_template(
"profile.html", message="Password updated", user_dict=user_dict
)
return render_template(
"profile.html", message="Old password is wrong", user_dict=user_dict
)
class MainView(FlaskView):
default_methods = ["GET", "POST"]
@login_required
def index(self):
types = db.list_types()
type_counts = db.count_words_type()
return render_template("index.html", types=types, type_counts=type_counts)
@login_required
def profile(self):
user_dict = db.get_current_user()
return render_template("profile.html", user_dict=user_dict)
class WordView(FlaskView, StaticVars):
default_methods = ["GET", "POST"]
def __init__(self):
super().__init__()
@login_required
def list(self):
type = request.args.get("type").lower()
words = db.get_words_type(type)
return render_template("words.html", words=words, type=type)
@login_required
def get_word(self):
word_id = request.args.get("word_id", False)
difficulty = request.args.get("difficulty", 3)
type = request.args.get("type", False)
shuffle_study = request.args.get("shuffle_study", False)
shuffle_words = request.args.get("shuffle_words", False)
word_count = request.args.get("word_count", False)
if shuffle_study:
shuffle_study = True
if shuffle_words:
shuffle_words = True
# print(shuffle_study)
# print(shuffle_words)
difficulty = int(difficulty)
if shuffle_words and shuffle_study:
# print("shuffled")
ids_temp = [str(x) for x in db.get_type_word_ids(type, difficulty)]
shuffle(ids_temp)
if word_count:
ids = ids_temp[: int(word_count)]
else:
ids = ids_temp
# print(ids)
session["word_ids"] = ids
if ids:
word_id = ids[0]
word_dict = db.get_word(word_id)
elif not shuffle_words and shuffle_study:
# print("not shuffled")
word_dict = db.get_word(word_id)
ids = session["word_ids"]
# print(ids)
else:
word_dict = db.get_word(word_id)
ids = [str(x) for x in db.get_type_word_ids(word_dict["type"], difficulty)]
if type and not ids:
words = db.get_words_type(type)
return render_template(
"words.html", words=words, type=type, message="No words"
)
elif not ids:
types = db.list_types()
return render_template("index.html", types=types, message="No words")
return render_template(
"word.html",
word_dict=word_dict,
ids=ids,
shuffle_study=shuffle_study,
periods={
"easy_to_med_per": self.AUTO_DIFF_EASY_TO_MEDIUM_PERIOD,
"med_to_hard_per": self.AUTO_DIFF_MEDIUM_TO_HARD_PERIOD,
"hard_per": self.HARD_PERIOD,
},
)
@login_required
def update_difficulty(self):
word_id = request.form.get("word_id")
word_id = request.json["word_id"]
difficulty = request.json["difficulty"]
db.update_difficulty(word_id, int(difficulty))
data = {"message": "Done", "code": "SUCCESS"}
return make_response(jsonify(data), 200)
@login_required
def reset_word_difficulties(self):
db.reset_word_difficulties()
types = db.list_types()
return render_template(
"index.html", types=types, message="Word difficulties resetted"
)
@login_required
def add_update_word(self):
user_id = session["user_id"]
word_id = request.args.get("word_id")
word = request.args.get("word", False)
artikel = request.args.get("artikel", "")
plural = request.args.get("plural", "")
type = request.args.get("type")
sentence = request.args.get("sentence")
difficulty = request.args.get("difficulty", 1)
sentence_eng = request.args.get("sentence_eng")
sentence_user = request.args.get("sentence_user")
pronunciation = request.args.get("pronunciation")
verb_tenses = request.args.get("verb_tenses")
update = request.args.get("update", False)
# word type white list
if type not in [
"noun",
"verb",
"adjective",
"adverb",
"phrase",
"preposition",
"pronoun",
"conjunction",
"interjection",
]:
data = {"message": "Incorrect type", "code": "BAD_REQUEST"}
return make_response(jsonify(data), 400)
if update == "false":
update = False
new_word = {
"word": word,
"type": type,
"user_id": ObjectId(user_id),
"sentence": sentence,
"sentence_eng": sentence_eng,
"sentence_user": sentence_user,
"pronunciation": pronunciation,
"verb_tenses": verb_tenses,
"difficulty": int(difficulty),
}
if type == "noun":
new_word["artikel"] = artikel
new_word["plural"] = plural
if word and type:
db.add_update_word(word_id, new_word, update)
message = "Updated database"
if update:
message = "Updated database"
if word:
return (
json.dumps({"success": True, "message": message}),
200,
{"ContentType": "application/json"},
)
else:
return (
json.dumps({"success": False, "message": "No word entered"}),
400,
{"ContentType": "application/json"},
)
else:
types = db.list_types()
return render_template("index.html", types=types, message="Incorrect input")
@login_required
def update_word_redirect(self):
word_id = request.args.get("word_id")
word_dict = db.get_word(word_id)
types = db.list_types()
return render_template(
"index.html", types=types, word_dict=word_dict, word_id=word_id, update=True
)
@login_required
def delete_word(self):
word_id = request.args.get("word_id")
db.delete_word(word_id)
types = db.list_types()
return render_template("index.html", types=types, message="Word deleted")
@login_required
def search(self):
search_term = request.args.get("search_term").lower()
words = db.search_word(search_term)
if len(words) > 0:
return render_template("words.html", words=words, type="Search")
else:
types = db.list_types()
return render_template("index.html", types=types, message="No words")
@login_required
def get_words_without_sentence(self):
words = db.get_words_without_sentence()
if len(words) > 0:
return render_template("words.html", words=words, type="Add sentence")
else:
types = db.list_types()
return render_template("index.html", types=types, message="No words")
@login_required
def get_counts(self):
type = request.args.get("type", False)
counts = db.count_words_diff(type)
return counts, 200
views = [MainView, WordView, AuthView]
server.register_views(views)
if __name__ == "__main__":
app.run(host="0.0.0.0")