Commit fd0cb5d8 authored by Herman Andersen Dyrkorn's avatar Herman Andersen Dyrkorn
Browse files

Merge branch '47-refactor-edit-salamander' into 'master'

Resolve "refactor edit salamander"

Closes #47

See merge request !50
parents cc3ffaf8 3724d2c8
......@@ -138,15 +138,14 @@ class EditSalamander(Resource):
if salamander:
growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id,
image_id=int(data['image_id'])).first()
if "weight" in data:
if "weight" in data and "length" in data:
growth_row.weight = sanitize_number_str(str(data['weight']))
db.session.commit()
if "length" in data:
growth_row.length = sanitize_number_str(str(data['length']))
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']:
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'],
......@@ -163,7 +162,6 @@ class EditSalamander(Resource):
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.:
......@@ -172,32 +170,34 @@ class EditSalamander(Resource):
str(result)),
path_to_original_image=original_path,
path_to_processed_image=processed_path)
new_id = result
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,
True, result)
True, result, new_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)
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})
else:
return jsonify(
{"message": "salamander cannot be moved to the same location", 'status': 400})
return jsonify({"message": "salamander data updated", 'status': 200})
else:
return jsonify({"message": "salamander updated", 'status': 200})
return jsonify({"message": "wrong data", 'status': 400})
else:
return jsonify({"message": "Salamander ID" + data['id'] + " does not exist", 'status': 400})
else:
......@@ -212,7 +212,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)
......@@ -222,7 +222,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:
......@@ -240,7 +240,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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment