Skip to content

Commit

Permalink
add parent_asset_id to GenericAssetSchema and turn the type to int (#959
Browse files Browse the repository at this point in the history
)

* add parent_asset_id to GenericAssetSchema and turn the type to int in the click command

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

* don't check when the field is missing

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

* args -> kwargs

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

* add changelog

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

* refactor: rename kwargument

Signed-off-by: F.N. Claessen <felix@seita.nl>

---------

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Co-authored-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
victorgarcia98 and Flix6x committed Jan 12, 2024
1 parent a11e412 commit c287e51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Bugfixes
* Allow showing beliefs (plot and file export) via the CLI for sensors with non-unique names [see `PR #947 <https://github.com/FlexMeasures/flexmeasures/pull/947>`_]
* Added Redis credentials to the Docker Compose configuration for the web server to ensure proper interaction with the Redis queue [see `PR #945 <https://github.com/FlexMeasures/flexmeasures/pull/945>`_]
* Fix API version listing (GET /api/v3_0) for hosts running on Python 3.8 [see `PR #917 <https://github.com/FlexMeasures/flexmeasures/pull/917>`_ and `PR #950 <https://github.com/FlexMeasures/flexmeasures/pull/950>`_]
* Fix the validation of the option ``--parent-asset`` of command ``flexmeasures add asset`` [see `PR #959 <https://github.com/FlexMeasures/flexmeasures/pull/959>`_]



v0.18.0 | December 23, 2023
Expand Down
7 changes: 7 additions & 0 deletions documentation/cli/change_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
FlexMeasures CLI Changelog
**********************

since v0.18.1 | January XX, 2023
=======================================

* Fix the validation of the option ``--parent-asset`` of command ``flexmeasures add asset``.


since v0.17.0 | November 8, 2023
=======================================

* Add ``--consultancy`` option to ``flexmeasures add account`` to create a consultancy relationship with another account.


since v0.16.0 | September 29, 2023
=======================================

Expand Down
11 changes: 5 additions & 6 deletions flexmeasures/cli/data_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
from flexmeasures.data.schemas.generic_assets import (
GenericAssetSchema,
GenericAssetTypeSchema,
GenericAssetIdField,
)
from flexmeasures.data.models.generic_assets import GenericAsset, GenericAssetType
from flexmeasures.data.models.user import User
Expand Down Expand Up @@ -331,10 +330,10 @@ def add_sensor(**args):
type=str,
help="Description (useful to explain acronyms, for example).",
)
def add_asset_type(**args):
def add_asset_type(**kwargs):
"""Add an asset type."""
check_errors(GenericAssetTypeSchema().validate(args))
generic_asset_type = GenericAssetType(**args)
check_errors(GenericAssetTypeSchema().validate(kwargs))
generic_asset_type = GenericAssetType(**kwargs)
db.session.add(generic_asset_type)
db.session.commit()
click.secho(
Expand Down Expand Up @@ -372,9 +371,9 @@ def add_asset_type(**args):
)
@click.option(
"--parent-asset",
"parent_asset",
"parent_asset_id",
required=False,
type=GenericAssetIdField(),
type=int,
help="Parent of this asset. The entity needs to exists on the database.",
)
def add_asset(**args):
Expand Down
9 changes: 9 additions & 0 deletions flexmeasures/data/schemas/generic_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def validate_generic_asset_type(self, generic_asset_type_id: int):
f"GenericAssetType with id {generic_asset_type_id} doesn't exist."
)

@validates("parent_asset_id")
def validate_parent_asset(self, parent_asset_id: int | None):
if parent_asset_id is not None:
parent_asset = GenericAsset.query.get(parent_asset_id)
if not parent_asset:
raise ValidationError(
f"Parent GenericAsset with id {parent_asset_id} doesn't exist."
)

@validates("account_id")
def validate_account(self, account_id: int | None):
if account_id is None and (
Expand Down

0 comments on commit c287e51

Please sign in to comment.