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

Merge branch '46-re-match' into 'master'

Resolve "re-match"

Closes #46

See merge request !49
parents c821af96 09a74000
Branches
No related tags found
1 merge request!49Resolve "re-match"
...@@ -80,7 +80,8 @@ class EditSalamander(Resource): ...@@ -80,7 +80,8 @@ class EditSalamander(Resource):
salamander = db.session.query(Salamander).filter_by(id=data['id']).first() salamander = db.session.query(Salamander).filter_by(id=data['id']).first()
location = db.session.query(Location).filter_by(name=data['location']).first() location = db.session.query(Location).filter_by(name=data['location']).first()
if user.admin and salamander and location: if user.admin and salamander and location:
salamander_path = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id)) salamander_path = os.path.join("./images", location.name, salamander.species, salamander.sex,
str(salamander.id))
img_id = int(data['image_id']) img_id = int(data['image_id'])
last_id = int(len(os.listdir(salamander_path)) / 2) - 1 last_id = int(len(os.listdir(salamander_path)) / 2) - 1
...@@ -100,7 +101,8 @@ class EditSalamander(Resource): ...@@ -100,7 +101,8 @@ class EditSalamander(Resource):
os.rmdir(salamander_path) os.rmdir(salamander_path)
db.session.delete(salamander) db.session.delete(salamander)
db.session.commit() db.session.commit()
return jsonify({"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200}) return jsonify(
{"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200})
else: else:
if img_id != last_id: if img_id != last_id:
original_path = glob.glob(os.path.join(salamander_path, str(last_id) + '.*'))[0] original_path = glob.glob(os.path.join(salamander_path, str(last_id) + '.*'))[0]
...@@ -109,13 +111,16 @@ class EditSalamander(Resource): ...@@ -109,13 +111,16 @@ class EditSalamander(Resource):
original_extension = os.path.splitext(original_path)[1] original_extension = os.path.splitext(original_path)[1]
processed_extension = os.path.splitext(processed_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 = db.session.query(SalamanderGrowth).filter_by(
salamander_id=salamander.id, image_id=last_id).first()
salamander_growth_row.image_id = img_id salamander_growth_row.image_id = img_id
db.session.commit() db.session.commit()
os.rename(processed_path, os.path.join(salamander_path, str(img_id) + "_str" + processed_extension)) os.rename(processed_path,
os.path.join(salamander_path, str(img_id) + "_str" + processed_extension))
os.rename(original_path, os.path.join(salamander_path, str(img_id) + original_extension)) os.rename(original_path, os.path.join(salamander_path, str(img_id) + original_extension))
return jsonify({"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200}) return jsonify(
{"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200})
else: else:
return jsonify({"message": "this user cannot delete this salamander", 'status': 400}) return jsonify({"message": "this user cannot delete this salamander", 'status': 400})
else: else:
...@@ -131,7 +136,8 @@ class EditSalamander(Resource): ...@@ -131,7 +136,8 @@ class EditSalamander(Resource):
with _ACCESS_DATABASE: with _ACCESS_DATABASE:
salamander = db.session.query(Salamander).filter_by(id=data['id']).first() salamander = db.session.query(Salamander).filter_by(id=data['id']).first()
if salamander: if salamander:
growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id, image_id=int(data['image_id'])).first() 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:
growth_row.weight = sanitize_number_str(str(data['weight'])) growth_row.weight = sanitize_number_str(str(data['weight']))
db.session.commit() db.session.commit()
...@@ -139,9 +145,12 @@ class EditSalamander(Resource): ...@@ -139,9 +145,12 @@ class EditSalamander(Resource):
growth_row.length = sanitize_number_str(str(data['length'])) growth_row.length = sanitize_number_str(str(data['length']))
db.session.commit() db.session.commit()
if "location" in data and "new_location" in data and "new_sex" in data and "new_species" in data: 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_path = os.path.join("images", data['location'], salamander.species, salamander.sex, str(salamander.id)) salamander.species != data['new_species']:
new_path_to_images = os.path.join("images", data['new_location'], data['new_species'], data['new_sex']) 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'] + '.*')) 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.*')) processed_path = glob.glob(os.path.join(salamander_path, data['image_id'] + '_str.*'))
if len(original_path) > 0 and len(processed_path) > 0: if len(original_path) > 0 and len(processed_path) > 0:
...@@ -151,32 +160,42 @@ class EditSalamander(Resource): ...@@ -151,32 +160,42 @@ class EditSalamander(Resource):
last_id = int(len(os.listdir(salamander_path)) / 2) - 1 last_id = int(len(os.listdir(salamander_path)) / 2) - 1
if data['new_sex'] != "juvenile": 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 result:
# if salamander already exist: # if salamander already exist:
if result > -1: if result > -1:
# move all images and delete previous ID.: # move all images and delete previous ID.:
move_images(salamander.id, result, image_id, move_images(salamander.id, result, image_id,
path_to_salamander=os.path.join(new_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_original_image=original_path,
path_to_processed_image=processed_path) path_to_processed_image=processed_path)
else: else:
add_salamander(salamander.id, image_id, data['new_location'], data['new_species'], add_salamander(salamander.id, image_id, data['new_location'],
data['new_sex'], new_path_to_images, user_id, original_path, processed_path) data['new_species'],
return handle_remaining_images(salamander, salamander_path, image_id, last_id) 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)
else: else:
return jsonify({"message": "Salamander image, " + data['image_id'] + " does not exist", 'status': 400}) return jsonify(
{"message": "Salamander image, " + data['image_id'] + " does not exist",
'status': 400})
# if last image was moved: # if last image was moved:
else: else:
add_salamander(salamander.id, image_id, data['new_location'], data['new_species'], add_salamander(salamander.id, image_id, data['new_location'], data['new_species'],
data['new_sex'], new_path_to_images, user_id, original_path, data['new_sex'], new_path_to_images, user_id, original_path,
processed_path) processed_path)
return handle_remaining_images(salamander, salamander_path, image_id, last_id) return handle_remaining_images(salamander, salamander_path, image_id, last_id,
False, 0)
else: else:
return jsonify({"message": "Image id, " + data['image_id'] + " does not exist in salamander_id, " + data['id'], 'status': 400}) return jsonify({"message": "Image id, " + data[
'image_id'] + " does not exist in salamander_id, " + data['id'], 'status': 400})
else: else:
return jsonify({"message": "salamander cannot be moved to the same location", 'status': 400}) return jsonify(
{"message": "salamander cannot be moved to the same location", 'status': 400})
else: else:
return jsonify({"message": "salamander updated", 'status': 200}) return jsonify({"message": "salamander updated", 'status': 200})
else: else:
...@@ -186,19 +205,25 @@ class EditSalamander(Resource): ...@@ -186,19 +205,25 @@ class EditSalamander(Resource):
def delete_growth_row(salamander_id, image_id): def delete_growth_row(salamander_id, image_id):
salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander_id, image_id=image_id).first() salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander_id,
image_id=image_id).first()
if salamander_growth_row: if salamander_growth_row:
db.session.delete(salamander_growth_row) db.session.delete(salamander_growth_row)
db.session.commit() db.session.commit()
def handle_remaining_images(salamander, salamander_path: str, image_id: int, last_id: int, ): def handle_remaining_images(salamander, salamander_path: str, image_id: int, last_id: int, match_bool, result):
# if last image was moved: # if last image was moved:
if len(os.listdir(salamander_path)) == 0: if len(os.listdir(salamander_path)) == 0:
os.rmdir(salamander_path) os.rmdir(salamander_path)
db.session.delete(salamander) db.session.delete(salamander)
db.session.commit() db.session.commit()
return jsonify({"id": salamander.id, "imageId": image_id, "message": "salamander moved", 'status': 200}) if match_bool and result > -1:
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})
else: else:
if image_id != last_id: if image_id != last_id:
original_path = glob.glob(os.path.join(salamander_path, str(last_id) + '.*'))[0] original_path = glob.glob(os.path.join(salamander_path, str(last_id) + '.*'))[0]
...@@ -211,17 +236,23 @@ def handle_remaining_images(salamander, salamander_path: str, image_id: int, las ...@@ -211,17 +236,23 @@ def handle_remaining_images(salamander, salamander_path: str, image_id: int, las
db.session.commit() db.session.commit()
os.rename(processed_path, os.path.join(salamander_path, str(image_id) + "_str" + processed_extension)) 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)) os.rename(original_path, os.path.join(salamander_path, str(image_id) + original_extension))
return jsonify({"id": salamander.id, "message": "salamander moved", 'status': 200}) if match_bool and result > -1:
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})
def lower_image_id_growth_row(salamander_id, old_id, new_id): 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() salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander_id,
image_id=old_id).first()
if salamander_growth_row: if salamander_growth_row:
salamander_growth_row.image_id = new_id salamander_growth_row.image_id = new_id
db.session.commit() db.session.commit()
def move_images(salamander_id, new_salamander_id, image_id, path_to_salamander, path_to_original_image, path_to_processed_image): def move_images(salamander_id, new_salamander_id, image_id, path_to_salamander, path_to_original_image,
path_to_processed_image):
path_to_salamander = os.path.abspath(path_to_salamander) path_to_salamander = os.path.abspath(path_to_salamander)
split_name_org = str(path_to_original_image).split('.') split_name_org = str(path_to_original_image).split('.')
split_name_proc = str(path_to_processed_image).split('.') split_name_proc = str(path_to_processed_image).split('.')
...@@ -230,14 +261,16 @@ def move_images(salamander_id, new_salamander_id, image_id, path_to_salamander, ...@@ -230,14 +261,16 @@ def move_images(salamander_id, new_salamander_id, image_id, path_to_salamander,
b = os.path.join(path_to_salamander, str(number_of_files)) + "_str." + split_name_proc[len(split_name_org) - 1] b = os.path.join(path_to_salamander, str(number_of_files)) + "_str." + split_name_proc[len(split_name_org) - 1]
move(path_to_original_image, a) move(path_to_original_image, a)
move(path_to_processed_image, b) move(path_to_processed_image, b)
salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander_id, image_id=image_id).first() salamander_growth_row = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander_id,
image_id=image_id).first()
if salamander_growth_row: if salamander_growth_row:
salamander_growth_row.image_id = number_of_files salamander_growth_row.image_id = number_of_files
salamander_growth_row.salamander_id = new_salamander_id salamander_growth_row.salamander_id = new_salamander_id
db.session.commit() db.session.commit()
def add_salamander(old_salamander_id, image_id, location, species, sex, path_to_images, user_id, path_to_original_image, path_to_processed_image): def add_salamander(old_salamander_id, image_id, location, species, sex, path_to_images, user_id, path_to_original_image,
path_to_processed_image):
location_id = db.session.query(Location.id).filter_by(name=location.lower()).first() location_id = db.session.query(Location.id).filter_by(name=location.lower()).first()
new_salamander = Salamander(sex=sex, species=species, location_id=location_id[0], uid=user_id) new_salamander = Salamander(sex=sex, species=species, location_id=location_id[0], uid=user_id)
db.session.add(new_salamander) db.session.add(new_salamander)
...@@ -245,5 +278,6 @@ def add_salamander(old_salamander_id, image_id, location, species, sex, path_to_ ...@@ -245,5 +278,6 @@ def add_salamander(old_salamander_id, image_id, location, species, sex, path_to_
salamander_id = db.session.query(Salamander.id).filter_by(uid=user_id).order_by(Salamander.id.desc()).first() salamander_id = db.session.query(Salamander.id).filter_by(uid=user_id).order_by(Salamander.id.desc()).first()
path_for_new_salamander = os.path.join(path_to_images, str(salamander_id[0])) path_for_new_salamander = os.path.join(path_to_images, str(salamander_id[0]))
os.makedirs(path_for_new_salamander) os.makedirs(path_for_new_salamander)
move_images(old_salamander_id, salamander_id[0], image_id, path_for_new_salamander, path_to_original_image, path_to_processed_image) move_images(old_salamander_id, salamander_id[0], image_id, path_for_new_salamander, path_to_original_image,
path_to_processed_image)
return salamander_id[0] return salamander_id[0]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment