aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2020-12-17 09:38:13 +0300
committerGravatar Hassan Abouelela <[email protected]>2020-12-17 09:38:13 +0300
commitd2427c7d3c3a1e877e99a724106b8d01d356e84c (patch)
treecb35783fb39c8a96c834c6835054bca2e3a2a159
parentAdds Timestamp Field (diff)
Fixes DB Timestamp on Return
As pointed out by @ks129, fetching the responses would cause an error in the timestamp, as the validation was returning the current time at fetching, instead of the stored value. Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r--SCHEMA.md15
-rw-r--r--backend/models/form_response.py15
-rw-r--r--backend/routes/forms/submit.py2
3 files changed, 21 insertions, 11 deletions
diff --git a/SCHEMA.md b/SCHEMA.md
index b37ed1d..59b6c33 100644
--- a/SCHEMA.md
+++ b/SCHEMA.md
@@ -127,13 +127,14 @@ Textareas require no additional configuration.
## Form response
-| Field | Type | Description |
-| ---------- | ---------------------------------------------------- | --------------------------------------------------------------------------- |
-| `_id`/`id` | MongoDB ObjectID | Random identifier used for the response |
-| `user` | Optional [user details object](#user-details-object) | An object describing the user that submitted if the form is not anonymous |
-| `antispam` | Optional [anti spam object](#anti-spam-object) | An object containing information about the anti-spam on the form submission |
-| `response` | Object | Object containing question IDs mapping to the users answer |
-| `form_id` | String | ID of the form that the user is submitting to |
+| Field | Type | Description |
+| ----------- | ---------------------------------------------------- | --------------------------------------------------------------------------- |
+| `_id`/`id` | MongoDB ObjectID | Random identifier used for the response |
+| `user` | Optional [user details object](#user-details-object) | An object describing the user that submitted if the form is not anonymous |
+| `antispam` | Optional [anti spam object](#anti-spam-object) | An object containing information about the anti-spam on the form submission |
+| `response` | Object | Object containing question IDs mapping to the users answer |
+| `form_id` | String | ID of the form that the user is submitting to |
+| `timestamp` | String | ISO formatted string of submission time. |
### User details object
diff --git a/backend/models/form_response.py b/backend/models/form_response.py
index b6570e5..8a4da28 100644
--- a/backend/models/form_response.py
+++ b/backend/models/form_response.py
@@ -15,11 +15,18 @@ class FormResponse(BaseModel):
antispam: t.Optional[AntiSpam]
response: dict[str, t.Any]
form_id: str
- timestamp: str = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
+ timestamp: str
- @validator("timestamp")
- def set_timestamp(cls, _: str) -> str:
- return datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
+ @validator("timestamp", pre=True)
+ def set_timestamp(cls, iso_string: str) -> str:
+ if iso_string is None:
+ return datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
+
+ elif not isinstance(iso_string, str):
+ raise ValueError("Submission timestamp must be a string.")
+
+ # Convert to datetime and back to ensure string is valid
+ return datetime.datetime.fromisoformat(iso_string).isoformat()
class Config:
allow_population_by_field_name = True
diff --git a/backend/routes/forms/submit.py b/backend/routes/forms/submit.py
index 3ecbda0..48ae4f6 100644
--- a/backend/routes/forms/submit.py
+++ b/backend/routes/forms/submit.py
@@ -34,6 +34,8 @@ class SubmitForm(Route):
async def post(self, request: Request) -> JSONResponse:
data = await request.json()
+ data["timestamp"] = None
+
if form := await request.state.db.forms.find_one(
{"_id": request.path_params["form_id"], "features": "OPEN"}
):