def delete_user_and_data(email: str): """ Delete a user, which also deletes their data. """ the_user = find_user_by_email(email) if the_user is None: print(f"Could not find user with email address '{email}' ...") return delete_user(the_user) app.db.session.commit()
def delete_a_user(email: str, force: bool): """ Delete a user & also their assets and data. """ if not force: prompt = f"Delete user '{email}'?" click.confirm(prompt, abort=True) the_user = find_user_by_email(email) if the_user is None: print(f"Could not find user with email address '{email}' ...") return delete_user(the_user) db.session.commit()
def test_delete_user(app): """Assert user has assets and power measurements. Deleting removes all of that.""" prosumer: User = find_user_by_email("*****@*****.**") num_users_before = User.query.count() user_assets_with_measurements_before = Asset.query.filter( Asset.owner_id == prosumer.id, Asset.asset_type_name.in_(["wind", "solar"]) ).all() asset_ids = [asset.id for asset in user_assets_with_measurements_before] for asset_id in asset_ids: num_power_measurements = Power.query.filter(Power.asset_id == asset_id).count() assert num_power_measurements == 96 delete_user(prosumer) assert find_user_by_email("*****@*****.**") is None user_assets_after = Asset.query.filter(Asset.owner_id == prosumer.id).all() assert len(user_assets_after) == 0 assert User.query.count() == num_users_before - 1 for asset_id in asset_ids: num_power_measurements = Power.query.filter(Power.asset_id == asset_id).count() assert num_power_measurements == 0
def delete_account(id: int, force: bool): """ Delete an account, including their users & data. """ account: Account = db.session.query(Account).get(id) if account is None: print(f"Account with ID '{id}' does not exist.") raise click.Abort if not force: prompt = f"Delete account '{account.name}', including generic assets, users and all their data?\n" users = User.query.filter(User.account_id == id).all() if users: prompt += "Affected users: " + ",".join( [u.username for u in users]) + "\n" generic_assets = GenericAsset.query.filter( GenericAsset.account_id == id).all() if generic_assets: prompt += ("Affected generic assets: " + ",".join([ga.name for ga in generic_assets]) + "\n") click.confirm(prompt, abort=True) for user in account.users: print(f"Deleting user {user} ...") delete_user(user) for role_account_association in RolesAccounts.query.filter_by( account_id=account.id).all(): role = AccountRole.query.get(role_account_association.role_id) print( f"Deleting association of account {account.name} and role {role.name} ..." ) db.session.delete(role_account_association) for asset in account.generic_assets: print(f"Deleting generic asset {asset} (and sensors & beliefs) ...") db.session.delete(asset) account_name = account.name db.session.delete(account) db.session.commit() print(f"Account {account_name} has been deleted.")