From c7b4be6278bc390190a8e5d2c33b73e51e9b9675 Mon Sep 17 00:00:00 2001 From: Jan Klenner Date: Fri, 6 Jun 2025 08:29:46 +0200 Subject: [PATCH] update activity relink to only show relevant databases and avoid unsafe activity opening in relink dialogue. Avoid error of undefined failed variable in relink Resolves #1167 --- .../actions/activity/activity_relink.py | 13 +++++++------ activity_browser/ui/widgets/dialog.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/activity_browser/actions/activity/activity_relink.py b/activity_browser/actions/activity/activity_relink.py index 347f0faf4..29eeec5d7 100644 --- a/activity_browser/actions/activity/activity_relink.py +++ b/activity_browser/actions/activity/activity_relink.py @@ -10,12 +10,12 @@ from activity_browser.ui.widgets import (ActivityLinkingDialog, ActivityLinkingResultsDialog) - class ActivityRelink(ABAction): """ ABAction to relink the exchanges of an activity to exchanges from another database. - This action only uses the first key from activity_keys + This action only uses the first key from activity_keys. + Only shows databases relevant to activity. """ icon = qicons.edit @@ -31,8 +31,10 @@ def run(activity_keys: List[tuple]): db = bd.Database(key[0]) activity = bd.get_activity(key) - # find the dependents for the database and construct the alternatives in tuple format - depends = db.find_dependents() + # find the dependents for the activity using bw2data method and construct the alternatives in tuple format + data = {} + data[activity.key] = db.load()[activity.key] + depends = db.find_dependents(data = data, ignore= [db.name]) options = [(depend, list(bd.databases)) for depend in depends] # present the alternatives to the user in a linking dialog @@ -49,16 +51,15 @@ def run(activity_keys: List[tuple]): # use the relink_activity_exchanges strategy to relink the exchanges of the activity relinking_results = {} + failed = 0 for old, new in dialog.relink.items(): other = bd.Database(new) failed, succeeded, examples = relink_activity_exchanges( activity, old, other ) relinking_results[f"{old} --> {other.name}"] = (failed, succeeded) - # restore normal cursor QtWidgets.QApplication.restoreOverrideCursor() - # if any relinks failed present them to the user if failed > 0: relinking_dialog = ActivityLinkingResultsDialog.present_relinking_results( diff --git a/activity_browser/ui/widgets/dialog.py b/activity_browser/ui/widgets/dialog.py index 95eab30e0..b9136c847 100644 --- a/activity_browser/ui/widgets/dialog.py +++ b/activity_browser/ui/widgets/dialog.py @@ -422,7 +422,7 @@ def construct_results_dialog( for act, key in unlinked_exchanges.items(): button = QtWidgets.QPushButton(act.as_dict()["name"]) button.clicked.connect( - lambda: signals.unsafe_open_activity_tab.emit(act.key) + lambda: signals.safe_open_activity_tab.emit(act.key) ) obj.exchangesUnlinked.addWidget(button) obj.updateGeometry() @@ -574,7 +574,7 @@ def construct_results_dialog( for act, key in unlinked_exchanges.items(): button = QtWidgets.QPushButton(act.as_dict()["name"]) button.clicked.connect( - lambda: signals.unsafe_open_activity_tab.emit(act.key) + lambda: signals.safe_open_activity_tab.emit(act.key) ) obj.exchangesUnlinked.addWidget(button) obj.updateGeometry()