diff --git a/QuizTheWord/__init__.py b/QuizTheWord/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/QuizTheWord/admin/__init__.py b/QuizTheWord/admin/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/QuizTheWord/admin/admin.py b/QuizTheWord/admin/admin.py index 5786d56..702e860 100644 --- a/QuizTheWord/admin/admin.py +++ b/QuizTheWord/admin/admin.py @@ -24,12 +24,21 @@ def questions(): @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") - ) + 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(): @@ -54,7 +63,7 @@ def query_questions(): response_dict["rows"].append({ "id": question.question_id, "question_id": question.question_id, - "question": question.question, + "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, diff --git a/QuizTheWord/admin/templates/admin/edit_question.html b/QuizTheWord/admin/templates/admin/edit_question.html index 2dd8cda..096c308 100644 --- a/QuizTheWord/admin/templates/admin/edit_question.html +++ b/QuizTheWord/admin/templates/admin/edit_question.html @@ -4,7 +4,7 @@
- +
@@ -16,41 +16,45 @@
{% if question.multiple_choice is not none %} -
- +
- -
-
- -
    - {% for answer in question.multiple_choice.wrong_answers %} -
  • - - -
  • - {% endfor %} -
- + +
+ +
+
+ +
    + {% for answer in question.multiple_choice.wrong_answers %} +
  • + + +
  • + {% endfor %} +
+ +
{% endif %} {% if question.hidden_answer is not none %} -
- +
- + +
+ +
{% endif %} @@ -61,7 +65,28 @@ {% block scripts %} {% endblock %} diff --git a/QuizTheWord/app.py b/QuizTheWord/app.py index 53b7723..2e8e069 100644 --- a/QuizTheWord/app.py +++ b/QuizTheWord/app.py @@ -5,7 +5,7 @@ from QuizTheWord import database from QuizTheWord.admin import admin app = Flask(__name__) -environment_configuration = os.environ['CONFIGURATION_SETUP'] +environment_configuration = os.environ.get('CONFIGURATION_SETUP', "QuizTheWord.config.Development") with app.app_context(): app.config.from_object(environment_configuration) user_datastore = SQLAlchemySessionUserDatastore(database.get_session(), database.User, database.Role) diff --git a/QuizTheWord/config.py b/QuizTheWord/config.py index 711bd04..d35bc9a 100644 --- a/QuizTheWord/config.py +++ b/QuizTheWord/config.py @@ -21,11 +21,11 @@ class Production(Config): DEBUG = False DB_SOCKET_DIR = environ.get("DB_SOCKET_DIR", "/cloudsql") CLOUD_SQL_CONNECTION_NAME = environ.get("CLOUD_SQL_CONNECTION_NAME") - DB_URL = f"postgres+psycopg2://{Config.DB_USER}:{Config.DB_PASSWORD}@{Config.DB_HOST}:{Config.DB_PORT}/{Config.DB_NAME}?host={DB_SOCKET_DIR}/{CLOUD_SQL_CONNECTION_NAME}" + DB_URL = f"postgresql+psycopg2://{Config.DB_USER}:{Config.DB_PASSWORD}@{Config.DB_HOST}:{Config.DB_PORT}/{Config.DB_NAME}?host={DB_SOCKET_DIR}/{CLOUD_SQL_CONNECTION_NAME}" class Development(Config): FLASK_ENV = "development" DEBUG = True TESTING = True - DB_URL = f"postgres+psycopg2://{Config.DB_USER}:{Config.DB_PASSWORD}@{Config.DB_HOST}:{Config.DB_PORT}/{Config.DB_NAME}" + DB_URL = f"postgresql+psycopg2://{Config.DB_USER}:{Config.DB_PASSWORD}@{Config.DB_HOST}:{Config.DB_PORT}/{Config.DB_NAME}" diff --git a/QuizTheWord/database.py b/QuizTheWord/database.py index 29b8691..3ec37b5 100644 --- a/QuizTheWord/database.py +++ b/QuizTheWord/database.py @@ -69,7 +69,7 @@ class AllQuestions(Base): __tablename__ = "all_questions" question_id = Column(Integer, primary_key=True, nullable=False) - question = Column(String) + question_text = Column(String) answer = Column(String) addresses = Column(String) @@ -78,7 +78,7 @@ class AllQuestions(Base): def __init__(self, question_id, question, answer, addresses): self.question_id = question_id - self.question = question + self.question_text = question self.answer = answer self.addresses = addresses @@ -88,7 +88,7 @@ class AllQuestions(Base): def get_dict(self): result = { "question_id": self.question_id, - "question": self.question, + "question": self.question_text, "answer": self.answer, "addresses": self.addresses, } @@ -103,18 +103,18 @@ class HiddenAnswer(Base): __tablename__ = "category_hidden_answer" question_id = Column(Integer, ForeignKey("all_questions.question_id"), primary_key=True) - difficulty = Column(Integer) - hint = Column(JSON) + hidden_answer_difficulty = Column(Integer) + hidden_answer_hint = Column(JSON) all_question_relationship = relationship("AllQuestions", lazy="joined", back_populates="hidden_answer") - question = association_proxy("all_question_relationship", "question") + question_text = association_proxy("all_question_relationship", "question_text") answer = association_proxy("all_question_relationship", "answer") addresses = association_proxy("all_question_relationship", "addresses") def __init__(self, question_id, difficulty, hint, base_question): self.question_id = question_id - self.difficulty = difficulty - self.hint = hint + self.hidden_answer_difficulty = difficulty + self.hidden_answer_hint = hint self.all_question_relationship = base_question def __repr__(self): @@ -123,8 +123,8 @@ class HiddenAnswer(Base): def get_dict(self): return { "question_id": self.question_id, - "difficulty": self.difficulty, - "hint": self.hint, + "difficulty": self.hidden_answer_difficulty, + "hint": self.hidden_answer_hint, } @@ -132,19 +132,19 @@ class MultipleChoice(Base): __tablename__ = "category_multiple_choice" question_id = Column(Integer, ForeignKey("all_questions.question_id"), primary_key=True) - difficulty = Column(Integer) - hint = Column(JSON) + multiple_choice_difficulty = Column(Integer) + multiple_choice_hint = Column(JSON) wrong_answers = Column(ARRAY(String)) all_question_relationship = relationship("AllQuestions", lazy="joined", back_populates="multiple_choice") - question = association_proxy("all_question_relationship", "question") + question_text = association_proxy("all_question_relationship", "question_text") answer = association_proxy("all_question_relationship", "answer") addresses = association_proxy("all_question_relationship", "addresses") def __init__(self, question_id, difficulty, hint, wrong_answers, base_question): self.question_id = question_id - self.difficulty = difficulty - self.hint = hint + self.multiple_choice_difficulty = difficulty + self.multiple_choice_hint = hint self.wrong_answers = wrong_answers self.all_question_relationship = base_question self.answer_list = None @@ -160,8 +160,8 @@ class MultipleChoice(Base): def get_dict(self): return { "question_id": self.question_id, - "difficulty": self.difficulty, - "hint": self.hint, + "difficulty": self.multiple_choice_difficulty, + "hint": self.multiple_choice_hint, "wrong_answers": self.wrong_answers, } @@ -205,20 +205,20 @@ Optional[Union[MultipleChoice, HiddenAnswer, AllQuestions]]: def get_random_question_of_difficulty(category: Union[Type[MultipleChoice], Type[HiddenAnswer]], difficulty: Literal[1, 2, 3]): session = get_session() - return session.query(category).filter(category.difficulty == difficulty).order_by(func.random()).first() + return session.query(category).filter(category.hidden_answer_difficulty == difficulty).order_by(func.random()).first() def get_random_hidden_answer(difficulty: Optional[Literal[1, 2, 3]] = None) -> HiddenAnswer: session = get_session() if difficulty is not None: - return session.query(HiddenAnswer).filter(HiddenAnswer.difficulty == difficulty).order_by(func.random()).first() + return session.query(HiddenAnswer).filter(HiddenAnswer.hidden_answer_difficulty == difficulty).order_by(func.random()).first() return session.query(HiddenAnswer).order_by(func.random()).first() def get_random_multiple_choice(difficulty: Optional[Literal[1, 2, 3]] = None) -> MultipleChoice: session = get_session() if difficulty is not None: - return session.query(MultipleChoice).filter(MultipleChoice.difficulty == difficulty).order_by( + return session.query(MultipleChoice).filter(MultipleChoice.multiple_choice_difficulty == difficulty).order_by( func.random()).first() return session.query(MultipleChoice).order_by(func.random()).first() @@ -252,10 +252,14 @@ def query_all_questions(offset, limit, query: dict = None, sort=None, order=None return questions, count -def update_question(question_id, question_text, answer, addresses): +def update_question(question_id, data): session = get_session() question: AllQuestions = session.query(AllQuestions).get(question_id) - question.question = question_text - question.answer = answer - question.addresses = addresses + 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() diff --git a/QuizTheWord/templates/hidden_answer.html b/QuizTheWord/templates/hidden_answer.html index f364920..6b2c8cc 100644 --- a/QuizTheWord/templates/hidden_answer.html +++ b/QuizTheWord/templates/hidden_answer.html @@ -5,7 +5,7 @@

Quiz The Word

-

{{ easy.question }}

+

{{ easy.question_text }}

{{ easy.answer }}

{{ easy.addresses }}
@@ -13,7 +13,7 @@