Implement AddCity and DeleteCity API calls + comments
This commit is contained in:
parent
d9828f3d57
commit
cea801a67f
|
@ -18,114 +18,133 @@ from persistence.models import UserRoles
|
|||
from pathlib import Path
|
||||
import os
|
||||
|
||||
#Admin user commands
|
||||
"""
|
||||
SaveCity class
|
||||
"""
|
||||
class SaveCity(Resource):
|
||||
def __init__(self):
|
||||
pass
|
||||
# Admin user commands
|
||||
|
||||
@role_required([UserRoles.Admin.value])
|
||||
def put(self):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
# class SaveCity(Resource):
|
||||
# """
|
||||
# SaveCity class performs an admin API call to save an instantiated city into the database
|
||||
# """
|
||||
#
|
||||
# def __init__(self):
|
||||
# self.db_factory = DBFactory(db_name='hub_prod', app_env='PROD',
|
||||
# dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
#
|
||||
# # todo: implement feature to persist instantiated city into database
|
||||
# @role_required([UserRoles.Admin.value])
|
||||
# def put(self):
|
||||
# session = refresh_session(request)
|
||||
# if session is None:
|
||||
# return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
# headers = session.headers
|
||||
|
||||
|
||||
"""
|
||||
UpdateCity class
|
||||
"""
|
||||
class UpdateCity(Resource):
|
||||
def __init__(self):
|
||||
self.db_factory = DBFactory(db_name='hub_prod', app_env='PROD',
|
||||
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
"""
|
||||
UpdateCity class performs an admin API call to update a city that already exists inside the database
|
||||
"""
|
||||
|
||||
@role_required([UserRoles.Admin.value])
|
||||
def put(self, city_id, city):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
def __init__(self):
|
||||
self.db_factory = DBFactory(db_name='hub_prod', app_env='PROD',
|
||||
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
|
||||
allowed_ext = {'gml', '3dm', 'xml', 'obj', 'rhino'}
|
||||
try:
|
||||
city_file = request.files['city_file']
|
||||
ext = city_file.filename.rsplit('.', 1)[1].lower()
|
||||
@role_required([UserRoles.Admin.value])
|
||||
def put(self, city_id):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
|
||||
if ext in allowed_ext:
|
||||
city_file_type = ext
|
||||
if ext == 'gml':
|
||||
city_file_type = 'citygml'
|
||||
elif ext == '3dm':
|
||||
city_file_type = 'rhino'
|
||||
allowed_ext = {'gml', '3dm', 'xml', 'obj', 'rhino'}
|
||||
try:
|
||||
city_file = request.files['city_file']
|
||||
ext = city_file.filename.rsplit('.', 1)[1].lower()
|
||||
|
||||
file_path = (
|
||||
Path(__file__).parent.parent / 'data/uploaded_city/{}'.format(city_file.filename)).resolve()
|
||||
city_file.save(file_path)
|
||||
city = GeometryFactory(city_file_type, file_path).city
|
||||
db_factory = DBFactory(city=city, db_name='hub_prod', app_env='PROD',
|
||||
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
saved_city = db_factory.persist_city(1)
|
||||
if ext in allowed_ext:
|
||||
city_file_type = ext
|
||||
if ext == 'gml':
|
||||
city_file_type = 'citygml'
|
||||
elif ext == '3dm':
|
||||
city_file_type = 'rhino'
|
||||
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
if type(saved_city) is not dict:
|
||||
return Response(response=json.dumps({
|
||||
'id': saved_city.id, 'name': saved_city.name, 'srs_name': saved_city.srs_name,
|
||||
'time_zone': saved_city.time_zone, 'version': saved_city.city_version,
|
||||
'country': saved_city.country_code,
|
||||
'lat': saved_city.latitude, 'lon': saved_city.longitude, 'lower_corner': saved_city.lower_corner,
|
||||
'upper_corner': saved_city.upper_corner, 'created': saved_city.created,
|
||||
'updated': saved_city.updated,
|
||||
'user': {'id': saved_city.user.id, 'name': saved_city.user.name, 'email': saved_city.user.email,
|
||||
'role': saved_city.user.role.value}
|
||||
}, default=str), status=201)
|
||||
return Response(response=json.dumps(saved_city), status=200)
|
||||
else:
|
||||
return Response(response=json.dumps({'err_msg': 'Unknown city file type'}), status=400)
|
||||
file_path = (
|
||||
Path(__file__).parent.parent / 'data/uploaded_city/{}'.format(city_file.filename)).resolve()
|
||||
city_file.save(file_path)
|
||||
city = GeometryFactory(city_file_type, file_path).city
|
||||
|
||||
saved_city = self.db_factory.update_city(city.city_id, city)
|
||||
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
if type(saved_city) is not dict:
|
||||
return Response(response=json.dumps({
|
||||
'id': saved_city.id, 'name': saved_city.name, 'srs_name': saved_city.srs_name,
|
||||
'time_zone': saved_city.time_zone, 'version': saved_city.city_version,
|
||||
'country': saved_city.country_code,
|
||||
'lat': saved_city.latitude, 'lon': saved_city.longitude,
|
||||
'lower_corner': saved_city.lower_corner,
|
||||
'upper_corner': saved_city.upper_corner, 'created': saved_city.created,
|
||||
'updated': saved_city.updated,
|
||||
'user': {'id': saved_city.user.id, 'name': saved_city.user.name, 'email': saved_city.user.email,
|
||||
'role': saved_city.user.role.value}
|
||||
}, default=str), status=201)
|
||||
return Response(response=json.dumps(saved_city), status=200)
|
||||
else:
|
||||
return Response(response=json.dumps({'err_msg': 'Unknown city file type'}), status=400)
|
||||
|
||||
except Exception as err:
|
||||
logger.error(err)
|
||||
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while updating city'}), status=400)
|
||||
|
||||
except Exception as err:
|
||||
logger.error(err)
|
||||
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while updating city'}), status=400)
|
||||
"""
|
||||
DeleteCity class
|
||||
"""
|
||||
class DeleteCity(Resource):
|
||||
def __init__(self, city_id):
|
||||
pass
|
||||
"""
|
||||
DeleteCity class performs an admin API call to delete an existing city stored in the database
|
||||
"""
|
||||
|
||||
@role_required([UserRoles.Admin.value])
|
||||
def delete(self):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
def __init__(self):
|
||||
self.db_factory = DBFactory(db_name='hub_prod', app_env='PROD',
|
||||
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
|
||||
@role_required([UserRoles.Admin.value])
|
||||
def delete(self):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
|
||||
try:
|
||||
payload = request.get_json()
|
||||
return Response(json.dumps(response=DBFactory.delete_city(city_id=payload["city_id"]), status=201))
|
||||
except Exception as err:
|
||||
logger.error(err)
|
||||
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while deleting city'}), status=400)
|
||||
|
||||
|
||||
# Standard user commands
|
||||
|
||||
#Standard user commands
|
||||
"""
|
||||
ListCities class
|
||||
"""
|
||||
class ListCities(Resource):
|
||||
def __init__(self):
|
||||
pass
|
||||
"""
|
||||
ListCities class performs a standard API call to list all existing cities stored in the database
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.db_factory = DBFactory(db_name='hub_prod', app_env='PROD',
|
||||
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
|
||||
def get(self):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
|
||||
@role_required([UserRoles.Admin.value])
|
||||
def put(self):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
"""
|
||||
SearchCity class
|
||||
"""
|
||||
class SearchCity(Resource):
|
||||
def __init__(self):
|
||||
pass
|
||||
"""
|
||||
SearchCity class performs a standard API call to select an existing city stored in the database
|
||||
"""
|
||||
|
||||
def get(self, city_id):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
def __init__(self):
|
||||
self.db_factory = DBFactory(db_name='hub_prod', app_env='PROD',
|
||||
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||||
|
||||
def get(self, city_id):
|
||||
session = refresh_session(request)
|
||||
if session is None:
|
||||
return Response(json.dumps({'error': 'invalid session'}), status=401)
|
||||
headers = session.headers
|
||||
|
|
Loading…
Reference in New Issue
Block a user