From 7d81af7ea8814186efae1508e3b87b014788bb34 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Wed, 18 Oct 2023 10:05:06 -0500 Subject: [PATCH] fix: Small fixes --- .../apps/core/models/shared_resources.py | 2 +- .../apps/shared_data/permission_rules.py | 2 + terraso_backend/apps/shared_data/views.py | 66 +++++++------------ terraso_backend/tests/graphql/conftest.py | 15 +++-- 4 files changed, 37 insertions(+), 48 deletions(-) diff --git a/terraso_backend/apps/core/models/shared_resources.py b/terraso_backend/apps/core/models/shared_resources.py index 45e51d744..211117fde 100644 --- a/terraso_backend/apps/core/models/shared_resources.py +++ b/terraso_backend/apps/core/models/shared_resources.py @@ -23,7 +23,7 @@ class SharedResource(BaseModel): """ This model represents a shared resource. Source represents the resource that is being shared (Example: DataEntry). - Target represents the resource that is receiving the shared resource (Example: Lanscape). + Target represents the resource that is receiving the shared resource (Example: Landscape). """ source = GenericForeignKey("source_content_type", "source_object_id") diff --git a/terraso_backend/apps/shared_data/permission_rules.py b/terraso_backend/apps/shared_data/permission_rules.py index 3f75c29f2..cec25cacd 100644 --- a/terraso_backend/apps/shared_data/permission_rules.py +++ b/terraso_backend/apps/shared_data/permission_rules.py @@ -39,6 +39,7 @@ def is_user_allowed_to_view_data_entry(data_entry, user): for shared_resource in shared_resources: if is_target_member(user, shared_resource.target): return True + return False def is_user_allowed_to_change_data_entry(data_entry, user): @@ -46,6 +47,7 @@ def is_user_allowed_to_change_data_entry(data_entry, user): for shared_resource in shared_resources: if is_target_manager(user, shared_resource.target): return True + return False @rules.predicate diff --git a/terraso_backend/apps/shared_data/views.py b/terraso_backend/apps/shared_data/views.py index ef98af751..7b3fc47a8 100644 --- a/terraso_backend/apps/shared_data/views.py +++ b/terraso_backend/apps/shared_data/views.py @@ -47,24 +47,13 @@ def post(self, request, **kwargs): target_slug = form_data.pop("target_slug")[0] if target_type not in ["group", "landscape"]: logger.error("Invalid target_type provided when adding dataEntry") - return JsonResponse( - { - "errors": [ - { - "message": [ - asdict( - ErrorMessage( - code="Invalid target_type provided when adding dataEntry", - context=ErrorContext( - model="DataEntry", field="target_type" - ), - ) - ) - ] - } - ] - }, - status=400, + return get_json_response_error( + [ + ErrorMessage( + code="Invalid target_type provided when adding dataEntry", + context=ErrorContext(model="DataEntry", field="target_type"), + ) + ] ) content_type = ContentType.objects.get(app_label="core", model=target_type) @@ -77,24 +66,13 @@ def post(self, request, **kwargs): "Target not found when adding dataEntry", extra={"target_type": target_type, "target_slug": target_slug}, ) - return JsonResponse( - { - "errors": [ - { - "message": [ - asdict( - ErrorMessage( - code="Target not found when adding dataEntry", - context=ErrorContext( - model="DataEntry", field="target_type" - ), - ) - ) - ] - } - ] - }, - status=400, + return get_json_response_error( + [ + ErrorMessage( + code="Target not found when adding dataEntry", + context=ErrorContext(model="DataEntry", field="target_type"), + ) + ] ) if has_multiple_files(request.FILES.getlist("data_file")): @@ -102,27 +80,25 @@ def post(self, request, **kwargs): code="Uploaded more than one file", context=ErrorContext(model="DataEntry", field="data_file"), ) - return JsonResponse({"errors": [{"message": [asdict(error_message)]}]}, status=400) + return get_json_response_error([error_message]) if is_file_upload_oversized(request.FILES.getlist("data_file"), MEDIA_UPLOAD_MAX_FILE_SIZE): error_message = ErrorMessage( code="File size exceeds 10 MB", context=ErrorContext(model="DataEntry", field="data_file"), ) - return JsonResponse({"errors": [{"message": [asdict(error_message)]}]}, status=400) + return get_json_response_error([error_message]) if not is_valid_shared_data_type(request.FILES.getlist("data_file")): error_message = ErrorMessage( code="invalid_media_type", context=ErrorContext(model="Shared Data", field="context_type"), ) - return JsonResponse({"errors": [{"message": [asdict(error_message)]}]}, status=400) + return get_json_response_error([error_message]) entry_form = DataEntryForm(data=form_data, files=request.FILES) if not entry_form.is_valid(): error_messages = get_error_messages(entry_form.errors.as_data()) - return JsonResponse( - {"errors": [{"message": [asdict(e) for e in error_messages]}]}, status=400 - ) + return get_json_response_error(error_messages) data_entry = entry_form.save() @@ -154,3 +130,9 @@ def get_error_messages(validation_errors): ) return error_messages + + +def get_json_response_error(error_messages, status=400): + return JsonResponse( + {"errors": [{"message": [asdict(e) for e in error_messages]}]}, status=status + ) diff --git a/terraso_backend/tests/graphql/conftest.py b/terraso_backend/tests/graphql/conftest.py index 3811688c1..e6db1caf2 100644 --- a/terraso_backend/tests/graphql/conftest.py +++ b/terraso_backend/tests/graphql/conftest.py @@ -251,7 +251,9 @@ def data_entry_current_user_file(users, groups): resource = mixer.blend( SharedResource, target=creator_group, - source=mixer.blend(DataEntry, created_by=creator, entry_type=DataEntry.ENTRY_TYPE_FILE), + source=mixer.blend( + DataEntry, slug=None, created_by=creator, size=100, entry_type=DataEntry.ENTRY_TYPE_FILE + ), ) return resource.source @@ -264,7 +266,9 @@ def data_entry_current_user_link(users, groups): resource = mixer.blend( SharedResource, target=creator_group, - source=mixer.blend(DataEntry, created_by=creator, entry_type=DataEntry.ENTRY_TYPE_LINK), + source=mixer.blend( + DataEntry, slug=None, created_by=creator, entry_type=DataEntry.ENTRY_TYPE_LINK + ), ) return resource.source @@ -277,7 +281,7 @@ def data_entry_other_user(users, groups): resource = mixer.blend( SharedResource, target=creator_group, - source=mixer.blend(DataEntry, created_by=creator, size=100), + source=mixer.blend(DataEntry, slug=None, created_by=creator, size=100), ) return resource.source @@ -337,16 +341,17 @@ def visualization_configs(users, groups): creator = users[0] creator_group = groups[1] creator_group.members.add(creator) - return mixer.cycle(5).blend( + visualizations = mixer.cycle(5).blend( VisualizationConfig, created_by=creator, data_entry=lambda: mixer.blend( SharedResource, target=creator_group, - source=lambda: mixer.blend(DataEntry, created_by=creator), + source=lambda: mixer.blend(DataEntry, created_by=creator, size=100), ).source, owner=creator_group, ) + return visualizations @pytest.fixture