blob: 0144074b54510d088749356dee158952f7aa78d0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
"""
Creates new form based on data provided.
"""
from pydantic import ValidationError
from starlette.authentication import requires
from starlette.requests import Request
from starlette.responses import JSONResponse
from backend.models import Form
from backend.route import Route
class FormCreate(Route):
"""
Creates new form from JSON data.
"""
name = "forms_create"
path = "/"
@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": "Form with same ID already exists."
}, status_code=400)
await request.state.db.forms.insert_one(form.dict(by_alias=True))
return JSONResponse(form.dict())
|