diff --git a/terraso_backend/apps/graphql/schema/commons.py b/terraso_backend/apps/graphql/schema/commons.py index 78f963471..c3a9a5988 100644 --- a/terraso_backend/apps/graphql/schema/commons.py +++ b/terraso_backend/apps/graphql/schema/commons.py @@ -105,7 +105,7 @@ def not_found(cls, model=None, field=None, msg=None): raise GraphQLNotFoundException(msg, field=field, model_name=model.__name__) @classmethod - def get_or_throw(cls, model, field_name, id_): + def get_or_throw[M](cls, model: type[M], field_name, id_) -> M: try: return model.objects.get(id=id_) except model.DoesNotExist: diff --git a/terraso_backend/apps/graphql/schema/schema.graphql b/terraso_backend/apps/graphql/schema/schema.graphql index def8172fe..c4246630a 100644 --- a/terraso_backend/apps/graphql/schema/schema.graphql +++ b/terraso_backend/apps/graphql/schema/schema.graphql @@ -814,7 +814,7 @@ type SoilDataNode { soilDepthSelect: SoilIdSoilDataSoilDepthSelectChoices landCoverSelect: SoilIdSoilDataLandCoverSelectChoices grazingSelect: SoilIdSoilDataGrazingSelectChoices - depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices + depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices! depthIntervals: [SoilDataDepthIntervalNode!]! depthDependentData: [DepthDependentSoilDataNode!]! } @@ -1106,14 +1106,14 @@ enum SoilIdSoilDataDepthIntervalPresetChoices { type SoilDataDepthIntervalNode { label: String! - soilTextureEnabled: Boolean! - soilColorEnabled: Boolean! - carbonatesEnabled: Boolean! - phEnabled: Boolean! - soilOrganicCarbonMatterEnabled: Boolean! - electricalConductivityEnabled: Boolean! - sodiumAdsorptionRatioEnabled: Boolean! - soilStructureEnabled: Boolean! + soilTextureEnabled: Boolean + soilColorEnabled: Boolean + soilStructureEnabled: Boolean + carbonatesEnabled: Boolean + phEnabled: Boolean + soilOrganicCarbonMatterEnabled: Boolean + electricalConductivityEnabled: Boolean + sodiumAdsorptionRatioEnabled: Boolean site: SiteNode! depthInterval: DepthInterval! } diff --git a/terraso_backend/apps/graphql/schema/sites.py b/terraso_backend/apps/graphql/schema/sites.py index 91acbd072..a7f31665d 100644 --- a/terraso_backend/apps/graphql/schema/sites.py +++ b/terraso_backend/apps/graphql/schema/sites.py @@ -223,10 +223,6 @@ def mutate_and_get_payload(cls, root, info, **kwargs): if hasattr(project, "soil_settings") and hasattr(site, "soil_data"): if project_id is not None: metadata["project_id"] = str(project.id) - else: - if hasattr(site, "soil_data"): - # Delete existing intervals if removed from project - site.soil_data.remove_from_project() log.log( user=user, diff --git a/terraso_backend/apps/project_management/models/sites.py b/terraso_backend/apps/project_management/models/sites.py index f40f6aa16..5bf455ef3 100644 --- a/terraso_backend/apps/project_management/models/sites.py +++ b/terraso_backend/apps/project_management/models/sites.py @@ -12,7 +12,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see https://www.gnu.org/licenses/. -from typing import Self, Union +from typing import List, Self, Union from django.db import models from django.db.models import Q @@ -106,7 +106,7 @@ def mark_seen_by(self, user: User): self.seen_by.add(user) @classmethod - def bulk_change_project(cls, sites: [Self], project: Project): + def bulk_change_project(cls, sites: List[Self], project: Project): for site in sites: site.owner = None site.project = project diff --git a/terraso_backend/apps/soil_id/graphql/soil_data.py b/terraso_backend/apps/soil_id/graphql/soil_data.py index 3d04108f1..bc9a72ba7 100644 --- a/terraso_backend/apps/soil_id/graphql/soil_data.py +++ b/terraso_backend/apps/soil_id/graphql/soil_data.py @@ -115,7 +115,7 @@ def grazing_enum(cls): @classmethod def depth_interval_preset_enum(cls): - return cls._meta.fields["depth_interval_preset"].type() + return cls._meta.fields["depth_interval_preset"].type.of_type() class ProjectSoilSettingsNode(DjangoObjectType): diff --git a/terraso_backend/apps/soil_id/migrations/0011_alter_soildata_depth_interval_preset_and_more.py b/terraso_backend/apps/soil_id/migrations/0011_alter_soildata_depth_interval_preset_and_more.py new file mode 100644 index 000000000..ba8ba65f6 --- /dev/null +++ b/terraso_backend/apps/soil_id/migrations/0011_alter_soildata_depth_interval_preset_and_more.py @@ -0,0 +1,77 @@ +# Copyright © 2024 Technology Matters +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see https://www.gnu.org/licenses/. + + +# Generated by Django 5.0.2 on 2024-03-05 23:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("soil_id", "0010_depthdependentsoildata_color_photo_lighting_condition_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="soildata", + name="depth_interval_preset", + field=models.CharField( + choices=[("LANDPKS", "Landpks"), ("NRCS", "Nrcs"), ("CUSTOM", "Custom")], + default="LANDPKS", + ), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="carbonates_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="electrical_conductivity_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="ph_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="sodium_adsorption_ratio_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="soil_color_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="soil_organic_carbon_matter_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="soil_structure_enabled", + field=models.BooleanField(blank=True, null=True), + ), + migrations.AlterField( + model_name="soildatadepthinterval", + name="soil_texture_enabled", + field=models.BooleanField(blank=True, null=True), + ), + ] diff --git a/terraso_backend/apps/soil_id/models/project_soil_settings.py b/terraso_backend/apps/soil_id/models/project_soil_settings.py index e961a14b3..2bafbe607 100644 --- a/terraso_backend/apps/soil_id/models/project_soil_settings.py +++ b/terraso_backend/apps/soil_id/models/project_soil_settings.py @@ -63,7 +63,6 @@ class MeasurementUnit(models.TextChoices): measurement_units = models.CharField(blank=True, null=True, choices=MeasurementUnit.choices) depth_interval_preset = models.CharField( - null=False, default=DepthIntervalPreset.LANDPKS.value, choices=DepthIntervalPreset.choices, ) diff --git a/terraso_backend/apps/soil_id/models/soil_data.py b/terraso_backend/apps/soil_id/models/soil_data.py index 421aff945..bfe884b33 100644 --- a/terraso_backend/apps/soil_id/models/soil_data.py +++ b/terraso_backend/apps/soil_id/models/soil_data.py @@ -211,16 +211,10 @@ class SoilDataDepthIntervalPreset(models.TextChoices): CUSTOM = "CUSTOM" depth_interval_preset = models.CharField( - choices=SoilDataDepthIntervalPreset.choices, blank=True, null=True + choices=SoilDataDepthIntervalPreset.choices, + default=SoilDataDepthIntervalPreset.LANDPKS.value, ) - def remove_from_project(self): - SoilDataDepthInterval.objects.filter(soil_data=self).delete() - # Default is to set the site interval to custom for now - # TODO: At some point, user will be able to set a default preset - self.depth_interval_preset = self.SoilDataDepthIntervalPreset.CUSTOM - self.save() - class SoilDataDepthInterval(BaseModel, BaseDepthInterval): soil_data = models.ForeignKey( @@ -240,11 +234,11 @@ def clean(self): super().clean() BaseDepthInterval.validate_intervals(list(self.soil_data.depth_intervals.all())) - soil_texture_enabled = models.BooleanField(blank=True, default=False) - soil_color_enabled = models.BooleanField(blank=True, default=False) - carbonates_enabled = models.BooleanField(blank=True, default=False) - ph_enabled = models.BooleanField(blank=True, default=False) - soil_organic_carbon_matter_enabled = models.BooleanField(blank=True, default=False) - electrical_conductivity_enabled = models.BooleanField(blank=True, default=False) - sodium_adsorption_ratio_enabled = models.BooleanField(blank=True, default=False) - soil_structure_enabled = models.BooleanField(blank=True, default=False) + soil_texture_enabled = models.BooleanField(blank=True, null=True) + soil_color_enabled = models.BooleanField(blank=True, null=True) + soil_structure_enabled = models.BooleanField(blank=True, null=True) + carbonates_enabled = models.BooleanField(blank=True, null=True) + ph_enabled = models.BooleanField(blank=True, null=True) + soil_organic_carbon_matter_enabled = models.BooleanField(blank=True, null=True) + electrical_conductivity_enabled = models.BooleanField(blank=True, null=True) + sodium_adsorption_ratio_enabled = models.BooleanField(blank=True, null=True)