Skip to content

Commit d6fe32a

Browse files
committed
ref: remove mode from DBInterrogator
1 parent 6fb605f commit d6fe32a

5 files changed

Lines changed: 58 additions & 69 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- enh: remove unused "Sharing" page
1515
- enh: add icon for "Find Data"
1616
- enh: cache CKANAPI instance in GUI
17+
- ref: remove `mode` from `DBInterrogator`
1718
- ref: replace `user_list` with `user_autocomplete`
1819
- ref: replace `search_dataset` with `search_dataset_via_api`
1920
in `APIInterrogator`; `search_dataset` is now only free text search

dcoraid/dbmodel/db_api.py

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,20 @@ class APIInterrogator(DBInterrogator):
2222
def __init__(self, api):
2323
self.api = api.copy()
2424
if api.user_id:
25-
mode = "user"
2625
user_data = api.get_user_dict()
2726
else:
28-
mode = "public"
2927
user_data = None
30-
super(APIInterrogator, self).__init__(mode=mode, user_data=user_data)
28+
super(APIInterrogator, self).__init__(user_data=user_data)
3129

3230
def get_circles(self):
3331
"""Return the list of DCOR Circle names
3432
"""
35-
if self.mode == "user":
36-
# Organizations the user is a member of
37-
circle_dict = self.api.get("organization_list_for_user",
38-
id=self.api.user_id,
39-
permission="read")
40-
data = [dd["name"] for dd in circle_dict]
41-
else:
42-
data = self.api.get("organization_list")
33+
data = self.api.get("organization_list")
4334
return data
4435

4536
def get_collections(self):
4637
"""Return the list of DCOR Collection names"""
47-
if self.mode == "user":
48-
collection_dict = self.api.get("group_list_authz", am_member=True)
49-
data = [dd["name"] for dd in collection_dict]
50-
else:
51-
data = self.api.get("group_list")
38+
data = self.api.get("group_list")
5239
if len(data) == 1000:
5340
raise NotImplementedError(
5441
"Reached hard limit of 1000 results! "
@@ -57,50 +44,56 @@ def get_collections(self):
5744

5845
def get_datasets_user_following(self):
5946
"""Return datasets the user is following"""
60-
assert self.mode == "user"
61-
data = self.api.get("dataset_followee_list", id=self.api.user_name)
47+
if self.api.user_id is not None:
48+
data = self.api.get("dataset_followee_list", id=self.api.user_name)
49+
else:
50+
data = []
6251
return DBExtract(data)
6352

6453
def get_datasets_user_owned(self):
6554
"""Return datasets the user created"""
66-
assert self.mode == "user"
67-
dbextract = self.search_dataset_via_api(
68-
filter_queries=[f"+creator_user_id:{self.api.user_id}"],
69-
limit=0,
70-
)
71-
return dbextract
55+
if self.api.user_id is not None:
56+
dbe = self.search_dataset_via_api(
57+
filter_queries=[f"+creator_user_id:{self.api.user_id}"],
58+
limit=0,
59+
)
60+
else:
61+
dbe = DBExtract()
62+
return dbe
7263

7364
def get_datasets_user_shared(self):
7465
"""Return datasets shared with the user"""
75-
assert self.mode == "user"
7666
# Perform a dataset search with all circles and collections.
7767
# This search may become too large (414 Request-URI Too Large).
7868
# Limit the search to 20 circles/collections.
79-
dbextract = DBExtract()
69+
dbe = DBExtract()
8070

81-
for circles_batch in batched(self.get_circles(), 20):
82-
dbextract += self.search_dataset_via_api(
83-
circles=list(circles_batch),
84-
filter_queries=[f"-creator_user_id:{self.api.user_id}"],
85-
limit=0,
86-
)
71+
if self.api.user_id is not None:
8772

88-
for collections_batch in batched(self.get_collections(), 20):
89-
dbextract += self.search_dataset_via_api(
90-
collections=list(collections_batch),
91-
filter_queries=[f"-creator_user_id:{self.api.user_id}"],
92-
limit=0,
93-
)
73+
for circles_batch in batched(self.get_circles(), 20):
74+
dbe += self.search_dataset_via_api(
75+
circles=list(circles_batch),
76+
filter_queries=[f"-creator_user_id:{self.api.user_id}"],
77+
limit=0,
78+
)
9479

95-
# all packages the user is a collaborator in
96-
collaborated = self.api.get("package_collaborator_list_for_user",
97-
id=self.user_data["id"])
98-
for col in collaborated:
99-
if col["package_id"] not in dbextract:
100-
ds_dict = self.api.get("package_show", id=col["package_id"])
101-
dbextract.add_datasets([ds_dict])
80+
for collections_batch in batched(self.get_collections(), 20):
81+
dbe += self.search_dataset_via_api(
82+
collections=list(collections_batch),
83+
filter_queries=[f"-creator_user_id:{self.api.user_id}"],
84+
limit=0,
85+
)
10286

103-
return dbextract
87+
# all packages the user is a collaborator in
88+
collaborated = self.api.get("package_collaborator_list_for_user",
89+
id=self.user_data["id"])
90+
for col in collaborated:
91+
if col["package_id"] not in dbe:
92+
ds_dict = self.api.get("package_show",
93+
id=col["package_id"])
94+
dbe.add_datasets([ds_dict])
95+
96+
return dbe
10497

10598
def get_users(self, ret_fullnames=False):
10699
"""Return the list of DCOR users"""
@@ -237,16 +230,17 @@ def search_dataset_via_api(self,
237230

238231
num_total = np.inf # just the initial value
239232
num_retrieved = 0
240-
dbextract = DBExtract()
233+
dbe = DBExtract()
241234
while start + num_retrieved < min(start + limit, num_total) and rows:
242-
data = self.api.get("package_search",
243-
q=urllib.parse.quote(query, safe=""),
244-
fq=final_fq,
245-
include_private=(self.mode == "user"),
246-
rows=rows,
247-
sort=sort_solr,
248-
start=start + num_retrieved,
249-
)
235+
data = self.api.get(
236+
"package_search",
237+
q=urllib.parse.quote(query, safe=""),
238+
fq=final_fq,
239+
include_private=bool(self.api.user_id is not None),
240+
rows=rows,
241+
sort=sort_solr,
242+
start=start + num_retrieved,
243+
)
250244
if np.isinf(num_total):
251245
# first iteration
252246
num_total = data["count"]
@@ -255,9 +249,9 @@ def search_dataset_via_api(self,
255249
# in the next iteration, only get the final
256250
# few results.
257251
rows = num_total - num_retrieved
258-
dbextract.add_datasets(data["results"])
252+
dbe.add_datasets(data["results"])
259253

260-
return dbextract
254+
return dbe
261255

262256
def update(self, reset=False):
263257
"""Ignored, since no local database exists"""

dcoraid/dbmodel/db_api_cached.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ def __init__(self, api, cache_location):
2828
}
2929

3030
if api.user_id:
31-
mode = "user"
3231
user_data = api.get_user_dict()
3332
else:
34-
mode = "public"
3533
user_data = None
3634

3735
self._mc = MetaCache(self.cache_location)
@@ -40,8 +38,7 @@ def __init__(self, api, cache_location):
4038
self._mc_version_path = self.cache_location / "cache_version"
4139
self._mc_version_path.touch()
4240

43-
super(CachedAPIInterrogator, self).__init__(mode=mode,
44-
user_data=user_data)
41+
super(CachedAPIInterrogator, self).__init__(user_data=user_data)
4542

4643
@property
4744
def local_timestamp(self):

dcoraid/dbmodel/db_core.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33

44
class DBInterrogator(abc.ABC):
5-
def __init__(self, mode, user_data):
6-
if mode == "user":
5+
def __init__(self, user_data):
6+
if user_data:
77
mandatory = ["id", "name", "number_created_packages"]
88
missing = [key for key in mandatory if key not in user_data]
99
if missing:
1010
raise ValueError("The following keys are missing in "
11-
f"`user_data` for `mode=='user'`: {missing}")
11+
f"`user_data`: {missing}")
1212
self.user_data = user_data
13-
self.mode = mode
1413
self.search_query = {}
1514

1615
@property
@@ -39,8 +38,6 @@ def get_collections(self):
3938

4039
def get_datasets_user(self):
4140
"""Return DBExtract with data owned by or shared with the user"""
42-
if self.mode != "user":
43-
raise ValueError("Cannot get user datasets if mode is 'public'!")
4441
owned = self.get_datasets_user_owned()
4542
shared = self.get_datasets_user_shared()
4643
following = self.get_datasets_user_following()

dcoraid/gui/panel_find_data/widget_find_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def on_search(self):
4141
mv.check_update_database()
4242
if mv.database:
4343
self.setCursor(QtCore.Qt.CursorShape.WaitCursor)
44-
dbextract = self.database.search_dataset(
44+
dbe = self.database.search_dataset(
4545
self.lineEdit_search.text(),
4646
limit=self.spinBox_public_rows.value())
47-
self.public_filter_chain.set_db_extract(dbextract)
47+
self.public_filter_chain.set_db_extract(dbe)
4848
self.setCursor(QtCore.Qt.CursorShape.ArrowCursor)
4949

5050
@QtCore.pyqtSlot()

0 commit comments

Comments
 (0)