Skip to content

Commit

Permalink
Update message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorrell committed Sep 18, 2024
1 parent 27a5070 commit 21e7cd7
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions caltechdata_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,22 @@ 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"
"-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()
Expand All @@ -415,7 +415,11 @@ def create_record(production):
if existing_data:
if filepath != "":
response = caltechdata_write(
existing_data, token, filepath, production=production, publish=False
existing_data,
token,
filepath,
production=production,
publish=False,
)
elif file_link != "":
response = caltechdata_write(
Expand Down Expand Up @@ -492,7 +496,6 @@ def create_record(production):
metadata, token, production=production, publish=False
)
rec_id = response


print_upload_message(rec_id, production)
with open(response + ".json", "w") as file:
Expand All @@ -503,20 +506,29 @@ 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/"
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
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}/`"""
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
Expand All @@ -533,30 +545,38 @@ 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":
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"
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"

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)
url_draft = API_URL_TEMPLATE_DRAFT.format(record_id=record_id)

response = requests.get(url)
response_draft = requests.get(url_draft)

filepath, file_link = upload_supporting_file(record_id)
print(file_link)

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): ").lower() == "y"

keepfile = (
input("Do you want to keep existing files? (y/n): ").lower() == "y"
)

if filepath != "":
response = caltechdata_edit(
record_id,
Expand All @@ -576,12 +596,9 @@ def edit_record(production):
publish=False,
keepfile=keepfile,
)

rec_id = response
print_upload_message(rec_id, production)





def download_file_by_id(record_id, token=None):
Expand Down

0 comments on commit 21e7cd7

Please sign in to comment.