181 lines
7.0 KiB
Python
181 lines
7.0 KiB
Python
"""
|
|
Greenery 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 catalog_factories.data_models.greenery.plant import Plant
|
|
from catalog_factories.data_models.greenery.soil import Soil
|
|
from catalog_factories.data_models.greenery.vegetation import Vegetation
|
|
from hub_api.helpers.session_helper import refresh_session
|
|
|
|
|
|
class ToJson:
|
|
@staticmethod
|
|
def plant_to_json(plant, percentage=None):
|
|
dictionary = {
|
|
'plant_percentage': percentage,
|
|
'plant_name': plant.name,
|
|
'plant_category': plant.category,
|
|
'plant_height': plant.height,
|
|
'plant_leaf_area_index': plant.leaf_area_index,
|
|
'plant_leaf_reflectivity': plant.leaf_reflectivity,
|
|
'plant_leaf_emissivity': plant.leaf_emissivity,
|
|
'plant_minimal_stomatal_resistance': plant.minimal_stomatal_resistance,
|
|
'plant_co2_sequestration': plant.co2_sequestration,
|
|
'plant_grows_on': []
|
|
}
|
|
if percentage is None:
|
|
dictionary.pop('plant_percentage')
|
|
soils = []
|
|
for soil in plant.grows_on:
|
|
soil_dic = ToJson.soil_to_json(soil)
|
|
soils.append(soil_dic)
|
|
dictionary['plant_grows_on'] = soils
|
|
return dictionary
|
|
|
|
@staticmethod
|
|
def soil_to_json(soil):
|
|
dictionary = {'soil_name': soil.name,
|
|
'soil_roughness': str(soil.roughness),
|
|
'soil_dry_conductivity': soil.dry_conductivity,
|
|
'soil_dry_density': soil.dry_density,
|
|
'soil_dry_specific_heat': soil.dry_specific_heat,
|
|
'soil_thermal_absortance': soil.thermal_absorptance,
|
|
'soil_solar_absortance': soil.solar_absorptance,
|
|
'soil_visible_absortance': soil.visible_absorptance,
|
|
'soil_saturation_volumetric_moisture_content': soil.saturation_volumetric_moisture_content,
|
|
'soil_residual_volumetric_moisture_content': soil.residual_volumetric_moisture_content,
|
|
'soil_initial_volumetric_moisture_content': soil.initial_volumetric_moisture_content
|
|
}
|
|
return dictionary
|
|
|
|
@staticmethod
|
|
def vegetation_to_json(vegetation):
|
|
|
|
dictionary = {'vegetation_name': vegetation.name,
|
|
'vegetation_category': vegetation.category,
|
|
'soil_thickness': vegetation.soil_thickness,
|
|
'management': str(vegetation.management),
|
|
'air_gap': vegetation.air_gap,
|
|
'soil_name': vegetation.soil_name,
|
|
'soil_roughness': str(vegetation.soil_roughness),
|
|
'dry_soil_conductivity': vegetation.dry_soil_conductivity,
|
|
'dry_soil_density': vegetation.dry_soil_density,
|
|
'dry_soil_specific_heat': vegetation.dry_soil_specific_heat,
|
|
'soil_thermal_absorptance': vegetation.soil_thermal_absorptance,
|
|
'soil_solar_absorptance': vegetation.soil_solar_absorptance,
|
|
'soil_visible_absorptance': vegetation.soil_visible_absorptance,
|
|
'soil_saturation_volumetric_moisture_content': vegetation.soil_saturation_volumetric_moisture_content,
|
|
'soil_residual_volumetric_moisture_content': vegetation.soil_residual_volumetric_moisture_content,
|
|
'soil_initial_volumetric_moisture_content': vegetation.soil_initial_volumetric_moisture_content,
|
|
'plant_percentages': []
|
|
}
|
|
percentages = []
|
|
for percentage in vegetation.plant_percentages:
|
|
percentage_dic = ToJson.plant_to_json(percentage, percentage.percentage)
|
|
percentages.append(percentage_dic)
|
|
dictionary['plant_percentages'] = percentages
|
|
return dictionary
|
|
|
|
|
|
class GreeneryCatalogEntry(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.greenery_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, Vegetation):
|
|
output['vegetations'] = ToJson.vegetation_to_json(entry)
|
|
if isinstance(entry, Plant):
|
|
output['plants'] = ToJson.plant_to_json(entry)
|
|
if isinstance(entry, Soil):
|
|
output['soils'] = ToJson.soil_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 GreeneryCatalogEntries(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.greenery_catalog
|
|
category = None
|
|
if request.data != b'':
|
|
category = request.json['category']
|
|
output = {}
|
|
if category is None:
|
|
content = catalog.entries()
|
|
output = {'vegetations': [], 'plants': [], 'soils': []}
|
|
for vegetation in content.vegetations:
|
|
output['vegetations'].append(ToJson.vegetation_to_json(vegetation))
|
|
for plant in content.plants:
|
|
output['plants'].append(ToJson.plant_to_json(plant))
|
|
for soil in content.soils:
|
|
output['soils'].append(ToJson.soil_to_json(soil))
|
|
else:
|
|
try:
|
|
content = catalog.entries(category)
|
|
output[category] = []
|
|
for entry in content:
|
|
if isinstance(entry, Vegetation):
|
|
output[category].append(ToJson.vegetation_to_json(entry))
|
|
if isinstance(entry, Plant):
|
|
output[category].append(ToJson.plant_to_json(entry))
|
|
if isinstance(entry, Soil):
|
|
output[category].append(ToJson.soil_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 GreeneryCatalogNames(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.greenery_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)
|