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

refactor delete and update location

parent dfc8d270
No related branches found
No related tags found
1 merge request!38Resolve "remove dead code and cleanup"
......@@ -23,8 +23,7 @@ class LocationEndpoint(Resource):
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, data['longitude']):
new_location = Location(name=data['name'].lower(), longitude=data['longitude'],
latitude=data['latitude'], radius=sanitize_int_str(str(data['radius'])))
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())
......@@ -53,7 +52,7 @@ class LocationEndpoint(Resource):
if user.admin:
if "id" in data:
salamanders = db.session.query(Salamander).filter_by(location_id=data['id'])
if salamanders and len(salamanders) == 0:
if salamanders is None:
location = db.session.query(Location).filter_by(id=data['id']).first()
if location:
db.session.delete(location)
......@@ -81,8 +80,8 @@ class LocationEndpoint(Resource):
if "new_name" in data:
folder_path = os.path.join("/images", location.name)
if os.path.isdir(folder_path):
new_path = os.rename(folder_path,os.path.join("/images", data['new_name'].lower()))
if not os.path.isdir(folder_path):
new_path = os.path.join("/images", data['new_name'].lower())
if not os.path.isdir(new_path):
os.rename(folder_path, new_path)
location.name = data['new_name'].lower()
else:
......@@ -91,12 +90,8 @@ class LocationEndpoint(Resource):
return jsonify({"message": "location folder was not found", "status": 400})
if "new_radius" in data:
location.radius = sanitize_int_str(str(data['new_radius']))
if "new_latitude" in data:
location.latitude = data['new_latitude']
if "new_longitude" in data:
location.longitude = data['new_longitude']
db.session.commit()
return jsonify({"location": str(location.id), " updated.": 200})
return jsonify({"location": location.id, " updated.": 200})
else:
return jsonify({"message": "location doesn't exist", "status": 400})
else:
......@@ -104,6 +99,7 @@ class LocationEndpoint(Resource):
else:
return jsonify({"message": "user not admin", "status": 400})
def create_directory_folders(location):
dir_smooth_male = "images/" + location + "/smooth_newt/" + "male/"
dir_smooth_female = "images/" + location + "/smooth_newt/" + "female/"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment