Skip to content

Commit

Permalink
Regression in editing assets (#122)
Browse files Browse the repository at this point in the history
* Asset crud UI: add name field only when posting, make error handling more robust

Co-authored-by: nhoening <nhoening@users.noreply.github.com>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>
Co-authored-by: Felix Claessen <30658763+Flix6x@users.noreply.github.com>
  • Loading branch information
4 people committed May 7, 2021
1 parent 0b8ba5c commit 97d55b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -25,6 +25,7 @@ v0.4.1 | May XX, 2021

Bugfixes
-----------
* Fix regression when editing assets in the UI [see `PR #122 <http://www.github.com/SeitaBV/flexmeasures/pull/122>`_]
* Prevent logging out user when clearing the session [see `PR #112 <http://www.github.com/SeitaBV/flexmeasures/pull/112>`_]
* Prevent user type data source to be created without setting a user [see `PR #111 <https://github.com/SeitaBV/flexmeasures/pull/111>`_]

Expand Down
7 changes: 4 additions & 3 deletions flexmeasures/data/transactional.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 13 additions & 8 deletions flexmeasures/ui/crud/assets.py
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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],
)

Expand All @@ -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(
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 97d55b9

Please sign in to comment.