Scale question type

This commit is contained in:
grey-cat-1908 2024-08-20 06:45:07 +00:00
parent a40cdb61e3
commit 14f64a2817
2 changed files with 37 additions and 2 deletions

View file

@ -18,6 +18,7 @@ class AnswerError(Enum):
INCORRECT_IDS = "The ids for some questions are incorrect." INCORRECT_IDS = "The ids for some questions are incorrect."
TIN_VALIDATION_FAILED = "The TIN validation process failed." TIN_VALIDATION_FAILED = "The TIN validation process failed."
SNILS_VALIDATION_FAILED = "The SNILS validation process failed." SNILS_VALIDATION_FAILED = "The SNILS validation process failed."
SCALE_VALUE_NOT_IN_RANGE = "The scale value must be within the specified range."
class BaseValue(BaseModel): class BaseValue(BaseModel):
@ -51,6 +52,15 @@ class TextValue(BaseValue):
raise ValueError(AnswerError.SNILS_VALIDATION_FAILED.value) raise ValueError(AnswerError.SNILS_VALIDATION_FAILED.value)
class ScaleValue(BaseValue):
question_type: form.QuestionType = form.QuestionType.scale
value: int
def validate(self, question: form.ScaleQuestion) -> None:
if not (question.min_value <= self.value <= question.max_value):
raise ValueError(AnswerError.SCALE_VALUE_NOT_IN_RANGE.value)
class SelectorValue(BaseValue): class SelectorValue(BaseValue):
question_type: form.QuestionType = form.QuestionType.selector question_type: form.QuestionType = form.QuestionType.selector
values: set[int] values: set[int]
@ -69,7 +79,7 @@ class SelectorValue(BaseValue):
raise ValueError(AnswerError.TOO_MANY_SELECTED.value) raise ValueError(AnswerError.TOO_MANY_SELECTED.value)
Value: TypeAlias = SelectorValue | TextValue Value: TypeAlias = SelectorValue | TextValue | ScaleValue
class AnswerData(BaseModel): class AnswerData(BaseModel):

View file

@ -14,11 +14,14 @@ class FormError(Enum):
MIN_VALUES_ERR = "min_values must be greater than or equal to 1." MIN_VALUES_ERR = "min_values must be greater than or equal to 1."
MAX_VALUES_ERR = "max_values cannot be less than min_length or greater than the number of options." MAX_VALUES_ERR = "max_values cannot be less than min_length or greater than the number of options."
SIMMILAR_ID_ERR = "All questions must have different id's" SIMMILAR_ID_ERR = "All questions must have different id's"
SCALE_MIN_VALUE_ERR = "min_value must be in range from 0 to 1"
SCALE_MAX_VALUE_ERR = "max_value must be in range from 2 to 10"
class QuestionType(IntEnum): class QuestionType(IntEnum):
text = 1 text = 1
selector = 2 selector = 2
scale = 3
class TextValidator(IntEnum): class TextValidator(IntEnum):
@ -76,6 +79,28 @@ class TextQuestion(BaseQuestion):
return v return v
class ScaleQuestion(BaseQuestion):
question_type: QuestionType = QuestionType.scale
min_value: int
min_label: str | None = None
max_value: int
max_label: str | None = None
@field_validator("min_value")
@classmethod
def validate_min_value(cls, v, info):
if v not in [0, 1]:
raise ValueError(FormError.SCALE_MIN_VALUE_ERR.value)
return v
@field_validator("max_value")
@classmethod
def validate_max_value(cls, v, info):
if v < 2 or v > 10:
raise ValueError(FormError.SCALE_MAX_VALUE_ERR.value)
return v
class SelectorQuestion(BaseQuestion): class SelectorQuestion(BaseQuestion):
question_type: QuestionType = QuestionType.selector question_type: QuestionType = QuestionType.selector
min_values: int = 1 min_values: int = 1
@ -101,7 +126,7 @@ class SelectorQuestion(BaseQuestion):
return v return v
Question: TypeAlias = SelectorQuestion | TextQuestion Question: TypeAlias = SelectorQuestion | TextQuestion | ScaleQuestion
class Page(BaseModel): class Page(BaseModel):