@@ -75,6 +75,56 @@ def caltechdata_edit(
75
75
if file_links :
76
76
metadata = add_file_links (metadata , file_links )
77
77
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 )
78
128
# Pull out pid information
79
129
if production == True :
80
130
repo_prefix = "10.22002"
@@ -106,56 +156,34 @@ def caltechdata_edit(
106
156
"provider" : "oai" ,
107
157
}
108
158
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 :
111
161
pids ["oai" ] = {
112
162
"identifier" : f"oai:data.caltech.edu:{ idv } " ,
113
163
"provider" : "oai" ,
114
164
}
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 :
118
168
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
+ }
123
173
metadata ["pids" ] = pids
124
174
125
175
data = customize_schema .customize_schema (copy .deepcopy (metadata ), schema = schema )
126
176
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 :
143
178
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" ]
152
179
# Update metadata
153
180
result = requests .put (
154
181
url + "/api/records/" + idv + "/draft" ,
155
182
headers = headers ,
156
183
json = data ,
157
184
)
158
-
185
+ if result .status_code != 200 :
186
+ raise Exception (result .text )
159
187
file_link = result .json ()["links" ]["files" ]
160
188
write_files_rdm (files , file_link , headers , f_headers )
161
189
@@ -181,7 +209,8 @@ def caltechdata_edit(
181
209
if result .status_code != 200 :
182
210
raise Exception (result .text )
183
211
# 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
185
214
result = requests .put (
186
215
url + "/api/records/" + idv + "/draft" ,
187
216
headers = headers ,
0 commit comments