aboutsummaryrefslogtreecommitdiffstats
path: root/backend/routes/forms/index.py
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2020-12-13 19:00:05 +0000
committerGravatar GitHub <[email protected]>2020-12-13 19:00:05 +0000
commit7c29f883996105bf7defa252a9b4428fd7e7c161 (patch)
tree7e99558e1aa4ff35628b8acc365f1cda7f192594 /backend/routes/forms/index.py
parentMerge pull request #18 from python-discord/ks123/form-name-description (diff)
parentMake error message more user friendly (diff)
Merge pull request #23 from python-discord/ks123/form-endpoint
Diffstat (limited to 'backend/routes/forms/index.py')
-rw-r--r--backend/routes/forms/index.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/backend/routes/forms/index.py b/backend/routes/forms/index.py
index f1df210..bb2b299 100644
--- a/backend/routes/forms/index.py
+++ b/backend/routes/forms/index.py
@@ -1,6 +1,7 @@
"""
Return a list of all forms to authenticated users.
"""
+from pydantic import ValidationError
from starlette.authentication import requires
from starlette.requests import Request
from starlette.responses import JSONResponse
@@ -14,7 +15,7 @@ class FormsList(Route):
List all available forms for administrator viewing.
"""
- name = "forms_list"
+ name = "forms_list_create"
path = "/"
@requires(["authenticated", "admin"])
@@ -31,3 +32,19 @@ class FormsList(Route):
return JSONResponse(
forms
)
+
+ @requires(["authenticated", "admin"])
+ async def post(self, request: Request) -> JSONResponse:
+ form_data = await request.json()
+ try:
+ form = Form(**form_data)
+ except ValidationError as e:
+ return JSONResponse(e.errors())
+
+ if await request.state.db.forms.find_one({"_id": form.id}):
+ return JSONResponse({
+ "error": "id_taken"
+ }, status_code=400)
+
+ await request.state.db.forms.insert_one(form.dict(by_alias=True))
+ return JSONResponse(form.dict())