Add route for making a new question
This commit is contained in:
parent
deadf5a61b
commit
cf85a316e3
0
QuizTheWord/__init__.py
Normal file
0
QuizTheWord/__init__.py
Normal file
@ -24,22 +24,7 @@ def questions():
|
|||||||
@roles_required("admin")
|
@roles_required("admin")
|
||||||
def edit_question(question_id):
|
def edit_question(question_id):
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
data = {
|
data = parse_question_form_data(request.form)
|
||||||
"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)
|
|
||||||
data["create_multiple_choice"] = request.form.get("create_multiple_choice", False, bool)
|
|
||||||
data["create_hidden_answer"] = request.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
|
|
||||||
database.update_question(question_id, data)
|
database.update_question(question_id, data)
|
||||||
return redirect(url_for("admin.edit_question", question_id=question_id))
|
return redirect(url_for("admin.edit_question", question_id=question_id))
|
||||||
|
|
||||||
@ -74,6 +59,16 @@ def query_questions():
|
|||||||
return jsonify(response_dict)
|
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):
|
def parse_question_query(query):
|
||||||
if query:
|
if query:
|
||||||
query: dict = json.loads(query)
|
query: dict = json.loads(query)
|
||||||
@ -83,3 +78,23 @@ def parse_question_query(query):
|
|||||||
query["hidden_answer"] = True if query["hidden_answer"] == "true" else False
|
query["hidden_answer"] = True if query["hidden_answer"] == "true" else False
|
||||||
return query
|
return query
|
||||||
return None
|
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
|
||||||
|
@ -39,7 +39,6 @@
|
|||||||
queryParams: query_params,
|
queryParams: query_params,
|
||||||
showRefresh: true,
|
showRefresh: true,
|
||||||
searchOnEnterKey: true,
|
searchOnEnterKey: true,
|
||||||
{#disableControlWhenSearch: true,#}
|
|
||||||
onClickRow: (item, element) => {
|
onClickRow: (item, element) => {
|
||||||
console.log(item);
|
console.log(item);
|
||||||
console.log(element);
|
console.log(element);
|
||||||
@ -48,6 +47,14 @@
|
|||||||
onLoadSuccess: enable_input,
|
onLoadSuccess: enable_input,
|
||||||
onPageChange: disable_input,
|
onPageChange: disable_input,
|
||||||
onSearch: disable_input,
|
onSearch: disable_input,
|
||||||
|
buttons: [
|
||||||
|
{
|
||||||
|
html: "<a class='btn btn-secondary' href='{{ url_for('admin.create_question') }}'>Add Question</a>",
|
||||||
|
attributes: {
|
||||||
|
title: "Add a new question to the database."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
columns: [
|
columns: [
|
||||||
{
|
{
|
||||||
field: "question_id",
|
field: "question_id",
|
||||||
|
@ -3,6 +3,7 @@ from flask import request, render_template, jsonify, Flask
|
|||||||
from flask_security import Security, SQLAlchemySessionUserDatastore
|
from flask_security import Security, SQLAlchemySessionUserDatastore
|
||||||
from QuizTheWord import database
|
from QuizTheWord import database
|
||||||
from QuizTheWord.admin import admin
|
from QuizTheWord.admin import admin
|
||||||
|
from QuizTheWord import config
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
environment_configuration = os.environ.get('CONFIGURATION_SETUP', "QuizTheWord.config.Development")
|
environment_configuration = os.environ.get('CONFIGURATION_SETUP', "QuizTheWord.config.Development")
|
||||||
|
@ -80,7 +80,7 @@ class AllQuestions(Base):
|
|||||||
multiple_choice = relationship("MultipleChoice", uselist=False, back_populates="all_question_relationship")
|
multiple_choice = relationship("MultipleChoice", uselist=False, back_populates="all_question_relationship")
|
||||||
hidden_answer = relationship("HiddenAnswer", uselist=False, back_populates="all_question_relationship")
|
hidden_answer = relationship("HiddenAnswer", uselist=False, back_populates="all_question_relationship")
|
||||||
|
|
||||||
def __init__(self, question_id, question, answer, addresses):
|
def __init__(self, question_id=None, question=None, answer=None, addresses=None):
|
||||||
self.question_id = question_id
|
self.question_id = question_id
|
||||||
self.question_text = question
|
self.question_text = question
|
||||||
self.answer = answer
|
self.answer = answer
|
||||||
@ -172,7 +172,8 @@ class MultipleChoice(Base):
|
|||||||
|
|
||||||
def add_multiple_choice_question(question, answer, addresses, difficulty, hint, wrong_answers):
|
def add_multiple_choice_question(question, answer, addresses, difficulty, hint, wrong_answers):
|
||||||
session = get_session()
|
session = get_session()
|
||||||
question_id = session.query(AllQuestions).count()
|
question_id = session.query(AllQuestions).order_by(AllQuestions.question_id.desc()).first().question_id + 1
|
||||||
|
print(question_id)
|
||||||
base_question = AllQuestions(question_id, question, answer, addresses)
|
base_question = AllQuestions(question_id, question, answer, addresses)
|
||||||
multiple_choice_question = MultipleChoice(question_id, difficulty, hint, wrong_answers, base_question)
|
multiple_choice_question = MultipleChoice(question_id, difficulty, hint, wrong_answers, base_question)
|
||||||
session.add(base_question)
|
session.add(base_question)
|
||||||
@ -180,6 +181,30 @@ def add_multiple_choice_question(question, answer, addresses, difficulty, hint,
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def add_question(data) -> AllQuestions:
|
||||||
|
session = get_session()
|
||||||
|
question_id = session.query(AllQuestions).order_by(AllQuestions.question_id.desc()).first().question_id + 1
|
||||||
|
question = AllQuestions(question_id)
|
||||||
|
session.add(question)
|
||||||
|
if data["create_multiple_choice"]:
|
||||||
|
multiple_choice = MultipleChoice(question.question_id)
|
||||||
|
session.add(multiple_choice)
|
||||||
|
question.multiple_choice = multiple_choice
|
||||||
|
if data["create_hidden_answer"]:
|
||||||
|
hidden_answer = HiddenAnswer(question.question_id)
|
||||||
|
session.add(hidden_answer)
|
||||||
|
question.hidden_answer = hidden_answer
|
||||||
|
for column in data.keys():
|
||||||
|
if column in AllQuestions.__table__.columns:
|
||||||
|
setattr(question, column, data[column])
|
||||||
|
if column in MultipleChoice.__table__.columns:
|
||||||
|
setattr(question.multiple_choice, column, data[column])
|
||||||
|
if column in HiddenAnswer.__table__.columns:
|
||||||
|
setattr(question.hidden_answer, column, data[column])
|
||||||
|
session.commit()
|
||||||
|
return question
|
||||||
|
|
||||||
|
|
||||||
def get_all_questions() -> List[AllQuestions]:
|
def get_all_questions() -> List[AllQuestions]:
|
||||||
session = get_session()
|
session = get_session()
|
||||||
return session.query(AllQuestions).all()
|
return session.query(AllQuestions).all()
|
||||||
|
2
app.yaml
2
app.yaml
@ -1,2 +1,2 @@
|
|||||||
runtime: python39
|
runtime: python39
|
||||||
entrypoint: QuizTheWord/app.py
|
entrypoint: gunicorn -b :$PORT QuizTheWord.app:app
|
@ -1,13 +1,16 @@
|
|||||||
click==7.1.2
|
click~=7.1.2
|
||||||
Flask==1.1.2
|
Flask~=1.1.2
|
||||||
itsdangerous==1.1.0
|
itsdangerous~=1.1.0
|
||||||
Jinja2==2.11.3
|
Jinja2~=2.11.3
|
||||||
MarkupSafe==1.1.1
|
MarkupSafe~=1.1.1
|
||||||
pg8000==1.17.0
|
pg8000~=1.17.0
|
||||||
psycopg2==2.8.6
|
psycopg2~=2.8.6
|
||||||
python-dotenv==0.15.0
|
python-dotenv~=0.15.0
|
||||||
scramp==1.2.0
|
scramp~=1.2.0
|
||||||
SQLAlchemy==1.3.23
|
SQLAlchemy~=1.3.23
|
||||||
Werkzeug==1.0.1
|
Werkzeug~=1.0.1
|
||||||
Flask-Security-Too~=4.0.0
|
Flask-Security-Too~=4.0.0
|
||||||
pytest~=6.2.2
|
pytest~=6.2.2
|
||||||
|
gunicorn~=20.1.0
|
||||||
|
html5validate~=0.0.2
|
||||||
|
bcrypt
|
Loading…
Reference in New Issue
Block a user