diff --git a/flexmeasures/api/v3_0/assets.py b/flexmeasures/api/v3_0/assets.py index d46c2af5f..54301d029 100644 --- a/flexmeasures/api/v3_0/assets.py +++ b/flexmeasures/api/v3_0/assets.py @@ -169,9 +169,6 @@ def index( search_terms=filter, filter_statement=filter_statement ) if page is None: - assets = db.session.scalars(query).all() - print("====================", assets[0].sensors_to_show) - print("====================", assets[0]) response = asset_schema.dump(db.session.scalars(query).all(), many=True) else: if per_page is None: diff --git a/flexmeasures/data/migrations/versions/950e23e3aa54_add_sensors_to_show_field_in_asset_model.py b/flexmeasures/data/migrations/versions/950e23e3aa54_add_sensors_to_show_field_in_asset_model.py index b729900e5..fccf85484 100644 --- a/flexmeasures/data/migrations/versions/950e23e3aa54_add_sensors_to_show_field_in_asset_model.py +++ b/flexmeasures/data/migrations/versions/950e23e3aa54_add_sensors_to_show_field_in_asset_model.py @@ -41,6 +41,9 @@ def upgrade(): sensors_to_show = attributes_data.get("sensors_to_show", []) + if not isinstance(sensors_to_show, list): + sensors_to_show = [sensors_to_show] + update_stmt = ( generic_asset_table.update() .where(generic_asset_table.c.id == asset_id) diff --git a/flexmeasures/data/models/generic_assets.py b/flexmeasures/data/models/generic_assets.py index 222587306..76e64656c 100644 --- a/flexmeasures/data/models/generic_assets.py +++ b/flexmeasures/data/models/generic_assets.py @@ -10,7 +10,7 @@ from sqlalchemy.engine import Row from sqlalchemy.ext.hybrid import hybrid_method from sqlalchemy.sql.expression import func, text -from sqlalchemy.ext.mutable import MutableDict +from sqlalchemy.ext.mutable import MutableDict, MutableList from timely_beliefs import BeliefsDataFrame, utils as tb_utils from flexmeasures.data import db @@ -23,7 +23,7 @@ from flexmeasures.data.services.timerange import get_timerange from flexmeasures.auth.policy import AuthModelMixin, EVERY_LOGGED_IN_USER from flexmeasures.utils import geo_utils -from flexmeasures.utils.coding_utils import flatten_unique +from flexmeasures.utils.coding_utils import flatten_unique, process_sensors from flexmeasures.utils.time_utils import determine_minimum_resampling_resolution @@ -86,7 +86,7 @@ class GenericAsset(db.Model, AuthModelMixin): longitude = db.Column(db.Float, nullable=True) attributes = db.Column(MutableDict.as_mutable(db.JSON), nullable=False, default={}) sensors_to_show = db.Column( - MutableDict.as_mutable(db.JSON), nullable=False, default={} + MutableList.as_mutable(db.JSON), nullable=False, default=[] ) # One-to-many (or many-to-one?) relationships @@ -141,17 +141,10 @@ class GenericAsset(db.Model, AuthModelMixin): ), ) - # def __init__( - # self, name, latitude=None, longitude=None, attributes=None, sensors=None - # ): - # super().__init__( - # name=name, latitude=latitude, longitude=longitude, attributes=attributes - # ) - - # # Process the sensors data - # processed_sensors_to_show = process_sensors(self) - # print("====================: ", {"sensors_to_show": processed_sensors_to_show}) - # self.sensors_to_show = {"sensors_to_show": processed_sensors_to_show} + def __init__(self, *args, **kwargs): + processed_sensors_to_show = process_sensors(self) + self.sensors_to_show = processed_sensors_to_show + super().__init__(*args, **kwargs) def __acl__(self): """