""" Greenery SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca Code contributors: Peter Yefi peteryefi@gmail.com """ import json from flask import Response, request from flask_restful import Resource from hub_api.config import Config from city_model_structure.greenery.vegetation import Vegetation from city_model_structure.greenery.soil import Soil from city_model_structure.greenery.plant import Plant import helpers.constants as cte from persistence.models import UserRoles from hub_api.helpers.auth import role_required class Greenery(Resource, Config): def __init__(self): super().__init__() @role_required([UserRoles.Admin.value]) def put(self, city_id): city = self.get_city(city_id) try: greenery_percentage = request.json['greenery_percentage'] if greenery_percentage == 0: response = {'result': 'succeed'} return Response(json.dumps(response), status=200) building_names = request.json['building_names'] vegetation_requested = request.json['vegetation'] vegetation_name = vegetation_requested['vegetation_name'] soil_thickness = vegetation_requested['soil_thickness'] soil_name = vegetation_requested['soil_name'] roughness = vegetation_requested['soil_roughness'] dry_conductivity = vegetation_requested['dry_soil_conductivity'] dry_density = vegetation_requested['dry_soil_density'] dry_specific_heat = vegetation_requested['dry_soil_specific_heat'] thermal_absorptance = vegetation_requested['soil_thermal_absorptance'] solar_absorptance = vegetation_requested['soil_solar_absorptance'] visible_absorptance = vegetation_requested['soil_visible_absorptance'] saturation_volumetric_moisture_content = vegetation_requested['soil_saturation_volumetric_moisture_content'] residual_volumetric_moisture_content = vegetation_requested['soil_residual_volumetric_moisture_content'] soil = Soil(soil_name, roughness, dry_conductivity, dry_density, dry_specific_heat, thermal_absorptance, solar_absorptance, visible_absorptance, saturation_volumetric_moisture_content, residual_volumetric_moisture_content) soil.initial_volumetric_moisture_content = '0.1' plant_percentages = vegetation_requested['plant_percentages'] plants = [] for plant_percentage in plant_percentages: plant_name = plant_percentage['plant_name'] height = plant_percentage['plant_height'] leaf_area_index = plant_percentage['plant_leaf_area_index'] leaf_reflectivity = plant_percentage['plant_leaf_reflectivity'] leaf_emissivity = plant_percentage['plant_leaf_emissivity'] minimal_stomatal_resistance = plant_percentage['plant_minimal_stomatal_resistance'] co2_sequestration = plant_percentage['plant_co2_sequestration'] grows_on_soils = plant_percentage['plant_grows_on'] plant = Plant(plant_name, height, leaf_area_index, leaf_reflectivity, leaf_emissivity, minimal_stomatal_resistance, co2_sequestration, grows_on_soils) plant.percentage = plant_percentage['plant_percentage'] plants.append(plant) vegetation = Vegetation(vegetation_name, soil, soil_thickness, plants) for building_name in building_names: for building in city.buildings: if building.human_readable_name != building_name: continue for internal_zone in building.internal_zones: for thermal_zone in internal_zone.thermal_zones: for thermal_boundary in thermal_zone.thermal_boundaries: if thermal_boundary.type == cte.ROOF: thermal_boundary.vegetation = vegetation response = {'result': 'succeed'} except KeyError as ex: response = {'error': f'Mandatory parameter {ex} is missing'} return Response(json.dumps(response), status=400) return Response(json.dumps(response))