From edd2054763f3e85959c1913eadb134b18c892f26 Mon Sep 17 00:00:00 2001 From: Dana Lambert Date: Mon, 8 Nov 2021 15:51:05 +1300 Subject: [PATCH] Create plant display fields on the backend --- backend/right_tree/api/models.py | 27 +++++++++++++++++++ backend/right_tree/api/serializers.py | 7 +++++ .../components/steps/results/ResultsStep.js | 23 +++++----------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/backend/right_tree/api/models.py b/backend/right_tree/api/models.py index 25cf16a..370383e 100644 --- a/backend/right_tree/api/models.py +++ b/backend/right_tree/api/models.py @@ -123,5 +123,32 @@ class Plant(models.Model): soil_variants = models.ManyToManyField(SoilVariant) zones = models.ManyToManyField(Zone) + @property + def display_name(self): + common_name_str = f' / {str(self.commonname)}' if self.commonname is not None else '' + synonym_str = f' / {str(self.synonym)}' if self.synonym is not None else '' + return f"{self.name}{common_name_str}{synonym_str}" + + @property + def display_growth_form(self): + growth_form_str = f'{str(self.growth_form)} / ' if self.growth_form is not None else '' + return f"{growth_form_str}{self.maxheight} / {self.spacing}" + + @property + def moisture_preferences(self): + return ', '.join([variant.name for variant in self.soil_variants.all()]) + + @property + def plant_tolerances(self): + return f"{self.water_tolerance.level} / {self.drought_tolerance.level} / {self.frost_tolerance.level} / {self.salinity_tolerance.level}" + + @property + def ecosystem_services(self): + return f"{str(self.purpose) or ''}" + + @property + def carbon_sequestration(self): + return "" + def __str__(self): return self.name diff --git a/backend/right_tree/api/serializers.py b/backend/right_tree/api/serializers.py index 88e0fdd..5d6eda8 100644 --- a/backend/right_tree/api/serializers.py +++ b/backend/right_tree/api/serializers.py @@ -104,6 +104,13 @@ class PlantSerializer(serializers.HyperlinkedModelSerializer): soil_variants = SoilVariantSerializer(many=True, read_only=True) zones = ZoneSerializer(many=True, read_only=True) + display_name = serializers.CharField(max_length=300) + display_growth_form = serializers.CharField(max_length=300) + moisture_preferences = serializers.CharField(max_length=50) + plant_tolerances = serializers.CharField(max_length=50) + ecosystem_services = serializers.CharField(max_length=200) + carbon_sequestration = serializers.CharField(max_length=50) + class Meta: model = Plant fields = '__all__' diff --git a/frontend/src/components/steps/results/ResultsStep.js b/frontend/src/components/steps/results/ResultsStep.js index e4912f0..cc988a9 100644 --- a/frontend/src/components/steps/results/ResultsStep.js +++ b/frontend/src/components/steps/results/ResultsStep.js @@ -17,23 +17,14 @@ export default function ResultsStep(props) { const getTableRows = () => { return props.plants.map((plant) => { - - const name = `${plant.name}/${plant.commonname}/${plant.synonym}`; - const growthForm = `${plant.growth_form}/${plant.maxheight}/${plant.spacing}`; - const moisturePreferences = `${plant.soil_variants.map((variant) => variant.name).join(', ')}`; - const plantTolerances = `${plant.water_tolerance.level}/${plant.drought_tolerance.level}/${plant.frost_tolerance.level}/${plant.salinity_tolerance.level}`;; - const ecosystemServices = `${plant.purpose}`; - const carbonSequestration = `` - const plantingStage = `${plant.stage}` - return createData( - name, - growthForm, - moisturePreferences, - plantTolerances, - ecosystemServices, - carbonSequestration, - plantingStage + plant.display_name, + plant.display_growth_form, + plant.moisture_preferences, + plant.plant_tolerances, + plant.ecosystem_services, + plant.carbon_sequestration, + plant.stage ); }) }