Skip to content

Commit

Permalink
feat: implement remaining functionality and do basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm committed Oct 30, 2024
1 parent b5fe246 commit a60d193
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 51 deletions.
9 changes: 5 additions & 4 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ type SoilDataUpdateMutationPayload {
}

input SoilDataUpdateMutationInput {
siteId: ID!
depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices
downSlope: SoilIdSoilDataDownSlopeChoices
crossSlope: SoilIdSoilDataCrossSlopeChoices
bedrock: Int
Expand All @@ -2239,7 +2239,7 @@ input SoilDataUpdateMutationInput {
soilDepthSelect: SoilIdSoilDataSoilDepthSelectChoices
landCoverSelect: SoilIdSoilDataLandCoverSelectChoices
grazingSelect: SoilIdSoilDataGrazingSelectChoices
depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices
siteId: ID!
clientMutationId: String
}

Expand All @@ -2250,7 +2250,6 @@ type DepthDependentSoilDataUpdateMutationPayload {
}

input DepthDependentSoilDataUpdateMutationInput {
siteId: ID!
depthInterval: DepthIntervalInput!
texture: SoilIdDepthDependentSoilDataTextureChoices
clayPercent: Int
Expand All @@ -2274,6 +2273,7 @@ input DepthDependentSoilDataUpdateMutationInput {
soilOrganicMatterTesting: SoilIdDepthDependentSoilDataSoilOrganicMatterTestingChoices
sodiumAbsorptionRatio: Decimal
carbonates: SoilIdDepthDependentSoilDataCarbonatesChoices
siteId: ID!
clientMutationId: String
}

Expand Down Expand Up @@ -2315,6 +2315,7 @@ input SoilDataPushInputEntry {
}

input SoilDataPushInputSoilData {
depthIntervalPreset: SoilIdSoilDataDepthIntervalPresetChoices
downSlope: SoilIdSoilDataDownSlopeChoices
crossSlope: SoilIdSoilDataCrossSlopeChoices
bedrock: Int
Expand Down Expand Up @@ -2383,7 +2384,6 @@ type SoilDataUpdateDepthIntervalMutationPayload {
}

input SoilDataUpdateDepthIntervalMutationInput {
siteId: ID!
label: String
depthInterval: DepthIntervalInput!
soilTextureEnabled: Boolean
Expand All @@ -2394,6 +2394,7 @@ input SoilDataUpdateDepthIntervalMutationInput {
electricalConductivityEnabled: Boolean
sodiumAdsorptionRatioEnabled: Boolean
soilStructureEnabled: Boolean
siteId: ID!
applyToIntervals: [DepthIntervalInput!] = null
clientMutationId: String
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class SoilDataUpdateMutation(BaseWriteMutation):

class Input(SoilDataInputs):
site_id = graphene.ID(required=True)
depth_interval_preset = SoilDataNode.depth_interval_preset_enum()

@classmethod
def mutate_and_get_payload(cls, root, info, site_id, **kwargs):
Expand Down
83 changes: 60 additions & 23 deletions terraso_backend/apps/soil_id/graphql/soil_data/push_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ class Input:
)

@staticmethod
def record_update(user: User, soil_data_entries: list[dict]) -> list[SoilDataHistory]:
def record_soil_data_push(user: User, soil_data_entries: list[dict]) -> list[SoilDataHistory]:
history_entries = []

for entry in soil_data_entries:
changes = copy.deepcopy(entry)
site_id = changes.pop("site_id")
site = Site.objects.filter(id=site_id).first()
changes = copy.deepcopy(entry["soil_data"])
site = Site.objects.filter(id=entry["site_id"]).first()

history_entry = SoilDataHistory(site=site, changed_by=user, soil_data_changes=changes)
history_entry.save()
Expand All @@ -97,10 +96,10 @@ def record_update(user: User, soil_data_entries: list[dict]) -> list[SoilDataHis
return history_entries

@staticmethod
def record_update_failure(
def record_entry_failure(
history_entry: SoilDataHistory, reason: SoilDataPushFailureReason, site_id: str
):
history_entry.update_failure_reason = reason
history_entry.update_failure_reason = reason.value
history_entry.save()
return SoilDataPushEntry(site_id=site_id, result=SoilDataPushEntryFailure(reason=reason))

Expand All @@ -114,11 +113,59 @@ def get_valid_site_for_soil_update(user: User, site_id: str):
if not check_site_permission(user, SiteAction.ENTER_DATA, Context(site=site)):
return None, SoilDataPushFailureReason.NOT_ALLOWED

if not check_site_permission(user, SiteAction.UPDATE_DEPTH_INTERVAL, Context(site=site)):
return None, SoilDataPushFailureReason.NOT_ALLOWED

if not hasattr(site, "soil_data"):
site.soil_data = SoilData()

return site, None

@staticmethod
def save_soil_data(site: Site, soil_data: dict):
if (
"depth_interval_preset" in soil_data
and soil_data["depth_interval_preset"] != site.soil_data.depth_interval_preset
):
site.soil_data.depth_intervals.all().delete()

BaseWriteMutation.assign_graphql_fields_to_model_instance(
model_instance=site.soil_data, fields=soil_data
)

@staticmethod
def save_depth_dependent_data(site: Site, depth_dependent_data: list[dict]):
for depth_dependent_entry in depth_dependent_data:
interval = depth_dependent_entry.pop("depth_interval")
depth_interval, _ = site.soil_data.depth_dependent_data.get_or_create(
depth_interval_start=interval["start"],
depth_interval_end=interval["end"],
)

BaseWriteMutation.assign_graphql_fields_to_model_instance(
model_instance=depth_interval, fields=depth_dependent_entry
)

@staticmethod
def update_depth_intervals(site: Site, depth_intervals: list[dict]):
for depth_interval_input in depth_intervals:
interval_input = depth_interval_input.pop("depth_interval")
depth_interval, _ = site.soil_data.depth_intervals.get_or_create(
depth_interval_start=interval_input["start"],
depth_interval_end=interval_input["end"],
)

BaseWriteMutation.assign_graphql_fields_to_model_instance(
model_instance=depth_interval, fields=depth_interval_input
)

@staticmethod
def delete_depth_intervals(site: Site, deleted_depth_intervals: list[dict]):
for interval in deleted_depth_intervals:
site.soil_data.depth_intervals.filter(
depth_interval_start=interval["start"], depth_interval_end=interval["end"]
).delete()

@staticmethod
def get_entry_result(user: User, soil_data_entry: dict, history_entry: SoilDataHistory):
site_id = soil_data_entry["site_id"]
Expand All @@ -131,33 +178,23 @@ def get_entry_result(user: User, soil_data_entry: dict, history_entry: SoilDataH
try:
site, reason = SoilDataPush.get_valid_site_for_soil_update(user=user, site_id=site_id)
if site is None:
return SoilDataPush.record_update_failure(
return SoilDataPush.record_entry_failure(
history_entry=history_entry,
site_id=site_id,
reason=reason,
)

BaseWriteMutation.assign_graphql_fields_to_model_instance(
model_instance=site.soil_data, fields=soil_data
)

for depth_dependent_entry in depth_dependent_data:
interval = depth_dependent_entry.pop("depth_interval")
depth_interval, _ = site.soil_data.depth_dependent_data.get_or_create(
depth_interval_start=interval["start"],
depth_interval_end=interval["end"],
)

BaseWriteMutation.assign_graphql_fields_to_model_instance(
model_instance=depth_interval, fields=depth_dependent_entry
)
SoilDataPush.save_soil_data(site, soil_data)
SoilDataPush.update_depth_intervals(site, depth_intervals)
SoilDataPush.save_depth_dependent_data(site, depth_dependent_data)
SoilDataPush.delete_depth_intervals(site, deleted_depth_intervals)

history_entry.update_succeeded = True
history_entry.save()
return SoilDataPushEntry(site_id=site_id, result=SoilDataPushEntrySuccess(site=site))

except (ValidationError, IntegrityError):
return SoilDataPush.record_update_failure(
return SoilDataPush.record_entry_failure(
history_entry=history_entry,
site_id=site_id,
reason=SoilDataPushFailureReason.INVALID_DATA,
Expand All @@ -169,7 +206,7 @@ def mutate_and_get_payload(cls, root, info, soil_data_entries: list[dict]):
user = info.context.user

with transaction.atomic():
history_entries = SoilDataPush.record_update(user, soil_data_entries)
history_entries = SoilDataPush.record_soil_data_push(user, soil_data_entries)

with transaction.atomic():
for entry, history_entry in zip(soil_data_entries, history_entries):
Expand Down
1 change: 1 addition & 0 deletions terraso_backend/apps/soil_id/graphql/soil_data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SoilDataDepthIntervalInputs:


class SoilDataInputs:
depth_interval_preset = SoilDataNode.depth_interval_preset_enum()
down_slope = SoilDataNode.down_slope_enum()
cross_slope = SoilDataNode.cross_slope_enum()
bedrock = graphene.Int()
Expand Down
Loading

0 comments on commit a60d193

Please sign in to comment.