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

Merge branch '35-remove-dead-code-and-cleanup' of...

Merge branch '35-remove-dead-code-and-cleanup' of https://git.gvk.idi.ntnu.no/HermanDyrkorn/salamander-api into 35-remove-dead-code-and-cleanup
parents e408efe4 2a100411
Branches
No related tags found
1 merge request!38Resolve "remove dead code and cleanup"
......@@ -51,17 +51,21 @@ class LocationEndpoint(Resource):
data = request.form
if user.admin:
if "id" in data:
salamanders = db.session.query(Salamander).filter_by(location_id=data['id'])
if not salamanders:
salamanders = db.session.query(Salamander).filter_by(location_id=data['id']).all()
if salamanders is not None:
if len(salamanders) == 0:
location = db.session.query(Location).filter_by(id=data['id']).first()
if location:
db.session.delete(location)
db.session.commit()
return jsonify({"location": str(location.id), "deleted": 200})
return jsonify({"message": location.name + " deleted.", "status": 200})
else:
return jsonify({"message": "location doesn't exist", "status": 400})
else:
return jsonify({"message": "location needs to be empty", "status": 400})
return jsonify({"message": "location needs to be empty." + " There are " + str(len(salamanders))
+ " salamanders in this location." , "status": 400})
else:
return jsonify({"message": "query failed", "status": 400})
else:
return jsonify({"message": "wrong data", "status": 400})
else:
......
......@@ -89,42 +89,32 @@ class SalamanderClass(Resource):
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
path_to_images = os.path.join("images", data['new_location'].lower(), data['new_species'], data['new_sex'])
if data['new_sex'] != "juvenile":
result = match_file_structure(input_image=processed_path, match_directory=new_path_to_images)
if result:
# if salamander already exist:
if result > -1:
# move all images and delete previous ID.:
move_images(salamander.id, result, image_id,
path_to_salamander=os.path.join(path_to_images, str(result)),
path_to_salamander=os.path.join(new_path_to_images, str(result)),
path_to_original_image=original_path,
path_to_processed_image=processed_path)
else:
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()
return jsonify({"id": salamander.id, "imageId": image_id, "message": "salamander moved", 'status': 200})
else:
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_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=last_id).first()
salamander_growth_row.image_id = image_id
db.session.commit()
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", 'status': 200})
data['new_sex'], new_path_to_images, user_id, original_path, processed_path)
return handle_remaining_images(salamander, salamander_path, image_id,last_id)
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)
return handle_remaining_images(salamander, salamander_path, image_id, last_id)
else:
return jsonify({"message": "Image id, " + data['image_id'] + " does not exist in salamander_id, " + data['id'], 'status': 400})
else:
......@@ -144,6 +134,28 @@ 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, ):
# if last image was moved:
if len(os.listdir(salamander_path)) == 0:
os.rmdir(salamander_path)
db.session.delete(salamander)
db.session.commit()
return jsonify({"id": salamander.id, "imageId": image_id, "message": "salamander moved", 'status': 200})
else:
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_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=last_id).first()
salamander_growth_row.image_id = image_id
db.session.commit()
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", 'status': 200})
def lower_image_id_growth_row(salamander_id, old_id, new_id):
salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander_id, image_id=old_id).first()
if salamander_growth_row:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment