Skip to content

Commit

Permalink
Merge pull request #43 from kartoza/feat-find-layer-by-client-ids
Browse files Browse the repository at this point in the history
add API fetch layer by client id
  • Loading branch information
danangmassandy authored Jun 6, 2024
2 parents 802c406 + ffe97ce commit 52379d9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
46 changes: 45 additions & 1 deletion django_project/cplus_api/api_views/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
InputLayerSerializer,
PaginatedInputLayerSerializer,
UploadLayerSerializer,
FinishUploadLayerSerializer
FinishUploadLayerSerializer,
LAYER_SCHEMA_FIELDS
)
from cplus_api.serializers.common import (
APIErrorSerializer,
Expand Down Expand Up @@ -698,3 +699,46 @@ def post(self, request, *args, **kwargs):
'unavailable': list(ids_not_available),
'invalid': list(input_ids - ids_found)
})


class FetchLayerByClientId(APIView):
"""API to fetch input layer by client id."""
permission_classes = [IsAuthenticated]

@swagger_auto_schema(
operation_id='fetch-layer-by-client-id',
operation_description='API to fetch input layer by client id.',
tags=[LAYER_API_TAG],
request_body=openapi.Schema(
title='List of client id',
type=openapi.TYPE_ARRAY,
items=openapi.Items(
type=openapi.TYPE_STRING
)
),
responses={
200: openapi.Schema(
description=(
'Layer List'
),
type=openapi.TYPE_ARRAY,
items=openapi.Items(**LAYER_SCHEMA_FIELDS),
),
400: APIErrorSerializer,
403: APIErrorSerializer,
404: APIErrorSerializer
}
)
def post(self, request, *args, **kwargs):
layers = InputLayer.objects.filter(
client_id__in=request.data
).order_by('name')
results = []
for layer in layers:
if not validate_layer_access(layer, request.user):
continue
results.append(layer)
return Response(status=200, data=InputLayerSerializer(
results,
many=True
).data)
37 changes: 36 additions & 1 deletion django_project/cplus_api/tests/test_layer_api_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
CheckLayer,
is_internal_user,
validate_layer_access,
LayerUploadAbort
LayerUploadAbort,
FetchLayerByClientId
)
from cplus_api.models.profile import UserProfile
from cplus_api.utils.api_helper import convert_size
Expand Down Expand Up @@ -837,3 +838,37 @@ def test_abort_multipart_upload_with_exc(self, mocked_s3):
uuid=input_layer.uuid
).exists()
)

def test_layer_fetch_by_client_id(self):
view = FetchLayerByClientId.as_view()
payload = [
'ncs_pathways--Final_Alien_Invasive_Plant_priority_norm.tif'
'_4326_20_20_1072586664'
]
request = self.factory.post(
reverse('v1:fetch-layer-by-client-id'),
data=payload, format='json'
)
request.resolver_match = FakeResolverMatchV1
request.user = self.superuser
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [])
input_layer = InputLayerF.create(
privacy_type=InputLayer.PrivacyTypes.COMMON,
client_id=payload[0]
)
request = self.factory.post(
reverse('v1:fetch-layer-by-client-id'),
data=payload, format='json'
)
request.resolver_match = FakeResolverMatchV1
request.user = self.user_1
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)
find_layer = self.find_layer_from_response(
response.data, input_layer.uuid)
self.assertTrue(find_layer)
self.assertFalse(find_layer['url'])
self.assertFalse(input_layer.file)
8 changes: 7 additions & 1 deletion django_project/cplus_api/urls_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from cplus_api.api_views.layer import (
LayerList, LayerDetail, LayerUpload,
LayerUploadStart, LayerUploadFinish,
CheckLayer, LayerUploadAbort
CheckLayer, LayerUploadAbort,
FetchLayerByClientId
)
from cplus_api.api_views.scenario import (
ScenarioAnalysisSubmit,
Expand Down Expand Up @@ -36,6 +37,11 @@
LayerList.as_view(),
name='layer-list'
),
path(
'layer/filter/client_id/',
FetchLayerByClientId.as_view(),
name='fetch-layer-by-client-id'
),
path(
'layer/upload/start/',
LayerUploadStart.as_view(),
Expand Down

0 comments on commit 52379d9

Please sign in to comment.