From ae2d6ccac9d3c4d5181347e016660216212b3035 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 9 Jul 2024 18:56:25 +0100 Subject: Move unittest filtering to the Form.dict() function --- backend/models/form.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'backend/models/form.py') 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 -- cgit v1.2.3