diff --git a/api/endpoints/editsalamander.py b/api/endpoints/editsalamander.py index 2b7f3716997e76c5a2aab8fe687cebfe9760f7a3..ab2fdc8bea9b4edcc334d577ecc418942534fb6b 100644 --- a/api/endpoints/editsalamander.py +++ b/api/endpoints/editsalamander.py @@ -144,9 +144,12 @@ class EditSalamander(Resource): db.session.commit() if "location" in data and "new_location" in data and "new_sex" in data and "new_species" in data: - if data['location'] != data['new_location'] or salamander.sex != data['new_sex'] or salamander.species != data['new_species']: - salamander_path = os.path.join("images", data['location'], salamander.species, salamander.sex, str(salamander.id)) - new_path_to_images = os.path.join("images", data['new_location'], data['new_species'], data['new_sex']) + if data['location'] != data['new_location'] or salamander.sex != data[ + 'new_sex'] or salamander.species != data['new_species']: + salamander_path = os.path.join("images", data['location'], salamander.species, + salamander.sex, str(salamander.id)) + new_path_to_images = os.path.join("images", data['new_location'], data['new_species'], + data['new_sex']) original_path = glob.glob(os.path.join(salamander_path, data['image_id'] + '.*')) processed_path = glob.glob(os.path.join(salamander_path, data['image_id'] + '_str.*')) if len(original_path) > 0 and len(processed_path) > 0: @@ -156,7 +159,8 @@ class EditSalamander(Resource): last_id = int(len(os.listdir(salamander_path)) / 2) - 1 if data['new_sex'] != "juvenile": - result = match_file_structure(input_image=processed_path, match_directory=new_path_to_images) + result = match_file_structure(input_image=processed_path, + match_directory=new_path_to_images) if result: # if salamander already exist: if result > -1: @@ -172,18 +176,19 @@ class EditSalamander(Resource): data['new_sex'], new_path_to_images, user_id, original_path, processed_path) return handle_remaining_images(salamander, salamander_path, image_id, last_id, - True, result) + True, result, 0) else: return jsonify( {"message": "Salamander image, " + data['image_id'] + " does not exist", 'status': 400}) # if last image was moved: else: - add_salamander(salamander.id, image_id, data['new_location'], data['new_species'], - data['new_sex'], new_path_to_images, user_id, original_path, - processed_path) + new_id = add_salamander(salamander.id, image_id, data['new_location'], + data['new_species'], + data['new_sex'], new_path_to_images, user_id, original_path, + processed_path) return handle_remaining_images(salamander, salamander_path, image_id, last_id, - False, 0) + False, 0, new_id) else: return jsonify({"message": "Image id, " + data[ 'image_id'] + " does not exist in salamander_id, " + data['id'], 'status': 400}) @@ -205,7 +210,7 @@ def delete_growth_row(salamander_id, image_id): db.session.commit() -def handle_remaining_images(salamander, salamander_path: str, image_id: int, last_id: int, match_bool, result): +def handle_remaining_images(salamander, salamander_path: str, image_id: int, last_id: int, match_bool, result, new_id): # if last image was moved: if len(os.listdir(salamander_path)) == 0: os.rmdir(salamander_path) @@ -215,7 +220,7 @@ def handle_remaining_images(salamander, salamander_path: str, image_id: int, las return jsonify({"id": result, "matching": "Match!", "message": "Matched with salamander", 'status': 200}) else: return jsonify( - {"id": salamander.id, "matching": "No match.", "message": "New salamander in database", 'status': 200}) + {"id": new_id, "matching": "No match.", "message": "New salamander in database", 'status': 200}) else: if image_id != last_id: @@ -233,7 +238,7 @@ def handle_remaining_images(salamander, salamander_path: str, image_id: int, las return jsonify({"id": result, "matching": "Match!", "message": "Matched with salamander", 'status': 200}) else: return jsonify( - {"id": salamander.id, "matching": "No match.", "message": "New salamander in database", 'status': 200}) + {"id": new_id, "matching": "No match.", "message": "New salamander in database", 'status': 200}) def lower_image_id_growth_row(salamander_id, old_id, new_id):