83 lines
3.2 KiB
Python
83 lines
3.2 KiB
Python
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/<int:question_id>", methods=["GET", "POST"])
|
|
@roles_required("admin")
|
|
def edit_question(question_id):
|
|
if request.method == "POST":
|
|
data = {
|
|
"question_text": request.form.get("question_text"),
|
|
"answer": request.form.get("answer"),
|
|
"addresses": request.form.get("addresses"),
|
|
}
|
|
multiple_choice_difficulty = request.form.get("multiple_choice_difficulty", None, int)
|
|
wrong_answers = request.form.getlist("wrong_answers")
|
|
hidden_answer_difficulty = request.form.get("hidden_answer_difficulty", None, int)
|
|
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
|
|
database.update_question(question_id, data)
|
|
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_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)
|
|
|
|
|
|
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
|