aboutsummaryrefslogtreecommitdiffstats
path: root/backend/models
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2020-12-05 17:18:18 +0000
committerGravatar GitHub <[email protected]>2020-12-05 17:18:18 +0000
commite1a628bf579d74cbd6923ea42a91dd00ff5e4e05 (patch)
tree888b9b093f825cf8afc25f3494482b2ad61a1db2 /backend/models
parentCreate review-policy.yml (diff)
parentCreate route for creating new forms (diff)
Merge pull request #13 from python-discord/ks123/forms-routes
Diffstat (limited to 'backend/models')
-rw-r--r--backend/models/form.py10
-rw-r--r--backend/models/question.py31
2 files changed, 21 insertions, 20 deletions
diff --git a/backend/models/form.py b/backend/models/form.py
index d0f0a3c..a8c5f92 100644
--- a/backend/models/form.py
+++ b/backend/models/form.py
@@ -3,7 +3,7 @@ import typing as t
from pydantic import BaseModel, Field, validator
from backend.constants import FormFeatures
-from backend.models import Question
+from .question import Question
class Form(BaseModel):
@@ -13,12 +13,16 @@ class Form(BaseModel):
features: t.List[str]
questions: t.List[Question]
+ class Config:
+ allow_population_by_field_name = True
+
@validator("features")
- def validate_features(self, value: t.List[str]) -> t.Optional[t.List[str]]:
+ def validate_features(cls, value: t.List[str]) -> t.Optional[t.List[str]]:
"""Validates is all features in allowed list."""
# Uppercase everything to avoid mixed case in DB
value = [v.upper() for v in value]
- if not all(v in FormFeatures.__members__.values() for v in value):
+ allowed_values = list(v.value for v in FormFeatures.__members__.values())
+ if not all(v in allowed_values for v in value):
raise ValueError("Form features list contains one or more invalid values.")
if FormFeatures.COLLECT_EMAIL in value and FormFeatures.REQUIRES_LOGIN not in value: # noqa
diff --git a/backend/models/question.py b/backend/models/question.py
index 2324a47..1a012ff 100644
--- a/backend/models/question.py
+++ b/backend/models/question.py
@@ -1,6 +1,6 @@
import typing as t
-from pydantic import BaseModel, Field, validator
+from pydantic import BaseModel, Field, root_validator, validator
from backend.constants import QUESTION_TYPES, REQUIRED_QUESTION_TYPE_DATA
@@ -13,8 +13,11 @@ class Question(BaseModel):
type: str
data: t.Dict[str, t.Any]
+ class Config:
+ allow_population_by_field_name = True
+
@validator("type", pre=True)
- def validate_question_type(self, value: str) -> t.Optional[str]:
+ def validate_question_type(cls, value: str) -> t.Optional[str]:
"""Checks if question type in currently allowed types list."""
value = value.lower()
if value not in QUESTION_TYPES:
@@ -25,30 +28,24 @@ class Question(BaseModel):
return value
- @validator("data")
+ @root_validator
def validate_question_data(
- self,
+ cls,
value: t.Dict[str, t.Any]
) -> t.Optional[t.Dict[str, t.Any]]:
"""Check does required data exists for question type and remove other data."""
# When question type don't need data, don't add anything to keep DB clean.
- if self.type not in REQUIRED_QUESTION_TYPE_DATA:
- return {}
-
- # Required keys (and values) will be stored to here
- # to remove all unnecessary stuff
- result = {}
+ if value.get("type") not in REQUIRED_QUESTION_TYPE_DATA:
+ return value
- for key, data_type in REQUIRED_QUESTION_TYPE_DATA[self.type].items():
- if key not in value:
+ for key, data_type in REQUIRED_QUESTION_TYPE_DATA[value.get("type")].items():
+ if key not in value.get("data", {}):
raise ValueError(f"Required question data key '{key}' not provided.")
- if not isinstance(value[key], data_type):
+ if not isinstance(value["data"][key], data_type):
raise ValueError(
f"Question data key '{key}' expects {data_type.__name__}, "
- f"got {type(value[key]).__name__} instead."
+ f"got {type(value['data'][key]).__name__} instead."
)
- result[key] = value[key]
-
- return result
+ return value