Implement BaseModel class, TimestampMixin, TzDateTime type; cleanup Task model
This commit is contained in:
parent
7ce43a2bfe
commit
252c15acbd
15 changed files with 223 additions and 42 deletions
29
tofu_api/common/json/json_encoder.py
Normal file
29
tofu_api/common/json/json_encoder.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from flask.json import JSONEncoder as _FlaskJSONEncoder
|
||||
|
||||
__all__ = [
|
||||
'JSONEncoder',
|
||||
]
|
||||
|
||||
|
||||
class JSONEncoder(_FlaskJSONEncoder):
|
||||
"""
|
||||
Custom JSON encoder built on top of the Flask JSONEncoder class.
|
||||
"""
|
||||
|
||||
def default(self, obj: Any) -> Any:
|
||||
"""
|
||||
Convert any object to a JSON serializable type.
|
||||
"""
|
||||
# Convert datetimes to ISO format without microseconds (e.g. '2022-01-02T10:20:30+00:00')
|
||||
if isinstance(obj, datetime):
|
||||
return obj.isoformat(timespec='seconds')
|
||||
|
||||
# Use to_dict() method on objects that have it
|
||||
if hasattr(obj, 'to_dict'):
|
||||
return obj.to_dict()
|
||||
|
||||
# Fallback to the Flask JSONEncoder
|
||||
return super().default(obj)
|
||||
Loading…
Add table
Add a link
Reference in a new issue