Implement BaseModel class, TimestampMixin, TzDateTime type; cleanup Task model

This commit is contained in:
Lexi / Zoe 2022-04-15 21:45:10 +02:00
parent 7ce43a2bfe
commit 252c15acbd
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
15 changed files with 223 additions and 42 deletions

View 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)