api_v1.4/hub_api/city_info.py
2023-01-13 12:01:13 -05:00

101 lines
3.8 KiB
Python

"""
City info
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
"""
import json
from flask_apispec import use_kwargs, doc
from flask import Response, request
from flask_restful import Resource
from hub_api.helpers.session_helper import refresh_session
from marshmallow import fields, Schema
from hub_api.helpers.auth import role_required
from persistence.models import UserRoles
from flask_apispec.views import MethodResource
from hub_logger import logger
class AuthorizationHeader(Schema):
Authorization = fields.Str(required=True, description='Authorization token')
AppID = fields.Str(required=True, description='ID of app accessing API')
class CitySchema(Schema):
city_file = fields.Raw(type='file', required=True, description='City file')
class CityInfo(Resource):
def __init__(self):
pass
@staticmethod
def get():
session = refresh_session(request)
if session is None:
return Response(json.dumps({'error': 'invalid session'}), status=401)
headers = session.headers
city = session.city
# TODO: this is only for dompark project and need to be removed in future versions.
floor_area = 0
wall_construction = 'unknown'
floor_construction = 'unknown'
roof_construction = 'unknown'
window_type = 'unknown'
building_dic = {}
for building in city.buildings:
usages = [] # This is only valid for dompark project as all the building have the same usage
if building.lower_corner[2] == 0:
floor_area += building.floor_area
for internal_zone in building.internal_zones:
for usage_zone in internal_zone.usage_zones:
usages.append({'percentage': usage_zone.percentage, 'usage': usage_zone.usage})
for thermal_zone in internal_zone.thermal_zones:
for thermal_boundary in thermal_zone.thermal_boundaries:
if thermal_boundary.parent_surface.type == 'Ground':
floor_construction = thermal_boundary.construction_name
elif thermal_boundary.parent_surface.type == 'Wall':
wall_construction = thermal_boundary.construction_name
for thermal_opening in thermal_boundary.thermal_openings:
if thermal_opening.construction_name is not None:
window_type = thermal_opening.construction_name
break
else:
roof_construction = thermal_boundary.construction_name
name = building.human_readable_name
year_of_construction = str(building.year_of_construction)
building_dic = {
'name': str(name),
'floor_area': str(floor_area),
'year_of_construction': str(year_of_construction),
'usages': usages,
'wall_construction': wall_construction,
'floor_construction': floor_construction,
'roof_construction': roof_construction,
'window_type': window_type,
'default_archetype': 'industry ASHRAE_2004:4A non_standard_dompark'
}
buildings = [building_dic]
response = {'city_name': 'Montreal',
'climate_reference_city': str(city.climate_reference_city),
'buildings': buildings
}
return Response(json.dumps(response), headers=headers)
class City(MethodResource, Resource):
@doc(description='Persist a city', tags=['PersistCity'])
@role_required(UserRoles.Admin.value)
@use_kwargs(AuthorizationHeader, location='headers')
@use_kwargs(CitySchema)
def post(self, **kwargs):
try:
print(kwargs)
print(request.files)
return Response(response=json.dumps({'msg': 'Hello'}), status=201)
except Exception as err:
logger.error(err)
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while creating user'}), status=400)