Create plant display fields on the backend

This commit is contained in:
Dana Lambert 2021-11-08 15:51:05 +13:00
parent 73e7fe88f8
commit edd2054763
3 changed files with 41 additions and 16 deletions

View file

@ -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

View file

@ -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__'

View file

@ -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
);
})
}