Skip to content

Commit

Permalink
unify class hierarchy of question, answer models
Browse files Browse the repository at this point in the history
  • Loading branch information
MaHaWo committed Nov 6, 2024
1 parent 3eebee9 commit b73dd50
Showing 1 changed file with 34 additions and 84 deletions.
118 changes: 34 additions & 84 deletions mondey_backend/src/mondey_backend/models/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
class QuestionTextBase(SQLModel):
question: str = ""
options_json: str = ""
options: str = ""


class UserQuestionText(QuestionTextBase, table=True):
user_question_id: int | None = Field(
default=None, foreign_key="userquestion.id", primary_key=True
)
lang_id: str | None = fixed_length_string_field(
max_length=2, default=None, foreign_key="language.id", primary_key=True
)
class Question(SQLModel):
order: int = 0
component: str = "select"
type: str = "text"
options: str = ""
additional_option: str = ""


class QuestionAdmin(Question):
id: int


class QuestionTextPublic(QuestionTextBase):
Expand All @@ -36,28 +39,26 @@ class QuestionPublic(SQLModel):
additional_option: str = ""


class UserQuestion(SQLModel, table=True):
class UserQuestionText(QuestionTextBase, table=True):
user_question_id: int | None = Field(
default=None, foreign_key="userquestion.id", primary_key=True
)
lang_id: str | None = fixed_length_string_field(
max_length=2, default=None, foreign_key="language.id", primary_key=True
)


class UserQuestion(Question, table=True):
id: int | None = Field(default=None, primary_key=True)
order: int = 0
component: str = "select"
type: str = "text"
options: str = ""
text: Mapped[dict[str, UserQuestionText]] = dict_relationship(key="lang_id")
additional_option: str = ""


class UserQuestionPublic(QuestionPublic):
pass


class UserQuestionAdmin(SQLModel):
id: int
order: int
component: str = "select"
type: str = "text"
options: str
class UserQuestionAdmin(QuestionAdmin):
text: dict[str, UserQuestionText] = {}
additional_option: str = ""


# child questions
Expand All @@ -70,100 +71,49 @@ class ChildQuestionText(QuestionTextBase, table=True):
lang_id: str | None = fixed_length_string_field(
max_length=2, default=None, foreign_key="language.id", primary_key=True
)
options: str = ""


class ChildQuestion(SQLModel, table=True):
class ChildQuestion(Question, table=True):
id: int | None = Field(default=None, primary_key=True)
order: int = 0
component: str = "select"
type: str = "text"
options: str = ""
text: Mapped[dict[str, ChildQuestionText]] = dict_relationship(key="lang_id")
additional_option: str = ""


class ChildQuestionPublic(QuestionPublic):
pass


class ChildQuestionAdmin(SQLModel):
id: int
order: int
component: str = "select"
type: str = "text"
options: str
class ChildQuestionAdmin(QuestionAdmin):
text: dict[str, ChildQuestionText] = {}
additional_option: str = ""


# Answers to user questions. Internal model and 'public' model exposed to the forntend app
class AnswerBase(SQLModel):
answer: str
additional_answer: str | None


class UserAnswer(SQLModel, table=True):
"""
Internal model for user answers.
Parameters
----------
SQLModel : Pydantic model basic sqlmodel pydantic type
class AnswerPublicBase(AnswerBase):
question_id: int

table : bool, True
Makes sure this is created as a table in the database, by default True
"""

class UserAnswer(AnswerBase, table=True):
user_id: int = Field(default=None, primary_key=True)
question_id: int = Field(
default=None, primary_key=True, foreign_key="userquestion.id"
)
answer: str
additional_answer: str | None


class UserAnswerPublic(SQLModel):
"""
External data model for UserAnswers

Parameters
----------
SQLModel : Pydantic model basic sqlmodel pydantic type
"""

answer: str
question_id: int
additional_answer: str | None


class ChildAnswer(SQLModel, table=True):
"""
Internal model for user answers.
Parameters
----------
SQLModel : Pydantic model basic sqlmodel pydantic type
class UserAnswerPublic(AnswerPublicBase):
pass

table : bool, True
Makes sure this is created as a table in the database, by default True
"""

class ChildAnswer(AnswerBase, table=True):
user_id: int = Field(default=None, primary_key=True)
child_id: int = Field(default=None, primary_key=True)
question_id: int = Field(
default=None, primary_key=True, foreign_key="childquestion.id"
)
answer: str
additional_answer: str | None


class ChildAnswerPublic(SQLModel):
"""
External data model for UserAnswers

Parameters
----------
SQLModel : Pydantic model basic sqlmodel pydantic type
"""

answer: str
question_id: int
additional_answer: str | None
class ChildAnswerPublic(AnswerPublicBase):
pass

0 comments on commit b73dd50

Please sign in to comment.