@@ -81,33 +81,44 @@ def caltechdata_edit(
8181 for idv in ids :
8282 metadata ["id" ] = idv
8383
84- if files :
85- # Files to delete
84+ if files or delete :
85+ # Files to add or delete
8686 fjson = {}
8787 c = session ()
88- existing = c .get (api_url + idv )
89- file_info = existing .json ()["metadata" ]
88+ existing_meta = c .get (api_url + idv )
89+ file_info = existing_meta .json ()["metadata" ]
90+ existing = {}
91+ if "electronic_location_and_access" in file_info :
92+ for ex in file_info ["electronic_location_and_access" ]:
93+ name = ex ["electronic_name" ][0 ]
94+ fu = ex ["uniform_resource_identifier" ].split ("/" )[- 2 ]
95+ existing [name ] = fu
96+
97+ # File ids to delete
9098 fids = []
91- for f in files : # Check if new files match existing
92- if "electronic_location_and_access" in file_info :
93- for ex in file_info ["electronic_location_and_access" ]:
94- name = ex ["electronic_name" ][0 ]
95- fu = ex ["uniform_resource_identifier" ].split ("/" )[- 2 ]
96- if name == f :
99+
100+ if files :
101+ for f in files : # Check if new files match existing
102+ # In case we have a path
103+ fname = f .split ("/" )[- 1 ]
104+ if fname in existing :
105+ fids .append (existing [fname ])
106+ # Now upload new files
107+ fileinfo = [send_s3 (f , token , production ) for f in files ]
108+ fjson ["new" ] = fileinfo
109+
110+ if delete :
111+ for d in delete :
112+ if d in existing :
113+ fids .append (existing [d ])
114+ # Also check if we are deleting by extenson
115+ for e in existing .keys ():
116+ if e .split ("." )[- 1 ] == d :
97117 fids .append (fu )
98- for d in delete :
99- if name == d :
100- fids .append (fu )
101- if name .split ("." )[- 1 ] == d :
102- fids .append (fu )
118+
103119 if len (fids ) > 0 :
104120 fjson = {"delete" : fids }
105121
106- # upload new
107- print (files )
108- fileinfo = [send_s3 (f , token , production ) for f in files ]
109-
110- fjson ["new" ] = fileinfo
111122 metadata ["files" ] = fjson
112123
113124 dat = json .dumps ({"record" : metadata })
0 commit comments