2021-03-07 17:13:11 -08:00
|
|
|
import os
|
2021-04-22 16:48:32 -07:00
|
|
|
from flask import request, render_template, jsonify, Flask, url_for, redirect, flash
|
|
|
|
from flask_security import Security, SQLAlchemySessionUserDatastore, login_user, logout_user, current_user
|
2021-03-22 20:53:50 -07:00
|
|
|
from QuizTheWord import database
|
|
|
|
from QuizTheWord.admin import admin
|
2021-04-22 16:48:32 -07:00
|
|
|
# from QuizTheWord import config
|
2021-03-06 14:29:44 -08:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2021-03-27 13:55:04 -07:00
|
|
|
environment_configuration = os.environ.get('CONFIGURATION_SETUP', "QuizTheWord.config.Development")
|
2021-03-22 22:39:55 -07:00
|
|
|
with app.app_context():
|
|
|
|
app.config.from_object(environment_configuration)
|
|
|
|
user_datastore = SQLAlchemySessionUserDatastore(database.get_session(), database.User, database.Role)
|
2021-04-22 16:48:32 -07:00
|
|
|
security = Security(app, user_datastore, False)
|
2021-03-22 16:23:52 -07:00
|
|
|
app.register_blueprint(admin.Admin)
|
2021-03-06 14:29:44 -08:00
|
|
|
|
|
|
|
|
2021-04-22 16:48:32 -07:00
|
|
|
@app.context_processor
|
|
|
|
def func():
|
2021-08-29 15:59:43 -07:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
data = {
|
|
|
|
"user_authenticated": True,
|
|
|
|
"has_admin_access": current_user.has_permission("admin_site_access"),
|
|
|
|
"is_admin": current_user.has_role("admin"),
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
data = {
|
|
|
|
"user_authenticated": False,
|
|
|
|
"has_admin_access": False,
|
|
|
|
"is_admin": False,
|
|
|
|
}
|
|
|
|
return data
|
2021-04-22 16:48:32 -07:00
|
|
|
|
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2021-03-18 20:08:19 -07:00
|
|
|
return multiple_choice_category()
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/category/hidden_answer")
|
|
|
|
def hidden_answer_category():
|
|
|
|
easy = database.get_random_hidden_answer(1)
|
|
|
|
medium = database.get_random_hidden_answer(2)
|
|
|
|
hard = database.get_random_hidden_answer(3)
|
2021-03-06 14:29:44 -08:00
|
|
|
return render_template(
|
|
|
|
"hidden_answer.html",
|
2021-03-18 20:08:19 -07:00
|
|
|
title="Hidden Answer",
|
2021-03-06 14:29:44 -08:00
|
|
|
easy=easy,
|
|
|
|
medium=medium,
|
|
|
|
hard=hard,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
@app.route("/category/multiple_choice")
|
|
|
|
def multiple_choice_category():
|
2021-03-22 16:23:52 -07:00
|
|
|
easy = database.get_random_multiple_choice(1)
|
2021-03-18 20:08:19 -07:00
|
|
|
easy.randomize_answer_list()
|
2021-04-03 15:59:32 -07:00
|
|
|
medium = database.get_random_multiple_choice(2)
|
|
|
|
medium.randomize_answer_list()
|
|
|
|
hard = database.get_random_multiple_choice(3)
|
|
|
|
hard.randomize_answer_list()
|
2021-03-18 20:08:19 -07:00
|
|
|
return render_template(
|
|
|
|
"multiple_choice.html",
|
|
|
|
title="Multiple Choice",
|
|
|
|
easy=easy,
|
2021-04-03 15:59:32 -07:00
|
|
|
medium=medium,
|
|
|
|
hard=hard,
|
2021-03-18 20:08:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/category/multiple_choice/check_answer", methods=["GET"])
|
|
|
|
def check_answer():
|
|
|
|
question_id = request.args.get("question_id", type=int)
|
|
|
|
answer = request.args.get("answer", type=str)
|
|
|
|
if question_id is not None and answer is not None:
|
|
|
|
return jsonify(database.check_answer(question_id, answer))
|
|
|
|
|
|
|
|
|
2021-04-22 16:48:32 -07:00
|
|
|
@app.route("/login", methods=["GET", "POST"])
|
|
|
|
def login():
|
|
|
|
next_page = request.args.get("next", default=url_for("index"))
|
|
|
|
if request.method == "POST":
|
|
|
|
email = request.form.get("email")
|
|
|
|
password = request.form.get("password")
|
|
|
|
remember = request.args.get("remember")
|
|
|
|
user = user_datastore.find_user(email=email)
|
|
|
|
if user is None or not user.check_password(password):
|
|
|
|
flash("invalid email or password")
|
|
|
|
return redirect(url_for("login"))
|
|
|
|
login_user(user, remember=remember)
|
|
|
|
return redirect(next_page)
|
|
|
|
return render_template("login.html", title="login")
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
|
|
|
if request.method == "POST":
|
|
|
|
email = request.form.get("email")
|
|
|
|
password = request.form.get("password")
|
|
|
|
user = database.add_user(email, password)
|
|
|
|
if user is None:
|
|
|
|
flash("email already in use")
|
|
|
|
return redirect(url_for("register"))
|
|
|
|
login_user(user)
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
return render_template("register.html")
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/logout")
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
|
|
|
|
2021-03-22 16:23:52 -07:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def error_404(e):
|
|
|
|
print(e)
|
|
|
|
return render_template("error.html", error_msg="The requested page can not be found.", error_code=404), 404
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def error_404(e):
|
|
|
|
print(e)
|
|
|
|
msg = "There was an error with the server."
|
|
|
|
if app.config["DEBUG"]:
|
|
|
|
msg = e
|
|
|
|
return render_template("error.html", error_msg=msg, error_code=500), 500
|
|
|
|
|
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
if __name__ == "__main__":
|
2021-03-22 22:39:55 -07:00
|
|
|
with app.app_context():
|
|
|
|
database.init_db()
|
2021-03-06 14:29:44 -08:00
|
|
|
app.run()
|