From b73dd5005e007ee998d495bfedcee4cf3fc453a8 Mon Sep 17 00:00:00 2001 From: Harald Mack Date: Wed, 6 Nov 2024 16:13:12 +0100 Subject: [PATCH] unify class hierarchy of question, answer models --- .../src/mondey_backend/models/questions.py | 118 +++++------------- 1 file changed, 34 insertions(+), 84 deletions(-) diff --git a/mondey_backend/src/mondey_backend/models/questions.py b/mondey_backend/src/mondey_backend/models/questions.py index 691006b..3cf9df4 100644 --- a/mondey_backend/src/mondey_backend/models/questions.py +++ b/mondey_backend/src/mondey_backend/models/questions.py @@ -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): @@ -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 @@ -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