Skip to content

Commit fe94892

Browse files
Merge branch 'main' into asgi-middleware
2 parents 5d0da6b + 221ccd6 commit fe94892

6 files changed

Lines changed: 48 additions & 47 deletions

File tree

backend/discord.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def _verify_access_helper(
175175

176176
form = models.Form(**form)
177177

178-
for role_id in getattr(form, attribute, []):
178+
for role_id in getattr(form, attribute, None) or []:
179179
role = await request.state.db.roles.find_one({"id": role_id})
180180
if not role:
181181
continue

backend/routes/forms/discover.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@
1111
from backend.validation import api
1212

1313
__FEATURES = [
14-
constants.FormFeatures.DISCOVERABLE.value,
1514
constants.FormFeatures.OPEN.value,
1615
constants.FormFeatures.REQUIRES_LOGIN.value
1716
]
17+
if not constants.PRODUCTION:
18+
__FEATURES.append(constants.FormFeatures.DISCOVERABLE.value)
1819

1920
__QUESTION = Question(
2021
id="description",
21-
name="Check your cookies after pressing the button.",
22+
name="Click the button below to log into the forms application.",
2223
type="section",
23-
data={"text": "You can find cookies under \"Application\" in dev tools."},
24+
data={"text": ""},
2425
required=False
2526
)
2627

27-
EMPTY_FORM = Form(
28-
id="empty_auth",
28+
AUTH_FORM = Form(
29+
id="login",
2930
features=__FEATURES,
3031
questions=[__QUESTION],
31-
name="Auth form",
32-
description="An empty form to help you get a token.",
32+
name="Login",
33+
description="Log into Python Discord Forms.",
34+
submitted_text="This page can't be submitted."
3335
)
3436

3537

@@ -55,7 +57,7 @@ async def get(self, request: Request) -> JSONResponse:
5557
forms = [form.dict(admin=False) for form in forms]
5658

5759
# Return an empty form in development environments to help with authentication.
58-
if not forms and not constants.PRODUCTION:
59-
forms.append(EMPTY_FORM.dict(admin=False))
60+
if not constants.PRODUCTION:
61+
forms.append(AUTH_FORM.dict(admin=False))
6062

6163
return JSONResponse(forms)

backend/routes/forms/form.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from backend import constants, discord
1414
from backend.models import Form
1515
from backend.route import Route
16-
from backend.routes.forms.discover import EMPTY_FORM
16+
from backend.routes.forms.discover import AUTH_FORM
1717
from backend.routes.forms.unittesting import filter_unittests
1818
from backend.validation import ErrorMessage, OkayResponse, api
1919

@@ -35,14 +35,15 @@ async def get(self, request: Request) -> JSONResponse:
3535
"""Returns single form information by ID."""
3636
form_id = request.path_params["form_id"].lower()
3737

38+
if form_id == AUTH_FORM.id:
39+
# Empty form for login purposes
40+
return JSONResponse(AUTH_FORM.dict(admin=False))
41+
3842
try:
3943
await discord.verify_edit_access(form_id, request)
4044
admin = True
4145
except discord.FormNotFoundError:
42-
if not constants.PRODUCTION and form_id == EMPTY_FORM.id:
43-
# Empty form to help with authentication in development.
44-
return JSONResponse(EMPTY_FORM.dict(admin=False))
45-
raise
46+
return JSONResponse({"error": "not_found"}, status_code=404)
4647
except discord.UnauthorizedError:
4748
admin = False
4849

@@ -53,7 +54,11 @@ async def get(self, request: Request) -> JSONResponse:
5354
if not admin:
5455
filters["features"] = {"$in": ["OPEN", "DISCOVERABLE"]}
5556

56-
form = Form(**await request.state.db.forms.find_one(filters))
57+
form = await request.state.db.forms.find_one(filters)
58+
if not form:
59+
return JSONResponse({"error": "not_found"}, status_code=404)
60+
61+
form = Form(**form)
5762
if not admin:
5863
form = filter_unittests(form)
5964

backend/routes/forms/submit.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from backend.models import Form, FormResponse
2323
from backend.route import Route
2424
from backend.routes.auth.authorize import set_response_token
25+
from backend.routes.forms.discover import AUTH_FORM
2526
from backend.routes.forms.unittesting import execute_unittest
2627
from backend.validation import ErrorMessage, api
2728

@@ -106,9 +107,18 @@ async def submit(self, request: Request) -> JSONResponse:
106107
data = await request.json()
107108
data["timestamp"] = None
108109

109-
if form := await request.state.db.forms.find_one(
110-
{"_id": request.path_params["form_id"], "features": "OPEN"}
111-
):
110+
form_id = request.path_params["form_id"]
111+
112+
if form_id == AUTH_FORM.id:
113+
response = FormResponse(
114+
id="not-submitted",
115+
form_id=AUTH_FORM.id,
116+
response={question.id: None for question in AUTH_FORM.questions},
117+
timestamp=datetime.datetime.now().isoformat()
118+
).dict()
119+
return JSONResponse({"form": AUTH_FORM.dict(admin=False), "response": response})
120+
121+
if form := await request.state.db.forms.find_one({"_id": form_id, "features": "OPEN"}):
112122
form = Form(**form)
113123
response = data.copy()
114124
response["id"] = str(uuid.uuid4())

poetry.lock

Lines changed: 11 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ uvicorn = {extras = ["standard"], version = "^0.17.6"}
1313
motor = "^2.4.0"
1414
python-dotenv = "^0.19.2"
1515
pyjwt = "^2.4.0"
16-
httpx = "^0.22.0"
16+
httpx = "^0.23.0"
1717
gunicorn = "^20.1.0"
1818
pydantic = "^1.8.2"
1919
spectree = "^0.7.6"

0 commit comments

Comments
 (0)