Commit 74270774 authored by Eirik Martin Danielsen's avatar Eirik Martin Danielsen 💬
Browse files

using semaphors to safe access to database

parent edc5580e
......@@ -7,7 +7,7 @@ from api.models.dbmodels import Location, Salamander, User
import re
import os
from api.endpoints.matchsalamander import sanitize_int_str
from path_constants import _ACCESS_DATABASE
LATITUDE_REGEX = "^(\\+|-)?(?:90(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\\.[0-9]{1,6})?))$"
LONGITUDE_REGEX = "^(\\+|-)?(?:180(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\\.[0-9]{1,6})?))$"
......@@ -21,17 +21,18 @@ class LocationEndpoint(Resource):
def post():
data = request.form
if "name" in data and "radius" in data and "latitude" in data and "longitude" in data:
location = db.session.query(Location).filter_by(name=data['name'].lower()).first()
if location is None and re.search(LATITUDE_REGEX, data['latitude']) and re.search(LONGITUDE_REGEX,
with _ACCESS_DATABASE:
location = db.session.query(Location).filter_by(name=data['name'].lower()).first()
if location is None and re.search(LATITUDE_REGEX, data['latitude']) and re.search(LONGITUDE_REGEX,
data['longitude']):
new_location = Location(name=data['name'].lower(), longitude=data['longitude'],
new_location = Location(name=data['name'].lower(), longitude=data['longitude'],
latitude=data['latitude'], radius=sanitize_int_str(str(data['radius'])))
db.session.add(new_location)
db.session.commit()
create_directory_folders(data['name'].lower())
return jsonify({"message": "new location registered", "status": 200})
else:
return jsonify({"message": "this location name already in use or bad coordinates", "status": 400})
db.session.add(new_location)
db.session.commit()
create_directory_folders(data['name'].lower())
return jsonify({"message": "new location registered", "status": 200})
else:
return jsonify({"message": "this location name already in use or bad coordinates", "status": 400})
else:
return jsonify({"message": "wrong data", "status": 400})
......@@ -54,30 +55,31 @@ class LocationEndpoint(Resource):
data = request.form
if user.admin:
if "id" in data:
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()
path = os.path.join("images", location.name)
dir_list = os.listdir(path)
for directory in dir_list:
species_path = os.path.join(path, directory)
gender_dir_list = os.listdir(species_path)
for gender in gender_dir_list:
os.rmdir(os.path.join(species_path, gender))
os.rmdir(species_path)
os.rmdir(path)
return jsonify({"message": location.name + " deleted.", "status": 200})
with _ACCESS_DATABASE:
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()
path = os.path.join("images", location.name)
dir_list = os.listdir(path)
for directory in dir_list:
species_path = os.path.join(path, directory)
gender_dir_list = os.listdir(species_path)
for gender in gender_dir_list:
os.rmdir(os.path.join(species_path, gender))
os.rmdir(species_path)
os.rmdir(path)
return jsonify({"message": location.name + " deleted.", "status": 200})
else:
return jsonify({"message": "location doesn't exist", "status": 400})
else:
return jsonify({"message": "location doesn't exist", "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": "location needs to be empty." + " There are " + str(len(salamanders))
+ " salamanders in this location.", "status": 400})
else:
return jsonify({"message": "query failed", "status": 400})
return jsonify({"message": "query failed", "status": 400})
else:
return jsonify({"message": "wrong data", "status": 400})
else:
......@@ -91,25 +93,26 @@ class LocationEndpoint(Resource):
data = request.form
if user.admin:
if "id" in data:
location = db.session.query(Location).filter_by(id=data['id']).first()
if location:
if "new_radius" in data:
location.radius = sanitize_int_str(str(data['new_radius']))
db.session.commit()
if "new_name" in data:
folder_path = os.path.join(os.path.abspath("./images"), location.name.lower())
if os.path.isdir(folder_path):
new_path = os.path.join(os.path.abspath("./images"), data['new_name'].lower())
if not os.path.isdir(new_path):
os.rename(folder_path, new_path)
location.name = data['new_name'].lower()
db.session.commit()
return jsonify({"message": "location updated", "status": 200})
else:
return jsonify({"message": "location folder was not found", "status": 400})
return jsonify({"location": location.id, "status": 200})
else:
return jsonify({"message": "location doesn't exist", "status": 400})
with _ACCESS_DATABASE:
location = db.session.query(Location).filter_by(id=data['id']).first()
if location:
if "new_radius" in data:
location.radius = sanitize_int_str(str(data['new_radius']))
db.session.commit()
if "new_name" in data:
folder_path = os.path.join(os.path.abspath("./images"), location.name.lower())
if os.path.isdir(folder_path):
new_path = os.path.join(os.path.abspath("./images"), data['new_name'].lower())
if not os.path.isdir(new_path):
os.rename(folder_path, new_path)
location.name = data['new_name'].lower()
db.session.commit()
return jsonify({"message": "location updated", "status": 200})
else:
return jsonify({"message": "location folder was not found", "status": 400})
return jsonify({"location": location.id, "status": 200})
else:
return jsonify({"message": "location doesn't exist", "status": 400})
else:
return jsonify({"message": "wrong data", "status": 400})
else:
......
......@@ -7,6 +7,7 @@ from re import sub
from api import db, limiter
from api.models.dbmodels import Salamander, SalamanderGrowth, Location
from algorithm.brute_force_matching import match_file_structure
from path_constants import _ACCESS_DATABASE
APPROVED_SEX = ["male", "female", "juvenile"]
APPROVED_SPECIES = ["smooth_newt", "northern_crested_newt"]
......@@ -26,44 +27,45 @@ class MatchSalamander(Resource):
if "sex" in data and "location" in data and "species" in data:
if data['sex'] in APPROVED_SEX and data['species'] in APPROVED_SPECIES and data['location'].lower() in approved_locations:
temp_img_path = os.path.abspath("./temp_images/")
img_path = os.path.abspath("./images/")
list_of_name = glob.glob(os.path.join(temp_img_path, str(user_id) + '.*'))
list_of_name_str = glob.glob(os.path.join(temp_img_path, str(user_id) + '_str.*'))
if len(list_of_name_str) > 0 and len(list_of_name) > 0:
img_name = list_of_name[0]
img_str_name = list_of_name_str[0]
path_to_processed_image = os.path.join(temp_img_path, img_str_name)
path_to_original_image = os.path.join(temp_img_path, img_name)
path_to_images = os.path.join(img_path, data['location'].lower(), data['species'], data['sex'])
weight = None
length = None
if "weight" in data:
weight = sanitize_number_str(str(data['weight']))
if "length" in data:
length = sanitize_number_str(str(data['length']))
if data['sex'] != "juvenile":
result = match_file_structure(path_to_processed_image, path_to_images)
if result:
if result > -1:
number_of_files = int(len(os.listdir(os.path.join(path_to_images, str(result)))) / 2)
add_salamander_growth(weight, length, result, number_of_files)
move_and_rename_images(os.path.join(path_to_images, str(result)), path_to_original_image, path_to_processed_image)
return jsonify({"matching": "Match!", "message": "Matched with salamander", "id": result, 'status': 200})
with _ACCESS_DATABASE:
temp_img_path = os.path.abspath("./temp_images/")
img_path = os.path.abspath("./images/")
list_of_name = glob.glob(os.path.join(temp_img_path, str(user_id) + '.*'))
list_of_name_str = glob.glob(os.path.join(temp_img_path, str(user_id) + '_str.*'))
if len(list_of_name_str) > 0 and len(list_of_name) > 0:
img_name = list_of_name[0]
img_str_name = list_of_name_str[0]
path_to_processed_image = os.path.join(temp_img_path, img_str_name)
path_to_original_image = os.path.join(temp_img_path, img_name)
path_to_images = os.path.join(img_path, data['location'].lower(), data['species'], data['sex'])
weight = None
length = None
if "weight" in data:
weight = sanitize_number_str(str(data['weight']))
if "length" in data:
length = sanitize_number_str(str(data['length']))
if data['sex'] != "juvenile":
result = match_file_structure(path_to_processed_image, path_to_images)
if result:
if result > -1:
number_of_files = int(len(os.listdir(os.path.join(path_to_images, str(result)))) / 2)
add_salamander_growth(weight, length, result, number_of_files)
move_and_rename_images(os.path.join(path_to_images, str(result)), path_to_original_image, path_to_processed_image)
return jsonify({"matching": "Match!", "message": "Matched with salamander", "id": result, 'status': 200})
else:
salamander_id = add_salamander(data['location'], data['species'], data['sex'], path_to_images, user_id, path_to_original_image, path_to_processed_image)
number_of_files = int(len(os.listdir(os.path.join(path_to_images, str(salamander_id)))) / 2) - 1
add_salamander_growth(weight, length, salamander_id, number_of_files)
return jsonify({"matching": "No Match.", "message": "New salamander in database", "id": salamander_id, 'status': 200})
else:
salamander_id = add_salamander(data['location'], data['species'], data['sex'], path_to_images, user_id, path_to_original_image, path_to_processed_image)
number_of_files = int(len(os.listdir(os.path.join(path_to_images, str(salamander_id)))) / 2) - 1
add_salamander_growth(weight, length, salamander_id, number_of_files)
return jsonify({"matching": "No Match.", "message": "New salamander in database", "id": salamander_id, 'status': 200})
return jsonify({"message": "Internal server error", 'status': 500})
else:
return jsonify({"message": "Internal server error", 'status': 500})
salamander_id = add_salamander(data['location'], data['species'], data['sex'], path_to_images, user_id, path_to_original_image, path_to_processed_image)
number_of_files = int(len(os.listdir(os.path.join(path_to_images, str(salamander_id)))) / 2) - 1
add_salamander_growth(weight, length, salamander_id, number_of_files)
return jsonify({"matching": "No Match.", "message": "New juvenile in database", "id": salamander_id, 'status': 200})
else:
salamander_id = add_salamander(data['location'], data['species'], data['sex'], path_to_images, user_id, path_to_original_image, path_to_processed_image)
number_of_files = int(len(os.listdir(os.path.join(path_to_images, str(salamander_id)))) / 2) - 1
add_salamander_growth(weight, length, salamander_id, number_of_files)
return jsonify({"matching": "No Match.", "message": "New juvenile in database", "id": salamander_id, 'status': 200})
else:
return jsonify({"message": "internal server error", 'status': 500})
return jsonify({"message": "internal server error", 'status': 500})
else:
return jsonify({"message": "invalid sex, species or location", 'status': 400})
else:
......
......@@ -6,7 +6,7 @@ from api.models.dbmodels import Location, User, Salamander
import os
import glob
from image_encoder.image_encoder import *
from path_constants import _ACCESS_DATABASE
# get all images on a specific salamander based on id
class SalamanderEndpoint(Resource):
......@@ -18,22 +18,23 @@ class SalamanderEndpoint(Resource):
user_id = get_jwt_identity()
user = db.session.query(User).filter_by(id=user_id).first()
if user.admin:
salamander = db.session.query(Salamander).filter_by(id=salamander_id).first()
if salamander:
location = db.session.query(Location).filter_by(id=salamander.location_id).first()
salamander_images = []
path_to_salamander_images = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id))
list_of_paths = glob.glob(os.path.join(path_to_salamander_images, '*.*'))
for path in list_of_paths:
if not path.__contains__("_str"):
encoded = encode(path)[2:-1]
image_id = os.path.basename(path)[0]
image_data = {"id": image_id, "url": "data:image/png;base64," + encoded}
salamander_images.append(image_data)
with _ACCESS_DATABASE:
salamander = db.session.query(Salamander).filter_by(id=salamander_id).first()
if salamander:
location = db.session.query(Location).filter_by(id=salamander.location_id).first()
salamander_images = []
path_to_salamander_images = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id))
list_of_paths = glob.glob(os.path.join(path_to_salamander_images, '*.*'))
for path in list_of_paths:
if not path.__contains__("_str"):
encoded = encode(path)[2:-1]
image_id = os.path.basename(path)[0]
image_data = {"id": image_id, "url": "data:image/png;base64," + encoded}
salamander_images.append(image_data)
salamander_images.sort(key=lambda x: x.get('id'))
return jsonify({"images": salamander_images, "location": location.name, "id": salamander.id, "sex": salamander.sex, "species": salamander.species, 'status': 200})
else:
return jsonify({"message": "no salamander with this id", 'status': 400})
salamander_images.sort(key=lambda x: x.get('id'))
return jsonify({"images": salamander_images, "location": location.name, "id": salamander.id, "sex": salamander.sex, "species": salamander.species, 'status': 200})
else:
return jsonify({"message": "no salamander with this id", 'status': 400})
else:
return jsonify({"message": "no access to this endpoint", 'status': 400})
......@@ -8,7 +8,7 @@ from algorithm.brute_force_matching import match_file_structure
from api.endpoints.matchsalamander import sanitize_number_str
import glob
from shutil import move
from path_constants import _ACCESS_DATABASE
class SalamanderClass(Resource):
decorators = [limiter.limit("60/minute")]
......@@ -20,47 +20,48 @@ class SalamanderClass(Resource):
if "id" in data and "image_id" in data and "location" in data:
user_id = get_jwt_identity()
user = db.session.query(User).filter_by(id=user_id).first()
salamander = db.session.query(Salamander).filter_by(id=data['id']).first()
location = db.session.query(Location).filter_by(name=data['location']).first()
if user.admin and salamander and location:
salamander_path = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id))
img_id = int(data['image_id'])
last_id = int(len(os.listdir(salamander_path)) / 2) - 1
if img_id >= int(len(os.listdir(salamander_path)) / 2):
return jsonify({"message": "this image id does not exist", "status": 400})
original_path = glob.glob(os.path.join(salamander_path, str(img_id) + '.*'))[0]
processed_path = glob.glob(os.path.join(salamander_path, str(img_id) + '_str.*'))[0]
os.remove(original_path)
os.remove(processed_path)
delete_growth_row(salamander.id, img_id)
# 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": img_id, "message": "salamander deleted", 'status': 200})
else:
if img_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]
with _ACCESS_DATABASE:
salamander = db.session.query(Salamander).filter_by(id=data['id']).first()
location = db.session.query(Location).filter_by(name=data['location']).first()
if user.admin and salamander and location:
salamander_path = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id))
img_id = int(data['image_id'])
last_id = int(len(os.listdir(salamander_path)) / 2) - 1
original_extension = os.path.splitext(original_path)[1]
processed_extension = os.path.splitext(processed_path)[1]
if img_id >= int(len(os.listdir(salamander_path)) / 2):
return jsonify({"message": "this image id does not exist", "status": 400})
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
original_path = glob.glob(os.path.join(salamander_path, str(img_id) + '.*'))[0]
processed_path = glob.glob(os.path.join(salamander_path, str(img_id) + '_str.*'))[0]
os.remove(original_path)
os.remove(processed_path)
delete_growth_row(salamander.id, img_id)
# if last image was moved:
if len(os.listdir(salamander_path)) == 0:
os.rmdir(salamander_path)
db.session.delete(salamander)
db.session.commit()
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))
return jsonify({"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200})
else:
if img_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]
return jsonify({"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200})
else:
return jsonify({"message": "this user cannot delete this salamander", 'status': 400})
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 = img_id
db.session.commit()
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))
return jsonify({"id": salamander.id, "imageId": img_id, "message": "salamander deleted", 'status': 200})
else:
return jsonify({"message": "this user cannot delete this salamander", 'status': 400})
else:
return jsonify({"message": "wrong data in form", 'status': 400})
......@@ -71,58 +72,59 @@ class SalamanderClass(Resource):
user = db.session.query(User).filter_by(id=user_id).first()
data = request.form
if user.admin and "id" in data and "image_id" in data:
salamander = db.session.query(Salamander).filter_by(id=data['id']).first()
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:
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']:
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:
original_path = original_path[0]
processed_path = processed_path[0]
image_id = int(data['image_id'])
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)
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(new_path_to_images, str(result)),
path_to_original_image=original_path,
path_to_processed_image=processed_path)
with _ACCESS_DATABASE:
salamander = db.session.query(Salamander).filter_by(id=data['id']).first()
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:
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']:
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:
original_path = original_path[0]
processed_path = processed_path[0]
image_id = int(data['image_id'])
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)
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(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'], new_path_to_images, user_id, original_path, processed_path)
return handle_remaining_images(salamander, salamander_path, image_id,last_id)
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)
return jsonify({"message": "Salamander image, " + data['image_id'] + " does not exist", 'status': 400})
# if last image was moved:
else:
return jsonify({"message": "Salamander image, " + data['image_id'] + " does not exist", 'status': 400})
# if last image was moved:
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:
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)
return jsonify({"message": "Image id, " + data['image_id'] + " does not exist in salamander_id, " + data['id'], 'status': 400})
else:
return jsonify({"message": "Image id, " + data['image_id'] + " does not exist in salamander_id, " + data['id'], 'status': 400})
return jsonify({"message": "salamander cannot be moved to the same location", 'status': 400})
else:
return jsonify({"message": "salamander cannot be moved to the same location", 'status': 400})
return jsonify({"message": "salamander updated", 'status': 200})
else:
return jsonify({"message": "salamander updated", 'status': 200})
else:
return jsonify({"message": "Salamander ID" + data['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})
......
......@@ -6,7 +6,7 @@ from api.models.dbmodels import Location, User, Salamander, SalamanderGrowth
import os
import glob
from image_encoder.image_encoder import *
from path_constants import _ACCESS_DATABASE
# get one specific image on a specific salamander based on s_id and i_id
class SalamanderImageEndpoint(Resource):
......@@ -18,22 +18,23 @@ class SalamanderImageEndpoint(Resource):
user_id = get_jwt_identity()
user = db.session.query(User).filter_by(id=user_id).first()
if user.admin:
salamander = db.session.query(Salamander).filter_by(id=salamander_id).first()
salamander_growth = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id, image_id=image_id).first()
if salamander and salamander_growth:
location = db.session.query(Location).filter_by(id=salamander.location_id).first()
salamander_images = []
path_to_salamander_images = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id))
list_of_paths = glob.glob(os.path.join(path_to_salamander_images, '*.*'))
for path in list_of_paths:
basename = os.path.basename(path)
if basename.__contains__(str(image_id)):
encoded = encode(path)[2:-1]
image_data = {"url": "data:image/png;base64," + encoded}
salamander_images.append(image_data)
with _ACCESS_DATABASE:
salamander = db.session.query(Salamander).filter_by(id=salamander_id).first()
salamander_growth = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id, image_id=image_id).first()
if salamander and salamander_growth:
location = db.session.query(Location).filter_by(id=salamander.location_id).first()
salamander_images = []
path_to_salamander_images = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id))
list_of_paths = glob.glob(os.path.join(path_to_salamander_images, '*.*'))
for path in list_of_paths:
basename = os.path.basename(path)
if basename.__contains__(str(image_id)):
encoded = encode(path)[2:-1]
image_data = {"url": "data:image/png;base64," + encoded}
salamander_images.append(image_data)
return jsonify({"images": salamander_images, "length": salamander_growth.length, "weight": salamander_growth.weight, "imageId": image_id, "location": location.name, "salamanderId": salamander.id, "sex": salamander.sex, "species": salamander.species, 'status': 200})
else:
return jsonify({"message": "no salamander with this id or no growth data", 'status': 400})
return jsonify({"images": salamander_images, "length": salamander_growth.length, "weight": salamander_growth.weight, "imageId": image_id, "location": location.name, "salamanderId": salamander.id, "sex": salamander.sex, "species": salamander.species, 'status': 200})
else:
return jsonify({"message": "no salamander with this id or no growth data", 'status': 400})
else:
return jsonify({"message": "no access to this endpoint", 'status': 400})
......@@ -3,7 +3,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
from flask_restful import Resource
from api import db, limiter
from api.models.dbmodels import Location, User, Salamander, SalamanderGrowth
from path_constants import _ACCESS_DATABASE
# get all salamanders on a specific location
class SalamandersEndpoint(Resource):
......@@ -15,20 +15,21 @@ class SalamandersEndpoint(Resource):
user_id = get_jwt_identity()
user = db.session.query(User).filter_by(id=user_id).first()
if user.admin:
location = db.session.query(Location).filter_by(name=location_name.lower()).first()
if location:
salamanders = db.session.query(Salamander).filter_by(location_id=location.id).all()
if salamanders:
salamander_list = []
for salamander in salamanders:
number_of_images = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id).all()
last_date = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id).order_by(SalamanderGrowth.date.desc()).first()
ret = {"id": salamander.id, "sex": salamander.sex, "individuals": len(number_of_images), "species": salamander.species, "date": last_date.date}
salamander_list.append(ret)
return jsonify({"salamanders": salamander_list, 'status': 200})
with _ACCESS_DATABASE:
location = db.session.query(Location).filter_by(name=location_name.lower()).first()
if location:
salamanders = db.session.query(Salamander).filter_by(location_id=location.id).all()