101 lines
3.8 KiB
Python
101 lines
3.8 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__, url_prefix="/admin", template_folder="templates", static_folder="static")
|
|
|
|
|
|
@Admin.route("/")
|
|
@roles_required("admin")
|
|
def index():
|
|
return render_template("admin/index.html", title="admin")
|
|
|
|
|
|
@Admin.route("/questions/")
|
|
@roles_required("admin")
|
|
def questions():
|
|
return render_template("admin/question_list.html")
|
|
|
|
|
|
@Admin.route("/questions/edit/<int:question_id>", methods=["GET", "POST"])
|
|
@roles_required("admin")
|
|
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 = 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("/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_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"])
|
|
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)
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|