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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
"""Return a list of all publicly discoverable forms to unauthenticated users."""
from spectree.response import Response
from starlette.requests import Request
from starlette.responses import JSONResponse
from backend import constants
from backend.models import Form, FormList, Question
from backend.route import Route
from backend.validation import api
__FEATURES = [
constants.FormFeatures.OPEN.value,
constants.FormFeatures.REQUIRES_LOGIN.value,
]
if not constants.PRODUCTION:
__FEATURES.append(constants.FormFeatures.DISCOVERABLE.value)
__QUESTION = Question(
id="description",
name="Click the button below to log into the forms application.",
type="section",
data={"text": ""},
required=False,
)
AUTH_FORM = Form(
id="login",
features=__FEATURES,
questions=[__QUESTION],
name="Login",
description="Log into Python Discord Forms.",
submitted_text="This page can't be submitted.",
)
class DiscoverableFormsList(Route):
"""List all discoverable forms that should be shown on the homepage."""
name = "discoverable_forms_list"
path = "/discoverable"
@api.validate(resp=Response(HTTP_200=FormList), tags=["forms"])
async def get(self, request: Request) -> JSONResponse:
"""List all discoverable forms that should be shown on the homepage."""
cursor = request.state.db.forms.find({"features": "DISCOVERABLE"}).sort("name")
# Parse it to Form and then back to dictionary
# to replace _id with id
forms = [Form(**form).dict(admin=False) for form in await cursor.to_list(None)]
# Return an empty form in development environments to help with authentication.
if not constants.PRODUCTION:
forms.append(AUTH_FORM.dict(admin=False))
return JSONResponse(forms)
|