forked from platypush/platypush
Entity objects are now JSON-able
This commit is contained in:
parent
26ffc0b0e1
commit
2eeb1d4fea
1 changed files with 15 additions and 1 deletions
|
@ -16,13 +16,15 @@ from sqlalchemy import (
|
||||||
)
|
)
|
||||||
from sqlalchemy.orm import declarative_base, ColumnProperty
|
from sqlalchemy.orm import declarative_base, ColumnProperty
|
||||||
|
|
||||||
|
from platypush.message import JSONAble
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
entities_registry: Mapping[Type['Entity'], Mapping] = {}
|
entities_registry: Mapping[Type['Entity'], Mapping] = {}
|
||||||
|
|
||||||
|
|
||||||
class Entity(Base):
|
class Entity(Base):
|
||||||
"""
|
"""
|
||||||
Model for a general-purpose platform entity
|
Model for a general-purpose platform entity.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = 'entity'
|
__tablename__ = 'entity'
|
||||||
|
@ -55,6 +57,9 @@ class Entity(Base):
|
||||||
inspector = schema_inspect(cls)
|
inspector = schema_inspect(cls)
|
||||||
return tuple(inspector.mapper.column_attrs)
|
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):
|
def get_plugin(self):
|
||||||
from platypush.context import get_plugin
|
from platypush.context import get_plugin
|
||||||
|
|
||||||
|
@ -63,6 +68,11 @@ class Entity(Base):
|
||||||
return plugin
|
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():
|
def _discover_entity_types():
|
||||||
from platypush.context import get_plugin
|
from platypush.context import get_plugin
|
||||||
|
|
||||||
|
@ -88,6 +98,10 @@ def _discover_entity_types():
|
||||||
entities_registry[obj] = {}
|
entities_registry[obj] = {}
|
||||||
|
|
||||||
|
|
||||||
|
def get_entities_registry():
|
||||||
|
return entities_registry.copy()
|
||||||
|
|
||||||
|
|
||||||
def init_entities_db():
|
def init_entities_db():
|
||||||
from platypush.context import get_plugin
|
from platypush.context import get_plugin
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue