@@ -433,16 +433,16 @@ def create_record():
433433
434434def edit_record ():
435435 record_id = input ("Enter the CaltechDATA record ID: " )
436- file_name = download_file_by_id (record_id )
436+ token = get_or_set_token ()
437+ file_name = download_file_by_id (record_id ,token )
437438 if file_name :
438439 try :
439440 # Read the edited metadata file
440441 with open (file_name , "r" ) as file :
441442 metadata = json .load (file )
442- token = get_or_set_token ()
443443 response = caltechdata_edit (
444- record_id , metadata , token , production = False , publish = False
445- )
444+ record_id , metadata , token , production = False , publish = False
445+ )
446446 if response :
447447 print ("Metadata edited successfully." )
448448 else :
@@ -454,48 +454,51 @@ def edit_record():
454454 choice = get_user_input ("Do you want to add files? (y/n): " ).lower ()
455455 if choice == "y" :
456456 filepath , file_link = upload_supporting_file (record_id )
457+ print (file_link )
457458 if filepath != "" :
458459 response = caltechdata_edit (
459- record_id , metadata , token , filepath , production = False , publish = False
460+ record_id , token = token , files = filepath , production = False , publish = False
460461 )
461462 elif file_link != "" :
462463 response = caltechdata_edit (
463464 record_id ,
464465 metadata ,
465- token ,
466+ token = token ,
466467 file_links = file_link ,
467468 production = False ,
468469 publish = False ,
469470 )
470- else :
471- response = caltechdata_edit (
472- record_id , metadata , token , production = False , publish = False
473- )
474- rec_id = response
475- print (f'You can view and publish this record at https://data.caltechlibrary.dev/uploads/{ rec_id } ' )
476- elif choice == "n" :
477- response = caltechdata_edit (
478- record_id , metadata , token , production = False , publish = False
479- )
480471 rec_id = response
481472 print (f'You can view and publish this record at https://data.caltechlibrary.dev/uploads/{ rec_id } ' )
482- else :
483- print ("Invalid choice. Please enter 'metadata' or 'files'." )
484473
485474
486- def download_file_by_id (record_id ):
487- url = f"https://data.caltechlibrary.dev/records/{ record_id } /export/datacite-json"
475+ def download_file_by_id (record_id ,token = None ):
476+ url = f"https://data.caltechlibrary.dev/api/records/{ record_id } "
477+
478+ headers = {
479+ "accept" : "application/vnd.datacite.datacite+json" ,
480+ }
481+
482+ if token :
483+ headers ["Authorization" ] = "Bearer %s" % token
488484
489485 try :
490- response = requests .get (url )
491- if response .status_code == 200 :
492- file_content = response .content
493- file_name = f"downloaded_data_{ record_id } .json"
494- with open (file_name , "wb" ) as file :
495- file .write (file_content )
496- print (f"Metadata downloaded successfully: { file_name } " )
497- with open (file_name , "r" ) as file :
498- metadata = json .load (file )
486+ response = requests .get (url ,headers = headers )
487+ if response .status_code != 200 :
488+ # Might have a draft
489+ response = requests .get (
490+ url + "/draft" ,
491+ headers = headers ,
492+ )
493+ if response .status_code != 200 :
494+ raise Exception (f"Record { record_id } does not exist, cannot edit" )
495+ file_content = response .content
496+ file_name = f"downloaded_data_{ record_id } .json"
497+ with open (file_name , "wb" ) as file :
498+ file .write (file_content )
499+ print (f"Metadata downloaded successfully: { file_name } " )
500+ with open (file_name , "r" ) as file :
501+ metadata = json .load (file )
499502 while True :
500503 print ("Fields:" )
501504 for i , field in enumerate (metadata .keys ()):
@@ -590,8 +593,6 @@ def download_file_by_id(record_id):
590593
591594 print (f"File updated successfully." )
592595
593- else :
594- print (f"Failed to download file. Status code: { response .status_code } " )
595596 except Exception as e :
596597 print (f"An error occurred: { e } " )
597598 return file_name
0 commit comments