Skip to content
Snippets Groups Projects
Commit 1576f114 authored by Herman Andersen Dyrkorn's avatar Herman Andersen Dyrkorn
Browse files

Merge branch '34-rematch-function-and-endpoint' of...

Merge branch '34-rematch-function-and-endpoint' of https://git.gvk.idi.ntnu.no/HermanDyrkorn/salamander-api into 34-rematch-function-and-endpoint
parents 6f311e3a 4797456a
No related branches found
No related tags found
1 merge request!37Resolve "rematch function and endpoint"
......@@ -91,86 +91,68 @@ class SalamanderClass(Resource):
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'] + '.*'))[0]
processed_path = glob.glob(os.path.join(salamander_path, data['image_id'] + '_str.*'))[0]
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:
original_path = original_path[0]
processed_path = processed_path[0]
result = match_file_structure(input_image=processed_path, match_directory=new_path_to_images)
if result:
image_id = int(data['image_id'])
last_id = int(len(os.listdir(salamander_path)) / 2) - 1
debug_str = ""
path_to_images = os.path.join("images", data['new_location'].lower(), data['new_species'], data['new_sex'])
path_to_images = os.path.join("images", data['new_location'].lower(),
data['new_species'], data['new_sex'])
# if salamander already exist:
if result > -1:
# move all images and delete previous ID.:
debug_str = " Found a match with ID: " + str(result) + "."
move_images(salamander.id, result,image_id,path_to_salamander=os.path.join(path_to_images,str(result)),
move_images(salamander.id, result,image_id,
path_to_salamander=os.path.join(path_to_images,str(result)),
path_to_original_image=original_path,
path_to_processed_image=processed_path)
else:
debug_str = " It was a new salamander!"
add_salamander(salamander.id,image_id,data['new_location'], data['new_species'],data['new_sex'], path_to_images, user_id, original_path,
processed_path)
add_salamander(salamander.id,image_id,data['new_location'], data['new_species'],
data['new_sex'], path_to_images, user_id, original_path, processed_path)
# if last image was moved:
if len(os.listdir(salamander_path)) == 0:
os.rmdir(salamander_path)
db.session.delete(salamander)
db.session.commit()
debug_str = debug_str + " It was the last salamander in the folder."
return jsonify({"id": salamander.id, "imageId": image_id, "message": "salamander deleted",
'status': 200})
else:
original_image_path = sorted(glob.glob(os.path.join(salamander_path, '*[!_str].*')))[-1]
original_id = int(os.path.basename(original_image_path).split(".")[0])
processed_image_path = sorted(glob.glob(os.path.join(salamander_path, '*_str.*')))[-1]
if image_id != last_id:
original_path = glob.glob(os.path.join(salamander_path, str(last_id) + '.*'))[0]
processed_path = glob.glob(os.path.join(salamander_path, str(last_id) + '_str.*'))[0]
original_extension = os.path.splitext(original_image_path)[1]
processed_extension = os.path.splitext(processed_image_path)[1]
original_extension = os.path.splitext(original_path)[1]
processed_extension = os.path.splitext(processed_path)[1]
salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(
salamander_id=salamander.id, image_id=original_id).first()
salamander_id=salamander.id, image_id=last_id).first()
salamander_growth_row.image_id = image_id
db.session.commit()
os.rename(processed_image_path, os.path.join(salamander_path, str(image_id) + "_str" + processed_extension))
os.rename(original_image_path, os.path.join(salamander_path, str(image_id) + original_extension))
# Small note for future programmers by the guy who wrote this function:
# After a discussion we concluded that the file names were collapsed rather than moving
# the element into the open slot. This is slower, but we wanted to maintain chronological
# structure based on date. We do however not move and make space for a new element in the
# targeted location and therefore not necessarily maintain chronological structure.
# In other words, this is redundant work until that is fixed.
# DON'T DELETE! # DON'T DELETE! # DON'T DELETE!
# salamander_images = sorted(glob.glob(os.path.join(salamander_path, '*.*')))
# start_value = image_id
# for i in range(int(image_id*2),len(salamander_images)):
# image = salamander_images[i]
# name = os.path.basename(image)
# extension = os.path.splitext(image)[1]
# basename = name.split(".")[0]
#
# if basename.__contains__("_str"):
# os.rename(image, os.path.join(salamander_path,
# str(start_value) + "_str" + extension))
# lower_image_id_growth_row(salamander.id, start_value + 1, start_value)
# else:
# os.rename(image, os.path.join(salamander_path, str(start_value) + extension))
# lower_image_id_growth_row(salamander.id, start_value + 1, start_value)
# if i%2!=0:
# start_value += 1
# DON'T DELETE! # DON'T DELETE! # DON'T DELETE!
os.rename(processed_path,
os.path.join(salamander_path, str(image_id) + "_str" + processed_extension))
os.rename(original_path,
os.path.join(salamander_path, str(image_id) + original_extension))
return jsonify(
{"id": salamander.id, "message": "salamander moved. " + debug_str, 'status': 200})
else:
return jsonify(
{"message": "Salamander image, " + data['image_id'] + " does not exist", 'status': 400})
else:
return jsonify(
{"message": "Image id, " + data['image_id'] + " does not exist in salamander_id, " + str(salamander.id), 'status': 400})
else:
return jsonify({"message": "wrong data in form", 'status': 400})
return jsonify({"message": "wrong data in form", 'status': 400})
else:
return jsonify({"message": "Salamander ID" + str(salamander.id) + " does not exist", 'status': 400})
return jsonify({"message": "Salamander ID" + data['id'] + " does not exist", 'status': 400})
else:
return jsonify({"message": "Non admin users cannot move salamander", 'status': 400})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment