From d9828f3d5711b9de39a6167978d58ac10a6a27ac Mon Sep 17 00:00:00 2001 From: Koa Date: Mon, 23 Jan 2023 10:00:42 -0500 Subject: [PATCH] Starting the implementation of save_city --- hub_api/city_commands.py | 83 ++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/hub_api/city_commands.py b/hub_api/city_commands.py index e190ec3..ff831d9 100644 --- a/hub_api/city_commands.py +++ b/hub_api/city_commands.py @@ -7,13 +7,16 @@ Copyright © 2023 Project Author Koa Wells kekoa.wells@concordia.ca import json from flask import Response, request from flask_restful import Resource -from imports.user_factory import UserFactory -from exports.user_factory import UserFactory as ExUserFactory -import os -from hub_logger import logger from hub_api.helpers.session_helper import refresh_session -from hub_api.helpers.auth import generate_auth_token, role_required +from hub_api.helpers.auth import role_required +import os +from imports.db_factory import DBFactory +from imports.user_factory import UserFactory +from imports.geometry_factory import GeometryFactory +from hub_logger import logger from persistence.models import UserRoles +from pathlib import Path +import os #Admin user commands """ @@ -21,7 +24,7 @@ SaveCity class """ class SaveCity(Resource): def __init__(self): - print() + pass @role_required([UserRoles.Admin.value]) def put(self): @@ -36,20 +39,62 @@ UpdateCity class """ class UpdateCity(Resource): def __init__(self): - print() + 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): + 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 + + allowed_ext = {'gml', '3dm', 'xml', 'obj', 'rhino'} + try: + city_file = request.files['city_file'] + ext = city_file.filename.rsplit('.', 1)[1].lower() + + if ext in allowed_ext: + city_file_type = ext + if ext == 'gml': + city_file_type = 'citygml' + elif ext == '3dm': + city_file_type = 'rhino' + + 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 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) """ DeleteCity class """ class DeleteCity(Resource): - def __init__(self): - print() + def __init__(self, city_id): + pass @role_required([UserRoles.Admin.value]) def delete(self): @@ -64,7 +109,7 @@ ListCities class """ class ListCities(Resource): def __init__(self): - print() + pass @role_required([UserRoles.Admin.value]) def put(self): @@ -77,22 +122,10 @@ SearchCity class """ class SearchCity(Resource): def __init__(self): - print() + pass - def get(self): + 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 -""" -UpdateCity class -""" -class UpdateCity(Resource): - def __init__(self): - print() - - def get(self): - session = refresh_session(request) - if session is None: - return Response(json.dumps({'error': 'invalid session'}), status=401) - headers = session.headers \ No newline at end of file