quiz-the-word/main.py
Matthew Welch b4efbc8f3f Added user authentication for the admin site
Added error pages for 404 and 500 codes
Added site icon
2021-03-22 16:23:52 -07:00

76 lines
2.1 KiB
Python

import os
from flask import request, render_template, jsonify, Flask
from flask_security import Security, SQLAlchemySessionUserDatastore, RegisterForm
import database
from admin import admin
app = Flask(__name__)
environment_configuration = os.environ['CONFIGURATION_SETUP']
app.config.from_object(environment_configuration)
user_datastore = SQLAlchemySessionUserDatastore(database.Session, database.User, database.Role)
security = Security(app, user_datastore)
app.register_blueprint(admin.Admin)
@app.route("/")
def index():
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)
return render_template(
"hidden_answer.html",
title="Hidden Answer",
easy=easy,
medium=medium,
hard=hard,
)
@app.route("/category/multiple_choice")
def multiple_choice_category():
easy = database.get_random_multiple_choice(1)
easy.randomize_answer_list()
medium = database.get_random_multiple_choice(2)
medium.randomize_answer_list()
hard = database.get_random_multiple_choice(3)
hard.randomize_answer_list()
return render_template(
"multiple_choice.html",
title="Multiple Choice",
easy=easy,
# medium=medium,
# hard=hard,
)
@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))
@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
if __name__ == "__main__":
app.run()