-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
348 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
terraso_backend/apps/soil_id/graphql/soil_id/soil_data/types.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import enum | ||
import traceback | ||
|
||
import graphene | ||
import structlog | ||
from django.db import transaction | ||
|
||
from apps.graphql.schema.commons import BaseWriteMutation | ||
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.models.soil_data import SoilData | ||
|
||
logger = structlog.get_logger(__name__) | ||
|
||
|
||
class SoilDataBulkUpdateSuccess(graphene.ObjectType): | ||
soil_data = graphene.Field(SoilDataNode, required=True) | ||
|
||
|
||
# TODO: just a generic "can't access" result? | ||
class SoilDataBulkUpdateFailureReason(graphene.Enum): | ||
DOES_NOT_EXIST = "DOES_NOT_EXIST" | ||
NOT_ALLOWED = "NOT_ALLOWED" | ||
|
||
|
||
class SoilDataBulkUpdateFailure(graphene.ObjectType): | ||
reason = graphene.Field(SoilDataBulkUpdateFailureReason, required=True) | ||
|
||
|
||
class SoilDataBulkUpdateResult(graphene.Union): | ||
class Meta: | ||
types = (SoilDataBulkUpdateSuccess, SoilDataBulkUpdateFailure) | ||
|
||
|
||
class SoilDataBulkUpdateResultEntry(graphene.ObjectType): | ||
site_id = graphene.ID(required=True) | ||
result = graphene.Field(SoilDataBulkUpdateResult, required=True) | ||
|
||
|
||
class SoilDataBulkUpdateDepthDependentEntry(SoilDataDepthDependentInputs, graphene.InputObjectType): | ||
pass | ||
|
||
|
||
class SoilDataBulkUpdateEntry(SoilDataInputs, graphene.InputObjectType): | ||
site_id = graphene.ID(required=True) | ||
depth_intervals = graphene.Field( | ||
graphene.List(graphene.NonNull(SoilDataBulkUpdateDepthDependentEntry)), required=True | ||
) | ||
|
||
|
||
class SoilDataBulkUpdate(BaseWriteMutation): | ||
results = graphene.Field( | ||
graphene.List(graphene.NonNull(SoilDataBulkUpdateResultEntry)), required=True | ||
) | ||
|
||
class Input: | ||
soil_data = graphene.Field( | ||
graphene.List(graphene.NonNull(SoilDataBulkUpdateEntry)), required=True | ||
) | ||
|
||
@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 | ||
|
||
if not hasattr(site, "soil_data"): | ||
site.soil_data = SoilData() | ||
|
||
for attr, value in entry.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.save() | ||
|
||
results.append( | ||
SoilDataBulkUpdateResultEntry( | ||
site_id=site_id, | ||
result=SoilDataBulkUpdateSuccess(soil_data=site.soil_data), | ||
) | ||
) | ||
|
||
logger.info(results) | ||
|
||
return cls(results=results) | ||
except Exception as exc: | ||
logger.info(traceback.format_exc(exc)) |
Oops, something went wrong.