@@ -87,16 +87,18 @@ def register_image(self, image_file, text='', json_field=None, width=None):
8787 'user' : self .one .alyx .user , 'content_type' : self .content_type , 'object_id' : self .object_id ,
8888 'text' : text , 'width' : width , 'json' : json .dumps (json_field )}
8989 _logger .info (f'Registering image to { self .content_type } with id { self .object_id } ' )
90+ # to make sure an eventual note gets deleted with the image call the delete REST endpoint first
91+ current_note = self .one .alyx .rest ('notes' , 'list' ,
92+ django = f"object_id,{ self .object_id } ,text,{ text } ,json__name,{ text } " ,
93+ no_cache = True )
94+ if len (current_note ) == 1 :
95+ self .one .alyx .rest ('notes' , 'delete' , id = current_note [0 ]['id' ])
96+ # Open image for upload
97+ fig_open = open (image_file , 'rb' )
9098 # Catch error that results from object_id - content_type mismatch
9199 try :
92- # to make sure an eventual note gets deleted with the image call the delete REST endpoint first
93- current_note = self .one .alyx .rest ('notes' , 'list' ,
94- django = f"object_id,{ self .object_id } ,text,{ text } ,json__name,{ text } " ,
95- no_cache = True )
96- if len (current_note ) == 1 :
97- self .one .alyx .rest ('notes' , 'delete' , id = current_note [0 ]['id' ])
98- with open (image_file , 'rb' ) as fig_open :
99- note_db = self .one .alyx .rest ('notes' , 'create' , data = note , files = {'image' : fig_open })
100+ note_db = self .one .alyx .rest ('notes' , 'create' , data = note , files = {'image' : fig_open })
101+ fig_open .close ()
100102 return note_db
101103 except requests .HTTPError as e :
102104 if "matching query does not exist.'" in str (e ):
0 commit comments