From ed3430adbf10347f8ae687e5ebe4f8464f5b32eb Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Mon, 23 Oct 2023 11:15:07 -0500 Subject: [PATCH] fix: Added constant for valid target types for data entries, added more specific exception --- terraso_backend/apps/graphql/schema/data_entries.py | 5 +++-- terraso_backend/apps/shared_data/models/data_entries.py | 2 ++ terraso_backend/apps/shared_data/views.py | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/terraso_backend/apps/graphql/schema/data_entries.py b/terraso_backend/apps/graphql/schema/data_entries.py index 0a9991740..5ba5689d2 100644 --- a/terraso_backend/apps/graphql/schema/data_entries.py +++ b/terraso_backend/apps/graphql/schema/data_entries.py @@ -26,6 +26,7 @@ from apps.core.models import Group, Landscape, Membership from apps.graphql.exceptions import GraphQLNotAllowedException, GraphQLNotFoundException from apps.shared_data.models import DataEntry +from apps.shared_data.models.data_entries import VALID_TARGET_TYPES from .commons import BaseDeleteMutation, BaseWriteMutation, TerrasoConnection from .constants import MutationTypes @@ -139,7 +140,7 @@ def mutate_and_get_payload(cls, root, info, **kwargs): target_type = kwargs.pop("target_type") target_slug = kwargs.pop("target_slug") - if target_type not in ["group", "landscape"]: + if target_type not in VALID_TARGET_TYPES: logger.error("Invalid target_type provided when adding dataEntry") raise GraphQLNotFoundException( field="target_type", @@ -151,7 +152,7 @@ def mutate_and_get_payload(cls, root, info, **kwargs): try: target = model_class.objects.get(slug=target_slug) - except Exception: + except model_class.DoesNotExist: logger.error( "Target not found when adding dataEntry", extra={"target_type": target_type, "target_slug": target_slug}, diff --git a/terraso_backend/apps/shared_data/models/data_entries.py b/terraso_backend/apps/shared_data/models/data_entries.py index 62228d397..77c0b1a7b 100644 --- a/terraso_backend/apps/shared_data/models/data_entries.py +++ b/terraso_backend/apps/shared_data/models/data_entries.py @@ -23,6 +23,8 @@ from apps.shared_data import permission_rules as perm_rules from apps.shared_data.services import DataEntryFileStorage +VALID_TARGET_TYPES = ["group", "landscape"] + class DataEntry(BaseModel): """ diff --git a/terraso_backend/apps/shared_data/views.py b/terraso_backend/apps/shared_data/views.py index 7b3fc47a8..035ef02c9 100644 --- a/terraso_backend/apps/shared_data/views.py +++ b/terraso_backend/apps/shared_data/views.py @@ -30,6 +30,7 @@ from .forms import DataEntryForm from .models import DataEntry +from .models.data_entries import VALID_TARGET_TYPES logger = structlog.get_logger(__name__) @@ -45,7 +46,7 @@ def post(self, request, **kwargs): form_data["entry_type"] = DataEntry.ENTRY_TYPE_FILE target_type = form_data.pop("target_type")[0] target_slug = form_data.pop("target_slug")[0] - if target_type not in ["group", "landscape"]: + if target_type not in VALID_TARGET_TYPES: logger.error("Invalid target_type provided when adding dataEntry") return get_json_response_error( [