diff --git a/documentation/changelog.rst b/documentation/changelog.rst index d78be74b2..88977402f 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -25,6 +25,7 @@ v0.4.1 | May XX, 2021 Bugfixes ----------- +* Fix regression when editing assets in the UI [see `PR #122 `_] * Prevent logging out user when clearing the session [see `PR #112 `_] * Prevent user type data source to be created without setting a user [see `PR #111 `_] diff --git a/flexmeasures/data/transactional.py b/flexmeasures/data/transactional.py index 920da7397..adbade62c 100644 --- a/flexmeasures/data/transactional.py +++ b/flexmeasures/data/transactional.py @@ -59,7 +59,7 @@ def after_request_exception_rollback_session(exception): Register this on your app via the teardown_request setup method. We roll back the session if there was any error (which only has an effect if - the session has not yet been comitted). + the session has not yet been committed). Flask-SQLAlchemy is closing the scoped sessions automatically.""" if exception is not None: @@ -118,12 +118,13 @@ def wrap(*args, **kwargs): task_run.datetime = datetime.utcnow().replace(tzinfo=pytz.utc) task_run.status = status click.echo( - "Reported task %s status as %s" % (task_function.__name__, status) + "[FLEXMEASURES] Reported task %s status as %s" + % (task_function.__name__, status) ) db.session.commit() except Exception as e: click.echo( - "[FLEXMEASURES] Could not report the running of Task %s, encountered the following problem: [%s]." + "[FLEXMEASURES] Could not report the running of task %s. Encountered the following problem: [%s]." " The task might have run fine." % (task_function.__name__, str(e)) ) db.session.rollback() diff --git a/flexmeasures/ui/crud/assets.py b/flexmeasures/ui/crud/assets.py index b779a8965..d82be0b6e 100644 --- a/flexmeasures/ui/crud/assets.py +++ b/flexmeasures/ui/crud/assets.py @@ -16,6 +16,7 @@ from flexmeasures.data.models.assets import AssetType, Asset from flexmeasures.data.models.user import User from flexmeasures.data.models.markets import Market +from flexmeasures.utils.flexmeasures_inflection import parameterize from flexmeasures.ui.utils.plotting_utils import get_latest_power_as_plot from flexmeasures.ui.utils.view_utils import render_flexmeasures_template from flexmeasures.ui.crud.api_wrapper import InternalApi @@ -68,13 +69,13 @@ def validate_on_submit(self): ) return super().validate_on_submit() - def to_json(self) -> dict: + def to_json(self, for_posting=False) -> dict: """ turn form data into a JSON we can POST to our internal API """ data = copy.copy(self.data) - data["name"] = data["display_name"] # both are part of the asset model - data[ - "unit" - ] = "MW" # TODO: make unit a choice? this is hard-coded in the UI as well + if for_posting: + data["name"] = parameterize( + data["display_name"] + ) # best guess at un-humanizing data["capacity_in_mw"] = float(data["capacity_in_mw"]) data["min_soc_in_mwh"] = float(data["min_soc_in_mwh"]) data["max_soc_in_mwh"] = float(data["max_soc_in_mwh"]) @@ -248,7 +249,7 @@ def post(self, id: str): if form_valid and owner is not None and market is not None: post_asset_response = InternalApi().post( url_for("flexmeasures_api_v2_0.post_assets"), - args=asset_form.to_json(), + args=asset_form.to_json(for_posting=True), do_not_raise_for=[400, 422], ) @@ -263,8 +264,11 @@ def post(self, id: str): f"Internal asset API call unsuccessful [{post_asset_response.status_code}]: {post_asset_response.text}" ) asset_form.process_api_validation_errors(post_asset_response.json()) - if "message" in post_asset_response.json(): - error_msg = post_asset_response.json()["message"] + if ( + "message" in post_asset_response.json() + and "json" in post_asset_response.json()["message"] + ): + error_msg = str(post_asset_response.json()["message"]["json"]) if asset is None: msg = "Cannot create asset. " + error_msg return render_flexmeasures_template( @@ -311,6 +315,7 @@ def post(self, id: str): current_app.logger.error( f"Internal asset API call unsuccessful [{patch_asset_response.status_code}]: {patch_asset_response.text}" ) + msg = "Cannot edit asset." asset_form.process_api_validation_errors(patch_asset_response.json()) asset = Asset.query.get(id)