diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index 2bc102b570..5e0ee423b4 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -16,13 +16,15 @@ from sqlalchemy import ( ) from sqlalchemy.orm import declarative_base, ColumnProperty +from platypush.message import JSONAble + Base = declarative_base() entities_registry: Mapping[Type['Entity'], Mapping] = {} class Entity(Base): """ - Model for a general-purpose platform entity + Model for a general-purpose platform entity. """ __tablename__ = 'entity' @@ -55,6 +57,9 @@ class Entity(Base): inspector = schema_inspect(cls) return tuple(inspector.mapper.column_attrs) + def to_json(self) -> dict: + return {col.key: getattr(self, col.key) for col in self.columns} + def get_plugin(self): from platypush.context import get_plugin @@ -63,6 +68,11 @@ class Entity(Base): return plugin +# Inject the JSONAble mixin (Python goes nuts if done through +# standard multiple inheritance with an SQLAlchemy ORM class) +Entity.__bases__ = Entity.__bases__ + (JSONAble,) + + def _discover_entity_types(): from platypush.context import get_plugin @@ -88,6 +98,10 @@ def _discover_entity_types(): entities_registry[obj] = {} +def get_entities_registry(): + return entities_registry.copy() + + def init_entities_db(): from platypush.context import get_plugin