@@ -75,6 +75,56 @@ def caltechdata_edit(
7575 if file_links :
7676 metadata = add_file_links (metadata , file_links )
7777
78+ if production == True :
79+ url = "https://data.caltech.edu"
80+ else :
81+ url = "https://data.caltechlibrary.dev"
82+
83+ headers = {
84+ "Authorization" : "Bearer %s" % token ,
85+ "Content-type" : "application/json" ,
86+ }
87+ f_headers = {
88+ "Authorization" : "Bearer %s" % token ,
89+ "Content-type" : "application/octet-stream" ,
90+ }
91+
92+ # Check status
93+ existing = requests .get (
94+ url + "/api/records/" + idv ,
95+ headers = headers ,
96+ )
97+ if existing .status_code != 200 :
98+ # Might have a draft
99+ existing = requests .get (
100+ url + "/api/records/" + idv + "/draft" ,
101+ headers = headers ,
102+ )
103+ if existing .status_code != 200 :
104+ raise Exception (existing .text )
105+
106+ status = existing .json ()["status" ]
107+
108+ # Determine whether we need a new version
109+ version = False
110+ if status == "published" and files :
111+ version = True
112+
113+ if new_version :
114+ version = True
115+
116+ if version :
117+ # We need to make new version
118+ result = requests .post (
119+ url + "/api/records/" + idv + "/versions" ,
120+ headers = headers ,
121+ )
122+ if result .status_code != 201 :
123+ raise Exception (result .text )
124+ # Get the id of the new version
125+ idv = result .json ()["id" ]
126+
127+ print (idv )
78128 # Pull out pid information
79129 if production == True :
80130 repo_prefix = "10.22002"
@@ -106,56 +156,34 @@ def caltechdata_edit(
106156 "provider" : "oai" ,
107157 }
108158 oai = True
109- #Records are not happy without the auto-assigned oai identifier
110- if oai == False :
159+ # Existing records are not happy without the auto-assigned oai identifier
160+ if oai == False and version == False :
111161 pids ["oai" ] = {
112162 "identifier" : f"oai:data.caltech.edu:{ idv } " ,
113163 "provider" : "oai" ,
114164 }
115- #We do not want to lose the auto-assigned DOI
116- #Users with custom DOIs must pass them in the metadata
117- if doi == False :
165+ # We do not want to lose the auto-assigned DOI
166+ # Users with custom DOIs must pass them in the metadata
167+ if doi == False and version == False :
118168 pids ["doi" ] = {
119- "identifier" : f' { repo_prefix } /{ idv } ' ,
120- "provider" : "datacite" ,
121- "client" : "datacite" ,
122- }
169+ "identifier" : f" { repo_prefix } /{ idv } " ,
170+ "provider" : "datacite" ,
171+ "client" : "datacite" ,
172+ }
123173 metadata ["pids" ] = pids
124174
125175 data = customize_schema .customize_schema (copy .deepcopy (metadata ), schema = schema )
126176
127- if production == True :
128- url = "https://data.caltech.edu"
129- else :
130- url = "https://data.caltechlibrary.dev"
131-
132- headers = {
133- "Authorization" : "Bearer %s" % token ,
134- "Content-type" : "application/json" ,
135- }
136- f_headers = {
137- "Authorization" : "Bearer %s" % token ,
138- "Content-type" : "application/octet-stream" ,
139- }
140-
141- if files or new_version :
142- # We need to make new version
177+ if files :
143178 data ["files" ] = {"enabled" : True }
144- result = requests .post (
145- url + "/api/records/" + idv + "/versions" ,
146- headers = headers ,
147- )
148- if result .status_code != 201 :
149- raise Exception (result .text )
150- # Get the id of the new version
151- idv = result .json ()["id" ]
152179 # Update metadata
153180 result = requests .put (
154181 url + "/api/records/" + idv + "/draft" ,
155182 headers = headers ,
156183 json = data ,
157184 )
158-
185+ if result .status_code != 200 :
186+ raise Exception (result .text )
159187 file_link = result .json ()["links" ]["files" ]
160188 write_files_rdm (files , file_link , headers , f_headers )
161189
@@ -181,7 +209,8 @@ def caltechdata_edit(
181209 if result .status_code != 200 :
182210 raise Exception (result .text )
183211 # We want files to stay the same as the existing record
184- data ["files" ] = result .json ()["files" ]
212+ data ["files" ] = existing .json ()["files" ]
213+ # Update metadata
185214 result = requests .put (
186215 url + "/api/records/" + idv + "/draft" ,
187216 headers = headers ,
0 commit comments