2020-12-06 16:09:46 -08:00
|
|
|
from flask import current_app, g
|
2021-03-06 14:29:44 -08:00
|
|
|
import sqlalchemy
|
2021-03-18 20:08:19 -07:00
|
|
|
from typing import Union, Optional, Literal, Type, List, Tuple
|
|
|
|
import random
|
2021-03-07 17:13:11 -08:00
|
|
|
import os
|
|
|
|
from sqlalchemy import Column, JSON, String, Integer, create_engine, ForeignKey, func, ARRAY
|
2021-03-06 14:29:44 -08:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.ext.associationproxy import association_proxy
|
2021-03-18 20:08:19 -07:00
|
|
|
from sqlalchemy.orm import sessionmaker, relationship, scoped_session
|
2021-03-07 17:13:11 -08:00
|
|
|
from werkzeug.utils import import_string
|
2020-12-06 16:09:46 -08:00
|
|
|
|
2021-03-07 17:13:11 -08:00
|
|
|
|
|
|
|
environment_configuration = os.environ['CONFIGURATION_SETUP']
|
|
|
|
config = import_string(environment_configuration)
|
|
|
|
engine = create_engine(config.DB_URL)
|
2021-03-18 20:08:19 -07:00
|
|
|
session_factory = sessionmaker(bind=engine)
|
|
|
|
Session = scoped_session(session_factory)
|
2021-03-06 14:29:44 -08:00
|
|
|
Base = declarative_base()
|
2020-12-06 16:09:46 -08:00
|
|
|
|
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
class AllQuestions(Base):
|
|
|
|
__tablename__ = "all_questions"
|
2020-12-06 16:09:46 -08:00
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
question_id = Column(Integer, primary_key=True, nullable=False)
|
|
|
|
question = Column(String)
|
|
|
|
answer = Column(String)
|
|
|
|
addresses = Column(String)
|
2020-12-06 16:09:46 -08:00
|
|
|
|
2021-03-07 17:13:11 -08:00
|
|
|
def __init__(self, question_id, question, answer, addresses):
|
|
|
|
self.question_id = question_id
|
|
|
|
self.question = question
|
|
|
|
self.answer = answer
|
|
|
|
self.addresses = addresses
|
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<Question: {self.question_id}>"
|
2020-12-06 16:09:46 -08:00
|
|
|
|
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
class HiddenAnswer(Base):
|
|
|
|
__tablename__ = "category_hidden_answer"
|
2020-12-06 16:09:46 -08:00
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
question_id = Column(Integer, ForeignKey("all_questions.question_id"), primary_key=True)
|
|
|
|
difficulty = Column(Integer)
|
|
|
|
hint = Column(JSON)
|
2020-12-06 16:09:46 -08:00
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
all_question_relationship = relationship("AllQuestions", foreign_keys=[question_id], lazy="joined", uselist=False)
|
2021-03-06 14:29:44 -08:00
|
|
|
question = association_proxy("all_question_relationship", "question")
|
|
|
|
answer = association_proxy("all_question_relationship", "answer")
|
|
|
|
addresses = association_proxy("all_question_relationship", "addresses")
|
2020-12-06 16:09:46 -08:00
|
|
|
|
2021-03-07 17:13:11 -08:00
|
|
|
def __init__(self, question_id, difficulty, hint, base_question):
|
|
|
|
self.question_id = question_id
|
|
|
|
self.difficulty = difficulty
|
|
|
|
self.hint = hint
|
|
|
|
self.all_question_relationship = base_question
|
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<Question Hidden Answer: {self.question_id}>"
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2021-03-07 17:13:11 -08:00
|
|
|
wrong_answers = Column(ARRAY(String))
|
2021-03-06 14:29:44 -08:00
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
all_question_relationship = relationship("AllQuestions", foreign_keys=[question_id], lazy="joined", uselist=False)
|
2021-03-06 14:29:44 -08:00
|
|
|
question = association_proxy("all_question_relationship", "question")
|
|
|
|
answer = association_proxy("all_question_relationship", "answer")
|
|
|
|
addresses = association_proxy("all_question_relationship", "addresses")
|
|
|
|
|
2021-03-07 17:13:11 -08:00
|
|
|
def __init__(self, question_id, difficulty, hint, wrong_answers, base_question):
|
|
|
|
self.question_id = question_id
|
|
|
|
self.difficulty = difficulty
|
|
|
|
self.hint = hint
|
|
|
|
self.wrong_answers = wrong_answers
|
|
|
|
self.all_question_relationship = base_question
|
2021-03-18 20:08:19 -07:00
|
|
|
self.answer_list = None
|
|
|
|
|
|
|
|
def randomize_answer_list(self):
|
|
|
|
answer_list: List[str] = [*self.wrong_answers, self.answer]
|
|
|
|
random.shuffle(answer_list)
|
|
|
|
self.answer_list = answer_list
|
2021-03-07 17:13:11 -08:00
|
|
|
|
2021-03-06 14:29:44 -08:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<Question Multiple Choice: {self.question_id}>"
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
def add_multiple_choice_question(question, answer, addresses, difficulty, hint, wrong_answers):
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
|
|
|
question_id = session.query(AllQuestions).count()
|
|
|
|
base_question = AllQuestions(question_id, question, answer, addresses)
|
|
|
|
multiple_choice_question = MultipleChoice(question_id, difficulty, hint, wrong_answers, base_question)
|
|
|
|
session.add(base_question)
|
|
|
|
session.add(multiple_choice_question)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_questions() -> List[AllQuestions]:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
2021-03-06 14:29:44 -08:00
|
|
|
return session.query(AllQuestions).all()
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
def get_all_hidden_answer() -> List[HiddenAnswer]:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
2021-03-06 14:29:44 -08:00
|
|
|
return session.query(HiddenAnswer).all()
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
def get_all_multiple_choice() -> List[MultipleChoice]:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
2021-03-06 14:29:44 -08:00
|
|
|
return session.query(MultipleChoice).all()
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
def get_category_count(category: Union[Type[MultipleChoice], Type[HiddenAnswer], Type[AllQuestions]]) -> int:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
2021-03-06 14:29:44 -08:00
|
|
|
return session.query(category).count()
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
def get_question(category: Union[Type[MultipleChoice], Type[HiddenAnswer], Type[AllQuestions]], question_id: int) -> Optional[Union[Type[MultipleChoice], Type[HiddenAnswer], Type[AllQuestions]]]:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
2021-03-06 14:29:44 -08:00
|
|
|
return session.query(category).filter(category.question_id == question_id).one_or_none()
|
|
|
|
|
|
|
|
|
2021-03-18 20:08:19 -07:00
|
|
|
def get_random_question_of_difficulty(category: Union[Type[MultipleChoice], Type[HiddenAnswer]], difficulty: Literal[1, 2, 3]):
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
2021-03-06 14:29:44 -08:00
|
|
|
return session.query(category).filter(category.difficulty == difficulty).order_by(func.random()).first()
|
2021-03-18 20:08:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_random_hidden_answer(difficulty: Optional[Literal[1, 2, 3]] = None) -> HiddenAnswer:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
|
|
|
if difficulty is not None:
|
|
|
|
return session.query(HiddenAnswer).filter(HiddenAnswer.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: sqlalchemy.orm.session.Session = Session()
|
|
|
|
print(type(session))
|
|
|
|
if difficulty is not None:
|
|
|
|
return session.query(MultipleChoice).filter(MultipleChoice.difficulty == difficulty).order_by(func.random()).first()
|
|
|
|
return session.query(MultipleChoice).order_by(func.random()).first()
|
|
|
|
|
|
|
|
|
|
|
|
def check_answer(question_id: int, guess: str) -> Tuple[bool, str]:
|
|
|
|
session: sqlalchemy.orm.session.Session = Session()
|
|
|
|
question: AllQuestions = session.query(AllQuestions).filter(AllQuestions.question_id == question_id).one_or_none()
|
|
|
|
if question:
|
|
|
|
answer = question.answer
|
|
|
|
return answer == guess, answer
|