From d2427c7d3c3a1e877e99a724106b8d01d356e84c Mon Sep 17 00:00:00 2001 From: Hassan Abouelela <47495861+HassanAbouelela@users.noreply.github.com> Date: Thu, 17 Dec 2020 09:38:13 +0300 Subject: 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 <47495861+HassanAbouelela@users.noreply.github.com> --- backend/models/form_response.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'backend/models') 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 -- cgit v1.2.3