""" Construction catalog SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca """ import json from flask import request, Response from flask_restful import Resource from hub.catalog_factories.data_models.construction.archetype import Archetype from hub.catalog_factories.data_models.construction.construction import Construction from hub.catalog_factories.data_models.construction.material import Material from hub.catalog_factories.data_models.construction.window import Window from hub_api.helpers.session_helper import refresh_session from hub_api.helpers.auth import role_required from hub.persistence.models import UserRoles class ToJson: @staticmethod def archetype_to_json(archetype): constructions = [] for construction in archetype.constructions: constructions.append(ToJson.construction_to_json(construction)) dictionary = { 'name': archetype.name, 'function': archetype.function, 'construction_period': archetype.construction_period, 'average_storey_height': archetype.average_storey_height, 'thermal_capacity': archetype.thermal_capacity, 'extra_loses_due_to_thermal_bridges': archetype.extra_loses_due_to_thermal_bridges, 'indirect_heated_ratio': archetype.indirect_heated_ratio, 'infiltration_rate_for_ventilation_system_off': archetype.infiltration_rate_for_ventilation_system_off, 'infiltration_rate_for_ventilation_system_on': archetype.infiltration_rate_for_ventilation_system_on, 'constructions': constructions } return dictionary @staticmethod def construction_to_json(construction): layers = [] for layer in construction.layers: layers.append(ToJson.layer_to_json(layer)) dictionary = {'name': construction.name, 'type': construction.type, 'layers': layers } if construction.window is not None: dictionary['window_ratio'] = construction.window_ratio dictionary['window'] = ToJson.window_to_json(construction.window) return dictionary @staticmethod def window_to_json(window): if window is None: return {} dictionary = { 'name': window.name, 'frame_ratio': window.frame_ratio, 'g_value': window.g_value, 'overall_u_value': str(window.overall_u_value) } return dictionary @staticmethod def layer_to_json(layer): dictionary = {'name': layer.name, 'thickness': layer.thickness, 'material': ToJson.material_to_json(layer.material), } return dictionary @staticmethod def material_to_json(material): dictionary = {'id': material.id, 'name': material.name, 'solar_absorptance': material.solar_absorptance, 'thermal_absorptance': material.thermal_absorptance, 'visible_absorptance': material.visible_absorptance, 'no_mass': str(material.no_mass), 'thermal_resistance': '', 'conductivity': '', 'density': '', 'specific_heat': '' } if material.no_mass: dictionary['thermal_resistance'] = material.thermal_resistance else: dictionary['conductivity'] = material.conductivity dictionary['density'] = material.density dictionary['specific_heat'] = material.specific_heat return dictionary class ConstructionCatalogEntry(Resource): def __init__(self): pass @role_required([UserRoles.Admin.value, UserRoles.Hub_Reader.value]) def post(self): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers catalog = session.construction_catalog name = None if request.data == b'' or request.json['name'] is None: response = {'error': 'Mandatory parameter "name" is missing'} return Response(json.dumps(response), headers=headers, status=400) try: name = request.json['name'] entry = catalog.get_entry(name) output = {} if isinstance(entry, Archetype): output['archetypes'] = ToJson.archetype_to_json(entry) if isinstance(entry, Construction): output['constructions'] = ToJson.construction_to_json(entry) if isinstance(entry, Material): output['materials'] = ToJson.material_to_json(entry) if isinstance(entry, Window): output['windows'] = ToJson.window_to_json(entry) return Response(json.dumps(output), headers=headers) except IndexError: response = {'error': f'Name "{name}" unknown'} return Response(json.dumps(response), headers=headers, status=400) class ConstructionCatalogEntries(Resource): def __init__(self): pass @role_required([UserRoles.Admin.value, UserRoles.Hub_Reader.value]) def post(self): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers catalog = session.construction_catalog category = None if request.data != b'': category = request.json['category'] output = {} if category is None: output = {'archetypes': [], 'constructions':[], 'materials':[], 'windows':[]} content = catalog.entries() for archetype in content.archetypes: output['archetypes'].append(ToJson.archetype_to_json(archetype)) for construction in content.constructions: output['constructions'].append(ToJson.construction_to_json(construction)) for material in content.materials: output['materials'].append(ToJson.material_to_json(material)) for window in content.windows: output['windows'].append(ToJson.window_to_json(window)) else: try: output[category] = [] content = catalog.entries(category) for entry in content: if isinstance(entry, Archetype): output[category].append(ToJson.archetype_to_json(entry)) if isinstance(entry, Construction): output[category].append(ToJson.construction_to_json(entry)) if isinstance(entry, Material): output[category].append(ToJson.material_to_json(entry)) if isinstance(entry, Window): output[category].append(ToJson.window_to_json(entry)) except ValueError: output = {'error': f'Category "{category}" unknown'} return Response(json.dumps(output), headers=headers, status=400) return Response(json.dumps(output), headers=headers) class ConstructionCatalogNames(Resource): def __init__(self): pass @staticmethod def post(): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers catalog = session.construction_catalog category = None if request.data != b'': category = request.json['category'] if category is None: return Response(json.dumps(catalog.names()), headers=headers) else: try: return Response(json.dumps(catalog.names(category)), headers=headers) except ValueError: response = {'error': f'Category "{category}" unknown'} return Response(json.dumps(response), headers=headers, status=400)