Skip to content

Commit 0eb35e3

Browse files
committed
enh: update local DB when user adds dataset to collection
1 parent 899d99d commit 0eb35e3

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

dcoraid/dbmodel/db_api_cached.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def get_collections(self, refresh=False):
7474
clist += self.ai.get_collections()
7575
return clist
7676

77-
def get_dataset_dictionary(self, ds_id):
77+
def get_dataset_dict(self, ds_id):
7878
return self._mc[ds_id]
7979

8080
def get_datasets_user_following(self) -> DBExtract:

dcoraid/gui/dbview/filter_chain.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def update_resources(self):
128128

129129

130130
class FilterChainUser(FilterChain):
131+
added_datasets_to_collection = QtCore.pyqtSignal(dict, list)
131132

132133
def __init__(self, *args, **kwargs):
133134
"""Filter chain with user-related features"""
@@ -182,3 +183,5 @@ def on_add_datasets_to_collection(self):
182183
# "capacity" should not be necessary
183184
# https://github.com/ckan/ckan/issues/6543
184185
"capacity": "member"})
186+
self.added_datasets_to_collection.emit(collection,
187+
dataset_ids)

dcoraid/gui/panel_my_data/widget_my_data.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ def __init__(self, *args, **kwargs):
3131
self.on_refresh_private_data)
3232
self.user_filter_chain.download_resource.connect(
3333
self.request_download)
34+
self.user_filter_chain.added_datasets_to_collection.connect(
35+
self.on_added_datasets_to_collection)
36+
37+
@QtCore.pyqtSlot(dict, list)
38+
def on_added_datasets_to_collection(self, collection, dataset_ids):
39+
"""User manually added a bunch of datasets to a collection"""
40+
# Get the collection
41+
for col in self.database.get_collections(refresh=True):
42+
if col == collection["name"]:
43+
cid = collection["id"]
44+
break
45+
else:
46+
raise KeyError(f"Collection {collection['id']} not found!")
47+
# Append it to each dataset
48+
for ds_id in dataset_ids:
49+
ds_dict = self.database.get_dataset_dict(ds_id)
50+
collections = [g["id"] for g in ds_dict["groups"]]
51+
if cid in collections:
52+
c_idx = collections.index(cid)
53+
ds_dict["groups"][c_idx].update(collection)
54+
else:
55+
ds_dict["groups"].append(collection)
56+
self.database.update_dataset(ds_dict)
3457

3558
@QtCore.pyqtSlot()
3659
def on_refresh_private_data(self):

tests/test_gui.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,14 @@ def test_gui_mydata_dataset_add_to_collection(mw, qtbot):
142142
return_value=(f"{ii}: {item['display_name']}", True)):
143143
qtbot.mouseClick(fw_datasets.toolButton_custom,
144144
QtCore.Qt.MouseButton.LeftButton)
145-
# Now check whether that worked
145+
146+
# Check whether dataset is in collection in database
147+
ds_dict_db = mw.database.get_dataset_dict(ds_id)
148+
assert "groups" in ds_dict_db
149+
assert len(ds_dict_db["groups"]) == 1
150+
assert ds_dict_db["groups"][0]["name"] == defaults["collection"]
151+
152+
# Check whether dataset is in collection via API
146153
ds_dict = api.get("package_show", id=ds_id)
147154
assert "groups" in ds_dict
148155
assert len(ds_dict["groups"]) == 1
@@ -226,7 +233,7 @@ def test_gui_upload_simple(mw, qtbot):
226233
mw.panel_upload.widget_jobs.update_job_status()
227234
QtWidgets.QApplication.processEvents(
228235
QtCore.QEventLoop.ProcessEventsFlag.AllEvents, 200)
229-
assert mw.database.get_dataset_dictionary(dlg.dataset_id)
236+
assert mw.database.get_dataset_dict(dlg.dataset_id)
230237

231238

232239
def test_gui_upload_task(mw, qtbot):

0 commit comments

Comments
 (0)