From 375882f3b8b5e49defcec927f957e413b0b56338 Mon Sep 17 00:00:00 2001 From: RohanBhattaraiNP <152933030+RohanBhattaraiNP@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:47:32 +0545 Subject: [PATCH 1/3] Update cli.py --- caltechdata_api/cli.py | 62 ++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/caltechdata_api/cli.py b/caltechdata_api/cli.py index d14c62b..7519636 100644 --- a/caltechdata_api/cli.py +++ b/caltechdata_api/cli.py @@ -375,20 +375,34 @@ def upload_data_from_file(): except json.JSONDecodeError as e: print(f"Error: Invalid JSON format in the file '{filename}'. {str(e)}") +def parse_args(): + """Parse command-line arguments.""" + parser = argparse.ArgumentParser(description="CaltechDATA CLI tool.") + parser.add_argument( + "-test", + action="store_true", + help="Use test mode, sets production to False" + ) + args = parser.parse_args() + return args def main(): + args = parse_args() + + production = not args.test # Set production to False if -test flag is provided + choice = get_user_input( "Do you want to create or edit a CaltechDATA record? (create/edit): " ).lower() if choice == "create": - create_record() + create_record(production) elif choice == "edit": - edit_record() + edit_record(production) else: print("Invalid choice. Please enter 'create' or 'edit'.") -def create_record(): +def create_record(production): token = get_or_set_token() print("Using CaltechDATA token:", token) while True: @@ -401,7 +415,7 @@ def create_record(): if existing_data: if filepath != "": response = caltechdata_write( - existing_data, token, filepath, production=True, publish=False + existing_data, token, filepath, production=production, publish=False ) elif file_link != "": response = caltechdata_write( @@ -409,17 +423,26 @@ def create_record(): token, file_links=[file_link], s3_link=file_link, - production=True, + production=production, publish=False, ) else: response = caltechdata_write( - existing_data, token, production=True, publish=False + existing_data, token, production=production, publish=False ) rec_id = response - print( + if production == True: + + print( + f"""You can view and publish this record at + https://data.caltech.edu/uploads/{rec_id} + If you need to upload large files to S3, you can type + `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" + ) + else: + print( f"""You can view and publish this record at - https://data.caltech.edu/uploads/{rec_id} + https://data.caltechlibrary.dev/uploads/{rec_id} If you need to upload large files to S3, you can type `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" ) @@ -468,24 +491,33 @@ def create_record(): if confirm_upload(): if filepath != "": response = caltechdata_write( - metadata, token, filepath, production=True, publish=False + metadata, token, filepath, production=production, publish=False ) elif file_link != "": response = caltechdata_write( metadata, token, file_links=[file_link], - production=True, + production=production, publish=False, ) else: response = caltechdata_write( - metadata, token, production=True, publish=False + metadata, token, production=production, publish=False ) rec_id = response - print( + if production == True: + + print( + f"""You can view and publish this record at + https://data.caltech.edu/uploads/{rec_id} + If you need to upload large files to S3, you can type + `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" + ) + else: + print( f"""You can view and publish this record at - https://data.caltech.edu/uploads/{rec_id} + https://data.caltechlibrary.dev/uploads/{rec_id} If you need to upload large files to S3, you can type `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" ) @@ -498,7 +530,7 @@ def create_record(): print("Invalid choice. Please enter 'existing' or 'create'.") -def edit_record(): +def edit_record(production): record_id = input("Enter the CaltechDATA record ID: ") token = get_or_set_token() file_name = download_file_by_id(record_id, token) @@ -508,7 +540,7 @@ def edit_record(): with open(file_name, "r") as file: metadata = json.load(file) response = caltechdata_edit( - record_id, metadata, token, production=True, publish=False + record_id, metadata, token, production=production, publish=False ) if response: print("Metadata edited successfully.") From e76ddfd5dae9daff66dc875f74a322a938f92b86 Mon Sep 17 00:00:00 2001 From: RohanBhattaraiNP <152933030+RohanBhattaraiNP@users.noreply.github.com> Date: Wed, 18 Sep 2024 23:06:44 +0545 Subject: [PATCH 2/3] Update cli.py --- caltechdata_api/cli.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/caltechdata_api/cli.py b/caltechdata_api/cli.py index 7519636..75fb655 100644 --- a/caltechdata_api/cli.py +++ b/caltechdata_api/cli.py @@ -423,7 +423,7 @@ def create_record(production): token, file_links=[file_link], s3_link=file_link, - production=production, + production=True, publish=False, ) else: @@ -506,18 +506,17 @@ def create_record(production): metadata, token, production=production, publish=False ) rec_id = response - if production == True: + - print( - f"""You can view and publish this record at - https://data.caltech.edu/uploads/{rec_id} - If you need to upload large files to S3, you can type - `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" - ) + if production == True: + site_url = f" https://data.caltech.edu/uploads/{rec_id}" + else: - print( + site_url = f" https://data.caltechlibrary.dev/uploads/{rec_id}" + + print( f"""You can view and publish this record at - https://data.caltechlibrary.dev/uploads/{rec_id} + {site_url} If you need to upload large files to S3, you can type `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" ) From e5fdb48387477a73898fdfda42671b108c2af257 Mon Sep 17 00:00:00 2001 From: RohanBhattaraiNP <152933030+RohanBhattaraiNP@users.noreply.github.com> Date: Thu, 19 Sep 2024 00:10:03 +0545 Subject: [PATCH 3/3] Update cli.py -test --- caltechdata_api/cli.py | 79 ++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/caltechdata_api/cli.py b/caltechdata_api/cli.py index 75fb655..ae0681b 100644 --- a/caltechdata_api/cli.py +++ b/caltechdata_api/cli.py @@ -431,21 +431,7 @@ def create_record(production): existing_data, token, production=production, publish=False ) rec_id = response - if production == True: - - print( - f"""You can view and publish this record at - https://data.caltech.edu/uploads/{rec_id} - If you need to upload large files to S3, you can type - `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" - ) - else: - print( - f"""You can view and publish this record at - https://data.caltechlibrary.dev/uploads/{rec_id} - If you need to upload large files to S3, you can type - `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" - ) + print_upload_message(rec_id, production) break else: print("Going back to the main menu.") @@ -508,18 +494,7 @@ def create_record(production): rec_id = response - if production == True: - site_url = f" https://data.caltech.edu/uploads/{rec_id}" - - else: - site_url = f" https://data.caltechlibrary.dev/uploads/{rec_id}" - - print( - f"""You can view and publish this record at - {site_url} - If you need to upload large files to S3, you can type - `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/""" - ) + print_upload_message(rec_id, production) with open(response + ".json", "w") as file: json.dump(metadata, file, indent=2) break @@ -528,11 +503,20 @@ def create_record(production): else: print("Invalid choice. Please enter 'existing' or 'create'.") +def print_upload_message(rec_id, production): + base_url = "https://data.caltech.edu/uploads/" if production else "https://data.caltechlibrary.dev/uploads/" + print( + f"""You can view and publish this record at + {base_url}{rec_id} + If you need to upload large files to S3, you can type + `s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/`""" + ) def edit_record(production): record_id = input("Enter the CaltechDATA record ID: ") token = get_or_set_token() file_name = download_file_by_id(record_id, token) + if file_name: try: # Read the edited metadata file @@ -549,33 +533,36 @@ def edit_record(production): print(f"An error occurred during metadata editing: {e}") else: print("No metadata file found.") + choice = get_user_input("Do you want to add files? (y/n): ").lower() if choice == "y": - API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files" + if production: + API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files" + API_URL_TEMPLATE_DRAFT = "https://data.caltech.edu/api/records/{record_id}/draft/files" + else: + API_URL_TEMPLATE = "https://data.caltechlibrary.dev/api/records/{record_id}/files" + API_URL_TEMPLATE_DRAFT = "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files" + url = API_URL_TEMPLATE.format(record_id=record_id) - - API_URL_TEMPLATE2 = ( - "https://data.caltech.edu/api/records/{record_id}/draft/files" - ) - url2 = API_URL_TEMPLATE2.format(record_id=record_id) + url_draft = API_URL_TEMPLATE_DRAFT.format(record_id=record_id) + response = requests.get(url) - response2 = requests.get(url2) + response_draft = requests.get(url_draft) + filepath, file_link = upload_supporting_file(record_id) print(file_link) - if response.status_code == 404 and response2.status_code == 404: + + if response.status_code == 404 and response_draft.status_code == 404: keepfile = False else: - keepfile = input("Do you want to keep existing files? y/n: ") - if keepfile == "y": - keepfile = True - else: - keepfile = False + keepfile = input("Do you want to keep existing files? (y/n): ").lower() == "y" + if filepath != "": response = caltechdata_edit( record_id, token=token, files=filepath, - production=True, + production=production, publish=False, keepfiles=keepfile, ) @@ -585,14 +572,16 @@ def edit_record(production): metadata, token=token, file_links=file_link, - production=True, + production=production, publish=False, keepfile=keepfile, ) + rec_id = response - print( - f"You can view and publish this record at https://data.caltech.edu/uploads/{rec_id}\n" - ) + print_upload_message(rec_id, production) + + + def download_file_by_id(record_id, token=None):