Normalize UTC timezone on all the entity timestamps

This commit is contained in:
Fabio Manganiello 2022-04-07 01:13:29 +02:00
parent 948f37afd4
commit 44707731a8
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,7 +1,7 @@
import inspect import inspect
import pathlib import pathlib
from datetime import datetime from datetime import datetime
from typing import Mapping, Type, Tuple from typing import Mapping, Type, Tuple, Any
import pkgutil import pkgutil
from sqlalchemy import ( from sqlalchemy import (
@ -57,8 +57,16 @@ class Entity(Base):
inspector = schema_inspect(cls) inspector = schema_inspect(cls)
return tuple(inspector.mapper.column_attrs) return tuple(inspector.mapper.column_attrs)
def _serialize_value(self, col: ColumnProperty) -> Any:
val = getattr(self, col.key)
if isinstance(val, datetime):
# All entity timestamps are in UTC
val = val.isoformat() + '+00:00'
return val
def to_json(self) -> dict: def to_json(self) -> dict:
return {col.key: getattr(self, col.key) for col in self.columns} return {col.key: self._serialize_value(col) for col in self.columns}
def get_plugin(self): def get_plugin(self):
from platypush.context import get_plugin from platypush.context import get_plugin