Skip to content

Commit e230453

Browse files
committed
posts collection name and id
1 parent 7c6b558 commit e230453

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

scripts/migration/migrate.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,33 @@ def add_v1_space_members_to_v2_group(space, group_id, headers):
110110
headers=headers,
111111
)
112112

113+
113114
def get_clowder_v1_user_collections(headers, user_v1):
114115
endpoint = f"{CLOWDER_V1}/api/collections"
115116
response = requests.get(endpoint, headers=headers)
116117
return [col for col in response.json() if col["authorId"] == user_v1["id"]]
117118

119+
118120
def get_clowder_v1_dataset_collections(headers, user_v1, dataset_id):
119121
matching_collections = []
120122
endpoint = f"{CLOWDER_V1}/api/collections"
121123
response = requests.get(endpoint, headers=headers)
122-
user_collections =[col for col in response.json() if col["authorId"] == user_v1["id"]]
124+
user_collections = [
125+
col for col in response.json() if col["authorId"] == user_v1["id"]
126+
]
123127
for collection in user_collections:
124128
collection_id = collection["id"]
125-
collection_dataset_endpoint = f"{CLOWDER_V1}/api/collections/{collection_id}/datasets"
129+
collection_dataset_endpoint = (
130+
f"{CLOWDER_V1}/api/collections/{collection_id}/datasets"
131+
)
126132
dataset_response = requests.get(collection_dataset_endpoint, headers)
127133
datasets = dataset_response.json()
128134
for ds in datasets:
129-
if ds['id'] == dataset_id:
135+
if ds["id"] == dataset_id:
130136
matching_collections.append(collection)
131137
return matching_collections
132138

139+
133140
def create_local_user(user_v1):
134141
"""Create a local user in Clowder v2 if they don't already exist, and generate an API key."""
135142
# Search for the user by email
@@ -287,7 +294,9 @@ def download_and_upload_file(file, all_dataset_folders, dataset_v2_id, headers_v
287294
file_exists = os.path.exists(filename)
288295
# with open(filename, "rb") as file_data:
289296
response = requests.post(
290-
dataset_file_upload_endpoint, headers=headers_v2, files={"file": open(filename, "rb")}
297+
dataset_file_upload_endpoint,
298+
headers=headers_v2,
299+
files={"file": open(filename, "rb")},
291300
)
292301

293302
if response.status_code == 200:
@@ -307,9 +316,7 @@ def process_user_and_resources(user_v1, USER_MAP, DATASET_MAP):
307316
user_v1_datasets = get_clowder_v1_user_datasets(user_id=user_v1["id"])
308317
user_v2_api_key = create_local_user(user_v1)
309318
USER_MAP[user_v1["id"]] = user_v2_api_key
310-
base_user_headers_v2 = {
311-
"x-api-key": user_v2_api_key
312-
}
319+
base_user_headers_v2 = {"x-api-key": user_v2_api_key}
313320
user_headers_v2 = {
314321
"x-api-key": user_v2_api_key,
315322
"content-type": "application/json",
@@ -338,11 +345,28 @@ def process_user_and_resources(user_v1, USER_MAP, DATASET_MAP):
338345
download_and_upload_file(
339346
file, all_dataset_folders, dataset_v2_id, base_user_headers_v2
340347
)
341-
dataset_collections = get_clowder_v1_dataset_collections(headers=clowder_headers_v1, user_v1=user_v1, dataset_id=dataset['id'])
348+
dataset_collections = get_clowder_v1_dataset_collections(
349+
headers=clowder_headers_v1, user_v1=user_v1, dataset_id=dataset["id"]
350+
)
342351
# TODO for now taking the first collection, assuming a dataset is in one collection only
343352
dataset_collection = dataset_collections[0]
344-
dataset_collection_name = dataset_collection['collectionname']
345-
dataset_collection_id = dataset_collection['id']
353+
dataset_collection_name = dataset_collection["collectionname"]
354+
dataset_collection_id = dataset_collection["id"]
355+
# TODO this assumes that the COLLECTION DEFINITION is already in the db
356+
metadata_using_definition = {
357+
"content": {
358+
"collection_name": dataset_collection_name,
359+
"collection_id": dataset_collection_id,
360+
},
361+
"definition": "Collection",
362+
}
363+
response = requests.post(
364+
f"{CLOWDER_V2}/api/v2/datasets/{dataset_v2_id}/metadata",
365+
headers=user_headers_v2,
366+
json=metadata_using_definition,
367+
)
368+
if response.status_code == 200:
369+
print("Successfully uploaded collection metadata")
346370
return [USER_MAP, DATASET_MAP]
347371

348372

0 commit comments

Comments
 (0)