diff --git a/admin/admin.py b/admin/admin.py index ec27447..cd81990 100644 --- a/admin/admin.py +++ b/admin/admin.py @@ -20,10 +20,20 @@ def questions(): return render_template("admin/question_list.html") -@Admin.route("/admin/questions/edit/") +@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) diff --git a/admin/templates/admin/base.html b/admin/templates/admin/base.html index 9c5d268..5e162f6 100644 --- a/admin/templates/admin/base.html +++ b/admin/templates/admin/base.html @@ -4,6 +4,7 @@ Admin + {% block head %}{% endblock %} diff --git a/admin/templates/admin/edit_question.html b/admin/templates/admin/edit_question.html new file mode 100644 index 0000000..2dd8cda --- /dev/null +++ b/admin/templates/admin/edit_question.html @@ -0,0 +1,67 @@ +{% extends "admin/base.html" %} + +{% block body %} +
+
+ + +
+
+ + +
+
+ + +
+ {% if question.multiple_choice is not none %} + +
+ +
+ +
+
+ +
    + {% for answer in question.multiple_choice.wrong_answers %} +
  • + + +
  • + {% endfor %} +
+ +
+
+ {% endif %} + {% if question.hidden_answer is not none %} + +
+ +
+ +
+
+ {% endif %} + +
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/admin/templates/admin/question_list.html b/admin/templates/admin/question_list.html index 19efa43..b6890d9 100644 --- a/admin/templates/admin/question_list.html +++ b/admin/templates/admin/question_list.html @@ -1,7 +1,6 @@ {% extends "admin/base.html" %} {% block head %} - {% endblock %} @@ -22,6 +21,16 @@ var filter_data = [true, false] + function disable_input() { + console.log("disable"); + $("input, select").attr("disabled", true); + } + + function enable_input() { + console.log("enable"); + $("input, select").removeAttr("disabled"); + } + $("#question-table").bootstrapTable({ url: "/admin/question_query", pagination: true, @@ -30,6 +39,15 @@ queryParams: query_params, showRefresh: true, searchOnEnterKey: true, + {#disableControlWhenSearch: true,#} + onClickRow: (item, element) => { + console.log(item); + console.log(element); + window.location.href = `/admin/questions/edit/${item["question_id"]}` + }, + onLoadSuccess: enable_input, + onPageChange: disable_input, + onSearch: disable_input, columns: [ { field: "question_id", diff --git a/database.py b/database.py index f4bebaa..a084fc4 100644 --- a/database.py +++ b/database.py @@ -71,6 +71,19 @@ class AllQuestions(Base): def __repr__(self): return f"" + def get_dict(self): + result = { + "question_id": self.question_id, + "question": self.question, + "answer": self.answer, + "addresses": self.addresses, + } + if self.hidden_answer: + result["hidden_answer"] = self.hidden_answer.get_dict() + if self.multiple_choice: + result["multiple_choice"] = self.multiple_choice.get_dict() + return result + class HiddenAnswer(Base): __tablename__ = "category_hidden_answer" @@ -93,6 +106,13 @@ class HiddenAnswer(Base): def __repr__(self): return f"