Entity objects are now JSON-able

This commit is contained in:
Fabio Manganiello 2022-04-07 00:21:54 +02:00
parent 26ffc0b0e1
commit 2eeb1d4fea
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -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