Skip to content

Commit ae2d6cc

Browse files
committed
Move unittest filtering to the Form.dict() function
1 parent 03afb94 commit ae2d6cc

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

backend/models/form.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,19 @@ def validate_role(cls, values: dict[str, t.Any]) -> dict[str, t.Any]:
9494
def dict(self, admin: bool = True, **kwargs) -> dict[str, t.Any]: # noqa: FBT001, FBT002
9595
"""Wrapper for original function to exclude private data for public access."""
9696
data = super().dict(**kwargs)
97+
if admin:
98+
return data
9799

98100
returned_data = {}
99101

100-
if not admin:
101-
for field in PUBLIC_FIELDS:
102-
fetch_field = "_id" if field == "id" and kwargs.get("by_alias") else field
103-
104-
returned_data[field] = data[fetch_field]
105-
else:
106-
returned_data = data
102+
for field in PUBLIC_FIELDS:
103+
fetch_field = "_id" if field == "id" and kwargs.get("by_alias") else field
104+
returned_data[field] = data[fetch_field]
107105

106+
# Replace the unittest data section of code questions with the number of test cases.
107+
for question in returned_data["questions"]:
108+
if question["type"] == "code" and question["data"]["unittests"] is not None:
109+
question["data"]["unittests"]["tests"] = len(question["data"]["unittests"]["tests"])
108110
return returned_data
109111

110112

backend/routes/forms/form.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from backend.models import Form
1414
from backend.route import Route
1515
from backend.routes.forms.discover import AUTH_FORM
16-
from backend.routes.forms.unittesting import filter_unittests
1716
from backend.validation import ErrorMessage, OkayResponse, api
1817

1918
PUBLIC_FORM_FEATURES = (constants.FormFeatures.OPEN, constants.FormFeatures.DISCOVERABLE)
@@ -57,11 +56,7 @@ async def get(self, request: Request) -> JSONResponse:
5756
if not form:
5857
return JSONResponse({"error": "not_found"}, status_code=404)
5958

60-
form = Form(**form)
61-
if not admin:
62-
form = filter_unittests(form)
63-
64-
return JSONResponse(form.dict(admin=admin))
59+
return JSONResponse(Form(**form).dict(admin=admin))
6560

6661
@requires(["authenticated"])
6762
@api.validate(

backend/routes/forms/unittesting.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ class UnittestResult(NamedTuple):
2626
result: str
2727

2828

29-
def filter_unittests(form: Form) -> Form:
30-
"""
31-
Replace the unittest data section of code questions with the number of test cases.
32-
33-
This is used to redact the exact tests when sending the form back to the frontend.
34-
"""
35-
for question in form.questions:
36-
if question.type == "code" and question.data["unittests"] is not None:
37-
question.data["unittests"]["tests"] = len(question.data["unittests"]["tests"])
38-
39-
return form
40-
41-
4229
def _make_unit_code(units: dict[str, str]) -> str:
4330
"""Compose a dict mapping unit names to their code into an actual class body."""
4431
result = ""

0 commit comments

Comments
 (0)