62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
|
import json
|
||
|
from flask import render_template, Blueprint, request, jsonify
|
||
|
import database
|
||
|
from database import get_all_questions, get_question
|
||
|
|
||
|
|
||
|
Admin = Blueprint("admin", __name__, template_folder="templates")
|
||
|
|
||
|
|
||
|
@Admin.route("/admin/")
|
||
|
def index():
|
||
|
return render_template("admin/index.html", title="admin")
|
||
|
|
||
|
|
||
|
@Admin.route("/admin/questions/")
|
||
|
def questions():
|
||
|
return render_template("admin/question_list.html")
|
||
|
|
||
|
|
||
|
@Admin.route("/admin/questions/edit/<int:question_id>")
|
||
|
def edit_question(question_id):
|
||
|
question: database.AllQuestions = get_question(database.AllQuestions, question_id)
|
||
|
return render_template("admin/edit_question.html", question=question)
|
||
|
|
||
|
|
||
|
@Admin.route("/admin/question_query")
|
||
|
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
|