api_v1.4/hub_api/city_info.py
2023-02-01 15:48:14 -05:00

82 lines
3.4 KiB
Python

"""
City info
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
Code contributors: Peter Yefi peteryefi@gmail.com
"""
import json
from flask import Response, request, g
from flask_restful import Resource
from hub_api.helpers.auth import role_required
from hub.persistence.models import UserRoles
from hub.hub_logger import logger
from hub.imports.geometry_factory import GeometryFactory
from pathlib import Path
import os
from hub_api.config import Config
headers = {'Content-Type': 'application/json'}
class CityInfo(Resource, Config):
def __init__(self):
super().__init__()
@role_required([UserRoles.Admin.value])
def get(self, city_id):
city = self.get_city(city_id)
print(city.name)
if city:
return Response(response=json.dumps({
'id': city.id, 'name': city.name, 'srs_name': city.srs_name,
'time_zone': city.time_zone, 'version': city.city_version, 'country': city.country_code,
'lat': city.latitude, 'lon': city.longitude, 'lower_corner': city.lower_corner,
'upper_corner': city.upper_corner, 'created': city.created, 'updated': city.updated,
'user': {'id': city.user.id, 'name': city.user.name, 'email': city.user.email,
'role': city.user.role.value}
}, default=str), status=200, headers=headers)
return Response(response=json.dumps({'err_msg': 'City not found'}), status=404, headers=headers)
class City(Resource, Config):
def __init__(self):
super().__init__()
@role_required([UserRoles.Admin.value])
def post(self):
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
saved_city = self.import_db_factory.persist_city(g.user['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, headers=headers)
else:
return Response(response=json.dumps({'err_msg': 'Unknown city file type'}), status=400, headers=headers)
except Exception as err:
logger.error(err)
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while creating city'}), status=400, headers=headers)