Skip to content

Commit f423636

Browse files
ddey2longshuicy
andauthored
created v2 license based on v1 license details (#1193)
Co-authored-by: Chen Wang <[email protected]>
1 parent 2b0cc4f commit f423636

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

scripts/migration/migrate.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,74 @@ def create_admin_user():
273273
return generate_user_api_key(admin_user, admin_user["password"])
274274

275275

276+
def add_dataset_license(v1_license, headers):
277+
"""Create appropriate license (standard/custom) based on v1 license details"""
278+
license_id = "CC-BY"
279+
# standard licenses
280+
if v1_license["license_type"] == "license2":
281+
if (
282+
not v1_license["ccAllowCommercial"]
283+
and not v1_license["ccAllowDerivative"]
284+
and not v1_license["ccRequireShareAlike"]
285+
):
286+
license_id = "CC BY-NC-ND"
287+
elif (
288+
v1_license["ccAllowCommercial"]
289+
and not v1_license["ccAllowDerivative"]
290+
and not v1_license["ccRequireShareAlike"]
291+
):
292+
license_id = "CC BY-ND"
293+
elif (
294+
not v1_license["ccAllowCommercial"]
295+
and v1_license["ccAllowDerivative"]
296+
and not v1_license["ccRequireShareAlike"]
297+
):
298+
license_id = "CC BY-NC"
299+
elif (
300+
not v1_license["ccAllowCommercial"]
301+
and v1_license["ccAllowDerivative"]
302+
and v1_license["ccRequireShareAlike"]
303+
):
304+
license_id = "CC BY-NC-SA"
305+
elif (
306+
v1_license["ccAllowCommercial"]
307+
and v1_license["ccAllowDerivative"]
308+
and v1_license["ccRequireShareAlike"]
309+
):
310+
license_id = "CC BY-SA"
311+
elif (
312+
v1_license["ccAllowCommercial"]
313+
and v1_license["ccAllowDerivative"]
314+
and not v1_license["ccRequireShareAlike"]
315+
):
316+
license_id = "CC BY"
317+
elif v1_license["license_type"] == "license3":
318+
license_id = "CCO Public Domain Dedication"
319+
else:
320+
# custom license
321+
license_body = {
322+
"name": v1_license["license_text"],
323+
"url": v1_license["license_url"],
324+
"holders": v1_license["holders"],
325+
}
326+
if license_body["url"] == "":
327+
license_body["url"] = "https://dbpedia.org/page/All_rights_reserved"
328+
license_v2_endpoint = f"{CLOWDER_V2}/api/v2/licenses?"
329+
response = requests.post(
330+
license_v2_endpoint, headers=headers, json=license_body
331+
)
332+
print(response.json())
333+
license_id = response.json()["id"]
334+
return license_id
335+
336+
276337
def create_v2_dataset(dataset, headers):
277338
"""Create a dataset in Clowder v2."""
278339
# TODO: GET correct license
279-
dataset_in_v2_endpoint = f"{CLOWDER_V2}/api/v2/datasets?license_id=CC BY"
340+
print("Creating dataset license in Clowder v2.")
341+
v2_license_id = add_dataset_license(dataset["license"], headers)
342+
343+
dataset_in_v2_endpoint = f"{CLOWDER_V2}/api/v2/datasets?license_id={v2_license_id}"
280344
dataset_example = {
281345
"name": dataset["name"],
282346
"description": dataset["description"],
@@ -592,6 +656,13 @@ def build_collection_hierarchy(collection_id, headers):
592656
return children
593657

594658

659+
def build_collection_metadata_for_v1_dataset(dataset_id, user_v1, headers):
660+
dataset_collections = get_clowder_v1_dataset_collections(
661+
headers=headers, user_v1=user_v1, dataset_id=dataset_id
662+
)
663+
return dataset_collections
664+
665+
595666
def build_collection_space_metadata_for_v1_dataset(dataset, user_v1, headers):
596667
dataset_id = dataset["id"]
597668
dataset_collections = get_clowder_v1_dataset_collections(
@@ -660,6 +731,9 @@ def process_user_and_resources(user_v1, USER_MAP, DATASET_MAP):
660731
files_result = files_response.json()
661732

662733
for file in files_result:
734+
file_v2_id = download_and_upload_file(
735+
file, all_dataset_folders, dataset_v2_id, base_user_headers_v2
736+
)
663737
if file_v2_id is not None:
664738
add_file_metadata(file, file_v2_id, clowder_headers_v1, user_headers_v2)
665739
# posting the collection hierarchy as metadata
@@ -689,6 +763,7 @@ def process_user_and_resources(user_v1, USER_MAP, DATASET_MAP):
689763
print(
690764
f"Failed to add collection info as metadata in Clowder v2. Status code: {response.status_code}"
691765
)
766+
692767
if file_v2_id is not None:
693768
add_file_metadata(file, file_v2_id, clowder_headers_v1, user_headers_v2)
694769

0 commit comments

Comments
 (0)