Skip to content

Commit

Permalink
feat: Use default pagination class for v2 library views (#34879)
Browse files Browse the repository at this point in the history
This ensures `num_pages` is included in paginated response.
  • Loading branch information
yusuf-musleh committed Jun 11, 2024
1 parent 0b45e99 commit 221e333
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_library_validation(self):
}

@skip("This endpoint shouldn't support num_blocks and has_unpublished_*.")
@patch("openedx.core.djangoapps.content_libraries.views.LibraryApiPagination.page_size", new=2)
@patch("openedx.core.djangoapps.content_libraries.views.LibraryRootView.pagination_class.page_size", new=2)
def test_list_library(self):
"""
Test the /libraries API and its pagination
Expand Down Expand Up @@ -374,7 +374,7 @@ def test_library_blocks_studio_view(self):
assert 'resources' in fragment
assert 'Hello world!' in fragment['content']

@patch("openedx.core.djangoapps.content_libraries.views.LibraryApiPagination.page_size", new=2)
@patch("openedx.core.djangoapps.content_libraries.views.LibraryBlocksView.pagination_class.page_size", new=2)
def test_list_library_blocks(self):
"""
Test the /libraries/{lib_key_str}/blocks API and its pagination
Expand Down
38 changes: 17 additions & 21 deletions openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@
from organizations.models import Organization
from rest_framework import status
from rest_framework.exceptions import NotFound, PermissionDenied, ValidationError
from rest_framework.pagination import PageNumberPagination
from rest_framework.generics import GenericAPIView
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.viewsets import GenericViewSet

from openedx.core.djangoapps.content_libraries import api, permissions
from openedx.core.djangoapps.content_libraries.serializers import (
Expand Down Expand Up @@ -153,13 +153,10 @@ def wrapped_fn(*args, **kwargs):
return wrapped_fn


class LibraryApiPagination(PageNumberPagination):
class LibraryApiPaginationDocs:
"""
Paginates over ContentLibraryMetadata objects.
API docs for query params related to paginating ContentLibraryMetadata objects.
"""
page_size = 50
page_size_query_param = 'page_size'

apidoc_params = [
apidocs.query_parameter(
'pagination',
Expand All @@ -181,14 +178,14 @@ class LibraryApiPagination(PageNumberPagination):

@method_decorator(non_atomic_requests, name="dispatch")
@view_auth_classes()
class LibraryRootView(APIView):
class LibraryRootView(GenericAPIView):
"""
Views to list, search for, and create content libraries.
"""

@apidocs.schema(
parameters=[
*LibraryApiPagination.apidoc_params,
*LibraryApiPaginationDocs.apidoc_params,
apidocs.query_parameter(
'org',
str,
Expand All @@ -211,21 +208,20 @@ def get(self, request):
library_type = serializer.validated_data['type']
text_search = serializer.validated_data['text_search']

paginator = LibraryApiPagination()
queryset = api.get_libraries_for_user(
request.user,
org=org,
library_type=library_type,
text_search=text_search,
)
paginated_qs = paginator.paginate_queryset(queryset, request)
paginated_qs = self.paginate_queryset(queryset)
result = api.get_metadata(paginated_qs)

serializer = ContentLibraryMetadataSerializer(result, many=True)
# Verify `pagination` param to maintain compatibility with older
# non pagination-aware clients
if request.GET.get('pagination', 'false').lower() == 'true':
return paginator.get_paginated_response(serializer.data)
return self.get_paginated_response(serializer.data)
return Response(serializer.data)

def post(self, request):
Expand Down Expand Up @@ -506,13 +502,14 @@ def delete(self, request, lib_key_str): # pylint: disable=unused-argument

@method_decorator(non_atomic_requests, name="dispatch")
@view_auth_classes()
class LibraryBlocksView(APIView):
class LibraryBlocksView(GenericAPIView):
"""
Views to work with XBlocks in a specific content library.
"""

@apidocs.schema(
parameters=[
*LibraryApiPagination.apidoc_params,
*LibraryApiPaginationDocs.apidoc_params,
apidocs.query_parameter(
'text_search',
str,
Expand All @@ -538,13 +535,12 @@ def get(self, request, lib_key_str):
api.require_permission_for_library_key(key, request.user, permissions.CAN_VIEW_THIS_CONTENT_LIBRARY)
components = api.get_library_components(key, text_search=text_search, block_types=block_types)

paginator = LibraryApiPagination()
paginated_xblock_metadata = [
api.LibraryXBlockMetadata.from_component(key, component)
for component in paginator.paginate_queryset(components, request)
for component in self.paginate_queryset(components)
]
serializer = LibraryXBlockMetadataSerializer(paginated_xblock_metadata, many=True)
return paginator.get_paginated_response(serializer.data)
return self.get_paginated_response(serializer.data)

@convert_exceptions
def post(self, request, lib_key_str):
Expand Down Expand Up @@ -742,7 +738,7 @@ def delete(self, request, usage_key_str, file_path):

@method_decorator(non_atomic_requests, name="dispatch")
@view_auth_classes()
class LibraryImportTaskViewSet(ViewSet):
class LibraryImportTaskViewSet(GenericViewSet):
"""
Import blocks from Courseware through modulestore.
"""
Expand All @@ -760,9 +756,9 @@ def list(self, request, lib_key_str):
)
queryset = api.ContentLibrary.objects.get_by_key(library_key).import_tasks
result = ContentLibraryBlockImportTaskSerializer(queryset, many=True).data
paginator = LibraryApiPagination()
return paginator.get_paginated_response(
paginator.paginate_queryset(result, request)

return self.get_paginated_response(
self.paginate_queryset(result)
)

@convert_exceptions
Expand Down

0 comments on commit 221e333

Please sign in to comment.