Skip to content

Commit

Permalink
Transfer ownership of asset and its children to a different account. (#…
Browse files Browse the repository at this point in the history
…983)

* Transfer ownership of asset and its children to a different account.

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

* add docs and changelog entries

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

* address change requests

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

* improve conftest

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

---------

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 committed Feb 20, 2024
1 parent d4b70e5 commit 8b25ae9
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
3 changes: 3 additions & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ v0.20.0 | April XX, 2024
New features
-------------

* Add command ``flexmeasures edit transfer-ownership`` to transfer the ownership of an asset and its children from one account to another[see `PR #983 <https://github.com/FlexMeasures/flexmeasures/pull/983>`_]


Infrastructure / Support
----------------------

Expand Down
5 changes: 5 additions & 0 deletions documentation/cli/change_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
FlexMeasures CLI Changelog
**********************

since v.0.20.0 | March XX, 2024
=================================

* Add command ``flexmeasures edit transfer-ownership`` to transfer the ownership of an asset and its children.

since v0.19.0 | February 18, 2024
=======================================

Expand Down
2 changes: 2 additions & 0 deletions documentation/cli/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ of which some are referred to in this documentation.
``flexmeasures edit attribute`` Edit (or add) an asset attribute or sensor attribute.
``flexmeasures edit resample-data`` | Assign a new event resolution to an existing sensor
| and resample its data accordingly.
``flexmeasures edit transfer-ownership`` | Transfer the ownership of an asset and its children to
| a different account.
================================================= =======================================


Expand Down
39 changes: 38 additions & 1 deletion flexmeasures/cli/data_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
from flask import current_app as app
from flask.cli import with_appcontext
import json
from flexmeasures.data.models.user import Account
from flexmeasures.data.schemas.account import AccountIdField
from sqlalchemy import delete

from flexmeasures import Sensor
from flexmeasures import Sensor, Asset
from flexmeasures.data import db
from flexmeasures.data.schemas.attributes import validate_special_attributes
from flexmeasures.data.schemas.generic_assets import GenericAssetIdField
Expand Down Expand Up @@ -244,6 +246,41 @@ def resample_sensor_data(
click.secho("Successfully resampled sensor data.", **MsgStyle.SUCCESS)


@fm_edit_data.command("transfer-ownership")
@with_appcontext
@click.option(
"--asset",
"asset",
type=GenericAssetIdField(),
required=True,
help="Change the ownership of this asset and its children. Follow up with the asset's ID.",
)
@click.option(
"--new-owner",
"new_owner",
type=AccountIdField(),
required=True,
help="New owner of the asset and its children.",
)
def transfer_ownership(asset: Asset, new_owner: Account):
"""
Transfer the ownership of and asset and its children to an account.
"""

def transfer_ownership_recursive(asset: Asset, account: Account):
asset.owner = account
for child in asset.child_assets:
transfer_ownership_recursive(child, account)

transfer_ownership_recursive(asset, new_owner)
click.secho(
f"Success! Asset `{asset}` ownership was transfered to account `{new_owner}`.",
**MsgStyle.SUCCESS,
)

db.session.commit()


app.cli.add_command(fm_edit_data)


Expand Down
44 changes: 44 additions & 0 deletions flexmeasures/cli/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timedelta
from pytz import utc

from flexmeasures import Account
from flexmeasures.data.models.data_sources import DataSource
from flexmeasures.data.models.generic_assets import GenericAsset, GenericAssetType
from flexmeasures.data.models.time_series import Sensor, TimedBelief
Expand Down Expand Up @@ -209,3 +210,46 @@ def storage_schedule_sensors(
db.session.commit()

yield power_capacity.id, storage_efficiency.id


@pytest.fixture(scope="module")
def add_asset_with_children(db, setup_accounts):
account_id = setup_accounts["Supplier"].id
parent_type = GenericAssetType(
name="parent",
)
child_type = GenericAssetType(name="child")

db.session.add_all([parent_type, child_type])

parent = GenericAsset(
name="parent",
generic_asset_type=parent_type,
account_id=account_id,
)
db.session.add(parent)
db.session.flush() # assign parent asset id

assets = [
GenericAsset(
name=f"child_{i}",
generic_asset_type=child_type,
parent_asset_id=parent.id,
account_id=account_id,
)
for i in range(1, 3)
]

db.session.add_all(assets)
db.session.flush() # assign children asset ids

assets.append(parent)

return {a.name: a for a in assets}


@pytest.fixture(scope="module")
def add_alternative_account(app, db):
alternative_account = Account(name="Alternative Account")
db.session.add(alternative_account)
return alternative_account
34 changes: 34 additions & 0 deletions flexmeasures/cli/tests/test_data_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,37 @@ def test_cli_help(app):
result = runner.invoke(cmd, ["--help"])
assert result.exit_code == 0
assert "Usage" in result.output


def test_transfer_ownership(app, db, add_asset_with_children, add_alternative_account):
"""
Test that the parent and its children change their ownership from the old account
to the new one.
"""

from flexmeasures.cli.data_edit import transfer_ownership

parent = add_asset_with_children["parent"]
old_account = parent.owner
new_account = add_alternative_account

# assert that the children belong to the same account as the parent
for child in parent.child_assets:
assert child.owner == old_account

cli_input_params = {
"asset": parent.id,
"new_owner": new_account.id,
}

cli_input = to_flags(cli_input_params)

runner = app.test_cli_runner()
result = runner.invoke(transfer_ownership, cli_input)

assert result.exit_code == 0 # run command without errors

# assert that the parent and its children now belong to the new account
assert parent.owner == new_account
for child in parent.child_assets:
assert child.owner == new_account

0 comments on commit 8b25ae9

Please sign in to comment.