diff options
author | 2024-07-09 18:56:25 +0100 | |
---|---|---|
committer | 2024-07-09 19:01:28 +0100 | |
commit | ae2d6ccac9d3c4d5181347e016660216212b3035 (patch) | |
tree | f58690867087ca26c45670d34d100a83e2233c47 | |
parent | Document timezone field in SCHEMA.md (diff) |
Move unittest filtering to the Form.dict() function
-rw-r--r-- | backend/models/form.py | 16 | ||||
-rw-r--r-- | backend/routes/forms/form.py | 7 | ||||
-rw-r--r-- | backend/routes/forms/unittesting.py | 13 |
3 files changed, 10 insertions, 26 deletions
diff --git a/backend/models/form.py b/backend/models/form.py index 3db267e..739464e 100644 --- a/backend/models/form.py +++ b/backend/models/form.py @@ -94,17 +94,19 @@ class Form(BaseModel): def dict(self, admin: bool = True, **kwargs) -> dict[str, t.Any]: # noqa: FBT001, FBT002 """Wrapper for original function to exclude private data for public access.""" data = super().dict(**kwargs) + if admin: + return data returned_data = {} - if not admin: - for field in PUBLIC_FIELDS: - fetch_field = "_id" if field == "id" and kwargs.get("by_alias") else field - - returned_data[field] = data[fetch_field] - else: - returned_data = data + for field in PUBLIC_FIELDS: + fetch_field = "_id" if field == "id" and kwargs.get("by_alias") else field + returned_data[field] = data[fetch_field] + # Replace the unittest data section of code questions with the number of test cases. + for question in returned_data["questions"]: + if question["type"] == "code" and question["data"]["unittests"] is not None: + question["data"]["unittests"]["tests"] = len(question["data"]["unittests"]["tests"]) return returned_data diff --git a/backend/routes/forms/form.py b/backend/routes/forms/form.py index 410102a..86bbf49 100644 --- a/backend/routes/forms/form.py +++ b/backend/routes/forms/form.py @@ -13,7 +13,6 @@ from backend import constants, discord from backend.models import Form from backend.route import Route from backend.routes.forms.discover import AUTH_FORM -from backend.routes.forms.unittesting import filter_unittests from backend.validation import ErrorMessage, OkayResponse, api PUBLIC_FORM_FEATURES = (constants.FormFeatures.OPEN, constants.FormFeatures.DISCOVERABLE) @@ -57,11 +56,7 @@ class SingleForm(Route): if not form: return JSONResponse({"error": "not_found"}, status_code=404) - form = Form(**form) - if not admin: - form = filter_unittests(form) - - return JSONResponse(form.dict(admin=admin)) + return JSONResponse(Form(**form).dict(admin=admin)) @requires(["authenticated"]) @api.validate( diff --git a/backend/routes/forms/unittesting.py b/backend/routes/forms/unittesting.py index 3239d35..57c3a86 100644 --- a/backend/routes/forms/unittesting.py +++ b/backend/routes/forms/unittesting.py @@ -26,19 +26,6 @@ class UnittestResult(NamedTuple): result: str -def filter_unittests(form: Form) -> Form: - """ - Replace the unittest data section of code questions with the number of test cases. - - This is used to redact the exact tests when sending the form back to the frontend. - """ - for question in form.questions: - if question.type == "code" and question.data["unittests"] is not None: - question.data["unittests"]["tests"] = len(question.data["unittests"]["tests"]) - - return form - - def _make_unit_code(units: dict[str, str]) -> str: """Compose a dict mapping unit names to their code into an actual class body.""" result = "" |