Skip to content

Commit

Permalink
feat: history tracking, some API shape tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm committed Oct 2, 2024
1 parent 9bf5a17 commit a6b2510
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 80 deletions.
29 changes: 25 additions & 4 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2352,16 +2352,16 @@ type SoilDataBulkUpdateResultEntry {
union SoilDataBulkUpdateResult = SoilDataBulkUpdateSuccess | SoilDataBulkUpdateFailure

type SoilDataBulkUpdateSuccess {
soilData: SoilDataNode!
site: SiteNode!
}

type SoilDataBulkUpdateFailure {
reason: SoilDataBulkUpdateFailureReason!
}

enum SoilDataBulkUpdateFailureReason {
SITE_DELETED
LOST_PERMISSION
DOES_NOT_EXIST
NOT_ALLOWED
}

input SoilDataBulkUpdateInput {
Expand All @@ -2388,7 +2388,28 @@ input SoilDataBulkUpdateEntry {
landCoverSelect: SoilIdSoilDataLandCoverSelectChoices
grazingSelect: SoilIdSoilDataGrazingSelectChoices
siteId: ID!
depthIntervals: [SoilDataBulkUpdateDepthDependentEntry!]!
soilData: SoilDataBulkUpdateSoilData!
depthDependentData: [SoilDataBulkUpdateDepthDependentEntry!]!
}

input SoilDataBulkUpdateSoilData {
downSlope: SoilIdSoilDataDownSlopeChoices
crossSlope: SoilIdSoilDataCrossSlopeChoices
bedrock: Int
slopeLandscapePosition: SoilIdSoilDataSlopeLandscapePositionChoices
slopeAspect: Int
slopeSteepnessSelect: SoilIdSoilDataSlopeSteepnessSelectChoices
slopeSteepnessPercent: Int
slopeSteepnessDegree: Int
surfaceCracksSelect: SoilIdSoilDataSurfaceCracksSelectChoices
surfaceSaltSelect: SoilIdSoilDataSurfaceSaltSelectChoices
floodingSelect: SoilIdSoilDataFloodingSelectChoices
limeRequirementsSelect: SoilIdSoilDataLimeRequirementsSelectChoices
surfaceStoninessSelect: SoilIdSoilDataSurfaceStoninessSelectChoices
waterTableDepthSelect: SoilIdSoilDataWaterTableDepthSelectChoices
soilDepthSelect: SoilIdSoilDataSoilDepthSelectChoices
landCoverSelect: SoilIdSoilDataLandCoverSelectChoices
grazingSelect: SoilIdSoilDataGrazingSelectChoices
}

input SoilDataBulkUpdateDepthDependentEntry {
Expand Down
132 changes: 68 additions & 64 deletions terraso_backend/apps/soil_id/graphql/soil_id/soil_data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
from django.db import transaction

from apps.graphql.schema.commons import BaseWriteMutation
from apps.graphql.schema.sites import SiteNode
from apps.project_management.models.sites import Site
from apps.project_management.permission_rules import Context
from apps.project_management.permission_table import SiteAction, check_site_permission
from apps.soil_id.graphql.soil_data import (
SoilDataDepthDependentInputs,
SoilDataInputs,
SoilDataNode,
)
from apps.soil_id.graphql.soil_data import SoilDataDepthDependentInputs, SoilDataInputs
from apps.soil_id.models.soil_data import SoilData
from apps.soil_id.models.soil_data_history import SoilDataHistory

logger = structlog.get_logger(__name__)


class SoilDataBulkUpdateSuccess(graphene.ObjectType):
soil_data = graphene.Field(SoilDataNode, required=True)
site = graphene.Field(SiteNode, required=True)


# TODO: just a generic "can't access" result?
Expand All @@ -47,9 +45,14 @@ class SoilDataBulkUpdateDepthDependentEntry(SoilDataDepthDependentInputs, graphe
pass


class SoilDataBulkUpdateSoilData(SoilDataInputs, graphene.InputObjectType):
pass


class SoilDataBulkUpdateEntry(SoilDataInputs, graphene.InputObjectType):
site_id = graphene.ID(required=True)
depth_intervals = graphene.Field(
soil_data = graphene.Field(graphene.NonNull(SoilDataBulkUpdateSoilData))
depth_dependent_data = graphene.Field(
graphene.List(graphene.NonNull(SoilDataBulkUpdateDepthDependentEntry)), required=True
)

Expand All @@ -66,72 +69,73 @@ class Input:

@classmethod
def mutate_and_get_payload(cls, root, info, soil_data):
try:
results = []

with transaction.atomic():
for entry in soil_data:
site_id = entry.pop("site_id")
depth_intervals = entry.pop("depth_intervals")

site = Site.objects.filter(id=site_id).first()

if site is None:
results.append(
SoilDataBulkUpdateResultEntry(
site_id=site_id,
result=SoilDataBulkUpdateFailure(
reason=SoilDataBulkUpdateFailureReason.DOES_NOT_EXIST
),
)
)
continue

user = info.context.user
if not check_site_permission(user, SiteAction.ENTER_DATA, Context(site=site)):
results.append(
SoilDataBulkUpdateResultEntry(
site_id=entry["site_id"],
result=SoilDataBulkUpdateFailure(
reason=SoilDataBulkUpdateFailureReason.NOT_ALLOWED
),
)
)
continue
# TODO: refactor spaghetti mutation logic re: history, split into smaller functions
results = []

if not hasattr(site, "soil_data"):
site.soil_data = SoilData()
with transaction.atomic():
for entry in soil_data:
site_id = entry.pop("site_id")
depth_intervals = entry["depth_dependent_data"]

for attr, value in entry.items():
if isinstance(value, enum.Enum):
value = value.value
setattr(site.soil_data, attr, value)
site = Site.objects.filter(id=site_id).first()

site.soil_data.save()

for depth_interval_input in depth_intervals:
interval = depth_interval_input.pop("depth_interval")
depth_interval, _ = site.soil_data.depth_dependent_data.get_or_create(
depth_interval_start=interval["start"],
depth_interval_end=interval["end"],
if site is None:
results.append(
SoilDataBulkUpdateResultEntry(
site_id=site_id,
result=SoilDataBulkUpdateFailure(
reason=SoilDataBulkUpdateFailureReason.DOES_NOT_EXIST
),
)
)
continue

for attr, value in depth_interval_input.items():
if isinstance(value, enum.Enum):
value = value.value
setattr(depth_interval, attr, value)

depth_interval.save()

user = info.context.user
if not check_site_permission(user, SiteAction.ENTER_DATA, Context(site=site)):
results.append(
SoilDataBulkUpdateResultEntry(
site_id=site_id,
result=SoilDataBulkUpdateSuccess(soil_data=site.soil_data),
result=SoilDataBulkUpdateFailure(
reason=SoilDataBulkUpdateFailureReason.NOT_ALLOWED
),
)
)
continue

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

for attr, value in entry["soil_data"].items():
if isinstance(value, enum.Enum):
value = value.value
setattr(site.soil_data, attr, value)

site.soil_data.save()

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

for attr, value in depth_interval_input.items():
if isinstance(value, enum.Enum):
value = value.value
setattr(depth_interval, attr, value)

depth_interval_input["depth_interval"] = interval

depth_interval.save()

results.append(
SoilDataBulkUpdateResultEntry(
site_id=site_id,
result=SoilDataBulkUpdateSuccess(site=site),
)
)

logger.info(results)
history_entry = SoilDataHistory(site=site, changed_by=user, soil_data_changes=entry)
history_entry.save()

return cls(results=results)
except Exception as exc:
logger.info(traceback.format_exc(exc))
return cls(results=results)
69 changes: 69 additions & 0 deletions terraso_backend/apps/soil_id/migrations/0019_soildatahistory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.1.1 on 2024-10-01 20:04

import uuid

import django.db.models.deletion
import rules.contrib.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("project_management", "0029_alter_projectsettings_options"),
("soil_id", "0018_alter_projectsoilsettings_options_and_more"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="SoilDataHistory",
fields=[
("deleted_at", models.DateTimeField(db_index=True, editable=False, null=True)),
("deleted_by_cascade", models.BooleanField(default=False, editable=False)),
(
"id",
models.UUIDField(
default=uuid.uuid4, editable=False, primary_key=True, serialize=False
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("soil_data_changes", models.JSONField()),
(
"changed_by",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
(
"site",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="project_management.site"
),
),
],
options={
"ordering": ["created_at"],
"get_latest_by": "-created_at",
"abstract": False,
},
bases=(rules.contrib.models.RulesModelMixin, models.Model),
),
]
2 changes: 2 additions & 0 deletions terraso_backend/apps/soil_id/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ProjectSoilSettings,
)
from .soil_data import SoilData, SoilDataDepthInterval
from .soil_data_history import SoilDataHistory
from .soil_id_cache import SoilIdCache

__all__ = [
Expand All @@ -35,4 +36,5 @@
"BLMIntervalDefaults",
"DepthIntervalPreset",
"SoilIdCache",
"SoilDataHistory",
]
37 changes: 37 additions & 0 deletions terraso_backend/apps/soil_id/models/soil_data_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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/.

from django.db import models

from apps.core.models import User
from apps.core.models.commons import BaseModel
from apps.project_management.models.sites import Site


class SoilDataHistory(BaseModel):
site = models.ForeignKey(Site, on_delete=models.CASCADE)
changed_by = models.ForeignKey(User, on_delete=models.CASCADE)

# intended JSON schema: {
# ...soilDataInputs,
# "depth_intervals": [{
# "depth_interval": {
# "start": number,
# "end": number
# },
# ...depthIntervalInputs,
# }]
# }
soil_data_changes = models.JSONField()
Loading

0 comments on commit a6b2510

Please sign in to comment.