Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in editing assets #122

Merged
merged 5 commits into from May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
15 changes: 8 additions & 7 deletions flexmeasures/ui/crud/assets.py
Expand Up @@ -68,13 +68,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"] = (
nhoening marked this conversation as resolved.
Show resolved Hide resolved
data["display_name"].lower().replace(" ", "_")
) # 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 +248,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 @@ -264,7 +264,7 @@ def post(self, id: str):
)
asset_form.process_api_validation_errors(post_asset_response.json())
if "message" in post_asset_response.json():
error_msg = post_asset_response.json()["message"]
error_msg = str(post_asset_response.json()["message"]["json"])
nhoening marked this conversation as resolved.
Show resolved Hide resolved
if asset is None:
msg = "Cannot create asset. " + error_msg
return render_flexmeasures_template(
Expand Down Expand Up @@ -311,6 +311,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."
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
asset_form.process_api_validation_errors(patch_asset_response.json())
asset = Asset.query.get(id)

Expand Down