Skip to content

Commit

Permalink
fix: depth interval default methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm committed Mar 6, 2024
1 parent a8b4240 commit 13f7436
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 34 deletions.
2 changes: 1 addition & 1 deletion terraso_backend/apps/graphql/schema/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ type SoilDataNode {
soilDepthSelect: SoilIdSoilDataSoilDepthSelectChoices
landCoverSelect: SoilIdSoilDataLandCoverSelectChoices
grazingSelect: SoilIdSoilDataGrazingSelectChoices
depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices
depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices!
depthIntervals: [SoilDataDepthIntervalNode!]!
depthDependentData: [DepthDependentSoilDataNode!]!
}
Expand Down Expand Up @@ -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!
}
Expand Down
5 changes: 1 addition & 4 deletions terraso_backend/apps/graphql/schema/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from apps.project_management.graphql.projects import ProjectNode
from apps.project_management.models import Project, Site, sites
from apps.soil_id.models import SoilData
from apps.soil_id.models.project_soil_settings import ProjectSoilSettings

from .commons import (
BaseAuthenticatedMutation,
Expand Down Expand Up @@ -223,10 +224,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,
Expand Down
4 changes: 2 additions & 2 deletions terraso_backend/apps/project_management/models/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion terraso_backend/apps/soil_id/graphql/soil_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
26 changes: 10 additions & 16 deletions terraso_backend/apps/soil_id/models/soil_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)

0 comments on commit 13f7436

Please sign in to comment.