diff options
author | 2020-12-13 15:29:26 +0200 | |
---|---|---|
committer | 2020-12-13 15:29:26 +0200 | |
commit | 5948acaba542b8a1347c4c039635ad4a8066d2d8 (patch) | |
tree | ae279ead1e6f259e30c01261730e69b73805316e | |
parent | Merge pull request #18 from python-discord/ks123/form-name-description (diff) |
Add custom dict method in form to return sometimes only public fields
-rw-r--r-- | backend/models/form.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/backend/models/form.py b/backend/models/form.py index 8d7fe9b..6b3c8bb 100644 --- a/backend/models/form.py +++ b/backend/models/form.py @@ -5,6 +5,8 @@ from pydantic import BaseModel, Field, validator from backend.constants import FormFeatures from .question import Question +PUBLIC_FIELDS = ["id", "features", "questions", "name", "description"] + class Form(BaseModel): """Schema model for form.""" @@ -31,3 +33,12 @@ class Form(BaseModel): raise ValueError("COLLECT_EMAIL feature require REQUIRES_LOGIN feature.") return value + + def dict(self, admin: bool = True, **kwargs: t.Dict) -> t.Dict[str, t.Any]: + """Wrapper for original function to exclude private data for public access.""" + data = super().dict(**kwargs) + + if admin: + return data + + return {field: data[field] for field in PUBLIC_FIELDS} |