quiz-the-word/QuizTheWord/admin/admin.py
2023-11-27 19:25:33 -08:00

173 lines
5.9 KiB
Python

from __future__ import annotations
import json
from flask import Blueprint
from flask import jsonify
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from flask_security import permissions_accepted
from QuizTheWord import database
Admin = Blueprint("admin", __name__, url_prefix="/admin", template_folder="templates", static_folder="static")
@Admin.route("/")
@permissions_accepted("admin_site_access")
def index():
questions = database.get_unhealthy_questions()
return render_template("admin/index.html", title="admin", questions=questions)
@Admin.route("/questions/")
@permissions_accepted("question_edit", "question_add")
def questions():
return render_template("admin/question_list.html")
@Admin.route("/questions/edit/<int:question_id>", methods=["GET", "POST"])
@permissions_accepted("question_edit")
def edit_question(question_id):
if request.method == "POST":
data = parse_question_form_data(request.form)
database.update_question(question_id, data)
return redirect(url_for("admin.edit_question", question_id=question_id))
question: database.AllQuestions = database.get_question(database.AllQuestions, question_id)
if "application/json" in request.accept_mimetypes.values():
return jsonify(question.get_dict())
return render_template("admin/edit_question.html", question=question)
@Admin.route("/questions/query")
@permissions_accepted("admin_site_access")
def query_questions():
offset = request.args.get("offset", type=int)
limit = request.args.get("limit", type=int)
sort = request.args.get("sort")
order = request.args.get("order")
query = parse_question_query(request.args.get("filter"))
result = database.query_all_questions(offset, limit, query, sort, order)
response_dict = {
"total": result[1],
"rows": [],
}
for question in result[0]:
response_dict["rows"].append({
"id": question.question_id,
"question_id": question.question_id,
"question_text": question.question_text,
"answer": question.answer,
"multiple_choice": getattr(question, "multiple_choice", None) is not None,
"hidden_answer": getattr(question, "hidden_answer", None) is not None,
})
return jsonify(response_dict)
@Admin.route("/questions/add", methods=["GET", "POST"])
@permissions_accepted("question_add")
def create_question():
if request.method == "POST":
data = parse_question_form_data(request.form)
question = database.add_question(data)
return redirect(url_for("admin.edit_question", question_id=question.question_id))
question = database.AllQuestions()
return render_template("admin/edit_question.html", question=question)
@Admin.route("/users/")
@permissions_accepted("user_edit")
def users():
return render_template("admin/user_list.html")
@Admin.route("/users/edit/<int:user_id>", methods=["GET", "POST"])
@permissions_accepted("user_edit")
def edit_user(user_id):
if request.method == "POST":
data = parse_user_form_data(request.form)
database.update_user(user_id, data)
return redirect(url_for("admin.edit_user", user_id=user_id))
user = database.get_user(user_id)
roles = database.get_all_roles()
if "application/json" in request.accept_mimetypes.values():
return jsonify(user.get_dict())
return render_template("admin/edit_user.html", user=user, roles=roles)
@Admin.route("/users/query")
@permissions_accepted("user_edit")
def query_users():
offset = request.args.get("offset", type=int)
limit = request.args.get("limit", type=int)
sort = request.args.get("sort")
order = request.args.get("order")
query = parse_user_query(request.args.get("filter"))
result = database.query_users(offset, limit, query, sort, order)
response_dict = {
"total": result[1],
"rows": [],
}
for user in result[0]:
response_dict["rows"].append({
"id": user.user_id,
"user_id": user.user_id,
"username": user.username,
"email": user.email,
})
return jsonify(response_dict)
def parse_question_query(query):
if query:
parsed_query: dict = json.loads(query)
if "multiple_choice" in parsed_query.keys():
parsed_query["multiple_choice"] = True if parsed_query["multiple_choice"] == "true" else False
if "hidden_answer" in parsed_query.keys():
parsed_query["hidden_answer"] = True if parsed_query["hidden_answer"] == "true" else False
return parsed_query
return None
def parse_question_form_data(form):
data = {
"question_text": form.get("question_text"),
"answer": form.get("answer"),
"addresses": form.get("addresses"),
}
multiple_choice_difficulty = form.get("multiple_choice_difficulty", None, int)
wrong_answers = form.getlist("wrong_answers")
hidden_answer_difficulty = form.get("hidden_answer_difficulty", None, int)
data["create_multiple_choice"] = form.get("create_multiple_choice", False, bool)
data["create_hidden_answer"] = form.get("create_hidden_answer", False, bool)
if multiple_choice_difficulty is not None:
data["multiple_choice_difficulty"] = multiple_choice_difficulty
if len(wrong_answers) > 0:
data["wrong_answers"] = wrong_answers
if hidden_answer_difficulty is not None:
data["hidden_answer_difficulty"] = hidden_answer_difficulty
return data
def parse_user_form_data(form):
data = {
"username": form.get("username"),
"email": form.get("email"),
"roles": form.getlist("roles"),
}
return data
def parse_user_query(query):
if query:
parsed_query: dict = json.loads(query)
# if "admin" in query.keys():
# query["admin"] = True if query["admin"] == "true" else False
return parsed_query
return {}