diff options
| author | 2020-12-17 09:38:13 +0300 | |
|---|---|---|
| committer | 2020-12-17 09:38:13 +0300 | |
| commit | d2427c7d3c3a1e877e99a724106b8d01d356e84c (patch) | |
| tree | cb35783fb39c8a96c834c6835054bca2e3a2a159 /backend/models | |
| parent | Adds 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]>
Diffstat (limited to 'backend/models')
| -rw-r--r-- | backend/models/form_response.py | 15 | 
1 files changed, 11 insertions, 4 deletions
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  |