Skip to content

Commit

Permalink
fix: Added constant for valid target types for data entries, added mo…
Browse files Browse the repository at this point in the history
…re specific exception
  • Loading branch information
josebui committed Oct 24, 2023
1 parent 7d81af7 commit ed3430a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 3 additions & 2 deletions terraso_backend/apps/graphql/schema/data_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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},
Expand Down
2 changes: 2 additions & 0 deletions terraso_backend/apps/shared_data/models/data_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
3 changes: 2 additions & 1 deletion terraso_backend/apps/shared_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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(
[
Expand Down

0 comments on commit ed3430a

Please sign in to comment.