Skip to content

Commit

Permalink
fix: Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Oct 24, 2023
1 parent f191ba7 commit 7d81af7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion terraso_backend/apps/core/models/shared_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions terraso_backend/apps/shared_data/permission_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ 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):
shared_resources = data_entry.shared_resources.all()
for shared_resource in shared_resources:
if is_target_manager(user, shared_resource.target):
return True
return False


@rules.predicate
Expand Down
66 changes: 24 additions & 42 deletions terraso_backend/apps/shared_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -77,52 +66,39 @@ 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")):
error_message = ErrorMessage(
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()

Expand Down Expand Up @@ -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
)
15 changes: 10 additions & 5 deletions terraso_backend/tests/graphql/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7d81af7

Please sign in to comment.