import json from flask import render_template, Blueprint, request, jsonify, redirect, url_for from flask_security import roles_required from QuizTheWord import database from QuizTheWord.database import get_question Admin = Blueprint("admin", __name__, template_folder="templates") @Admin.route("/admin/") @roles_required("admin") def index(): return render_template("admin/index.html", title="admin") @Admin.route("/admin/questions/") @roles_required("admin") def questions(): return render_template("admin/question_list.html") @Admin.route("/admin/questions/edit/", methods=["GET", "POST"]) @roles_required("admin") def edit_question(question_id): if request.method == "POST": print(f"edit question: {request.form.get('question_text')}") database.update_question(question_id, request.form.get("question_text"), request.form.get("answer"), request.form.get("addresses") ) return redirect(url_for("admin.edit_question", question_id=question_id)) question: database.AllQuestions = 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("/admin/question_query") @roles_required("admin") 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": question.question, "answer": question.answer, "multiple_choice": getattr(question, "multiple_choice", None) is not None, "hidden_answer": getattr(question, "hidden_answer", None) is not None, }) # question1 = get_question(database.AllQuestions, 0) # question2 = get_question(database.AllQuestions, 1) # question3 = get_question(database.AllQuestions, 2) return jsonify(response_dict) def parse_question_query(query): if query: query: dict = json.loads(query) if "multiple_choice" in query.keys(): query["multiple_choice"] = True if query["multiple_choice"] == "true" else False if "hidden_answer" in query.keys(): query["hidden_answer"] = True if query["hidden_answer"] == "true" else False return query return None