flexmeasures.data.services.generic_assets
Functions
- flexmeasures.data.services.generic_assets.asset_contains_data(asset: GenericAsset, lock: bool = True) bool
Return whether an asset subtree holds beliefs or annotations.
The subtree’s assets and sensors are looked up first, and the probes name them by ID. Probing
timed_beliefwith a subquery for its sensors instead lets Postgres plan a join that reads the whole table’s index, which takes seconds on a large database whenever the subtree holds no beliefs.
- flexmeasures.data.services.generic_assets.check_parent_asset_access(parent: GenericAsset, account_id: int | None) None
Check that the current user may put an asset of the given account under this parent asset.
A parent in the same account needs nothing more than the caller already checks. A parent in another account (asset trees may span organisations) needs permission to add children under the parent, and to create assets in the child’s account (only admins may for public assets). The CLI may do anything.
- Raises:
Forbidden or Unauthorized – if the user may not.
- flexmeasures.data.services.generic_assets.cleanup_asset_references_in_assets(asset_id: int, asset_name: str | None = None) int
Remove references to a deleted asset in sensors_to_show across assets.
- flexmeasures.data.services.generic_assets.create_asset(asset_data: dict) GenericAsset
Create an asset.
Validates KPI sensor references, but assumes other data was validated by the caller. Does not commit the session. Creates an audit log.
- flexmeasures.data.services.generic_assets.cross_account_parent_warning(asset: GenericAsset, parent: GenericAsset | None, account_id: int | None) str | None
Describe placing an asset of the given account under a parent asset of another organisation.
The CLI may do this without the checks of check_parent_asset_access, so it warns instead. A parent in the same account, no parent and a public parent need no warning; a public asset under a parent of an account does (it shows up in that account’s tree).
- Parameters:
asset – the asset placed under the parent (its name and id are used)
parent – its (new) parent asset, if any
account_id – the (new) account of the asset (None for a public asset)
- Returns:
the warning text, or None if nothing crosses organisations
- flexmeasures.data.services.generic_assets.delete_asset(asset: GenericAsset)
Delete an asset.
Does not commit the session. Creates an audit log.
- flexmeasures.data.services.generic_assets.format_json_field_change(field_name: str, old_value, new_value) str
Format JSON field changes into a human-readable audit log entry.
Produces plain-language descriptions for sensors_to_show, flex_context, and flex_model changes. For generic JSON fields, nested dict/list structures are described recursively. Falls back to a simple before/after note for other fields or when an error occurs.
- Parameters:
field_name – Name of the field being changed.
old_value – The old value of the field.
new_value – The new value of the field.
- Returns:
A formatted string describing the changes.
Examples
>>> json = { ... "field_name": "flex_model", ... "old_value": {"production-capacity": "15 kW"}, ... "new_value": {"production-capacity": "15 kW", "storage-efficiency": "99.92%"} ... } >>> format_json_field_change(**json) 'Updated flex_model:\n1. Added storage-efficiency: 99.92%'
>>> json = { ... "field_name": "flex_context", ... "old_value": {"site-production-capacity": "1500 kW", "site-peak-production": "20000kW"}, ... "new_value": {"site-production-capacity": "15000 kW", "site-peak-production": "20000kW"} ... } >>> format_json_field_change(**json) 'Updated flex_context:\n1. Changed site-production-capacity: 1500 kW → 15000 kW'
>>> json = { ... "field_name": "flex_context", ... "old_value": {"site-production-capacity": "15000 kW", "site-peak-production": "20000kW"}, ... "new_value": {"site-peak-production": "20000kW"} ... } >>> format_json_field_change(**json) 'Updated flex_context:\n1. Removed site-production-capacity (was: 15000 kW)'
>>> json = { ... "field_name": "flex_model", ... "old_value": {"soc-usage": ["3500 kW", {"sensor": 7}]}, ... "new_value": {"soc-usage": ["3500 kW", {"sensor": 8}]} ... } >>> format_json_field_change(**json) 'Updated flex_model:\n1. Changed soc-usage[1].sensor: 7 → 8'
>>> json = { ... "field_name": "sensors_to_show", ... "old_value": [{"title": "Power", "plots": [{"sensor": 1}]}], ... "new_value": [ ... {"title": "Power", "plots": [{"sensor": 1}, {"sensor": 2}]}, ... {"title": "Price", "plots": [{"sensor": 3}]}, ... ], ... } >>> format_json_field_change(**json) 'Updated sensors_to_show:\n1. Changed graph 1 (Power): added plot 2: {"sensor": 2}\n2. Added graph 2: "Price" (sensors [3])'
- flexmeasures.data.services.generic_assets.get_readable_offspring(asset: GenericAsset) list[GenericAsset]
The assets below this one, at any depth, which the current user may read.
Being below a readable asset grants nothing by itself: a child asset can belong to another account than its parent, so each one is checked on its own.
- flexmeasures.data.services.generic_assets.patch_asset(db_asset: GenericAsset, asset_data: dict) GenericAsset
Patch an asset.
Throws validation error as it checks JSON fields (e.g. attributes) for validity explicitly. Does not commit the session. Creates an audit log.
- flexmeasures.data.services.generic_assets.validate_parent_asset_access(parent: GenericAsset, account_id: int | None, field_name: str = 'parent_asset_id') None
As check_parent_asset_access, for validators: refusal is a validation error.
- Raises:
ValidationError – if the user may not put an asset of this account under the parent.