Implement AddCity and DeleteCity API calls + comments

This commit is contained in:
KoaCWells 2023-01-24 15:21:53 -05:00
parent d9828f3d57
commit cea801a67f

View File

@ -18,32 +18,36 @@ 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):
"""
UpdateCity class performs an admin API call to update a city that already exists inside the database
"""
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 put(self, city_id, city):
def put(self, city_id):
session = refresh_session(request)
if session is None:
return Response(json.dumps({'error': 'invalid session'}), status=401)
@ -65,9 +69,8 @@ class UpdateCity(Resource):
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)
saved_city = self.db_factory.update_city(city.city_id, city)
if os.path.exists(file_path):
os.remove(file_path)
@ -76,7 +79,8 @@ class UpdateCity(Resource):
'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,
'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,
@ -89,40 +93,55 @@ class UpdateCity(Resource):
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
"""
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)
headers = session.headers
#Standard user commands
"""
ListCities class
"""
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
class ListCities(Resource):
def __init__(self):
pass
"""
ListCities class performs a standard API call to list all existing cities stored in the database
"""
@role_required([UserRoles.Admin.value])
def put(self):
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
"""
SearchCity class
"""
class SearchCity(Resource):
"""
SearchCity class performs a standard API call to select an existing city stored in the database
"""
def __init__(self):
pass
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)