Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions cms/djangoapps/contentstore/views/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,27 @@ def _list_libraries(request):
"""
org = request.GET.get('org', '')
text_search = request.GET.get('text_search', '').lower()
paginate = request.GET.get('pagination', 'false').lower() == 'true'

if org:
libraries = modulestore().get_libraries(org=org)
else:
libraries = modulestore().get_libraries()

library_count = len(libraries)

if paginate:
page = int(request.GET.get('page', 1))
page_size = int(request.GET.get('page_size', 50))

offset = (page - 1) * page_size
limit = page * page_size
libraries = libraries[offset:limit]

lib_info = [
{
"display_name": lib.display_name,
"library_key": str(lib.location.library_key),
'display_name': lib.display_name,
'library_key': text_type(lib.location.library_key),
}
for lib in libraries
if (
Expand All @@ -172,6 +183,16 @@ def _list_libraries(request):
has_studio_read_access(request.user, lib.location.library_key)
)
]

if paginate:
# This format is used by rest-framework based paginated API endpoints
# so we can use the same response processing on the client side as we
# do with other API endpoints
return JsonResponse({
'results': lib_info,
'count': library_count,
})

return JsonResponse(lib_info)


Expand Down
93 changes: 93 additions & 0 deletions cms/djangoapps/contentstore/views/tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,99 @@ def test_list_libraries(self):
self.assertEqual(entry["display_name"], lib_dict[key].display_name)
del lib_dict[key] # To ensure no duplicates are matched

def test_list_paginated_libraries(self):
"""
Test that we can GET /library/ to list all libraries visible to the current user
with a default 50 items per page using pagination.
"""
# Create some more libraries
libraries = [LibraryFactory.create() for _ in range(3)]
lib_dict = {lib.location.library_key: lib for lib in libraries}

response = self.client.get_json(LIBRARY_REST_URL, {
"pagination": "true",
})

self.assertEqual(response.status_code, 200)
response_data = parse_json(response)
lib_list = response_data["results"]

self.assertEqual(response_data["count"], len(lib_list))
self.assertEqual(len(lib_list), len(libraries))
for entry in lib_list:
self.assertIn("library_key", entry)
self.assertIn("display_name", entry)
key = CourseKey.from_string(entry["library_key"])
self.assertIn(key, lib_dict)
self.assertEqual(entry["display_name"], lib_dict[key].display_name)
del lib_dict[key] # To ensure no duplicates are matched

def test_list_paginated_libraries_with_page_size_set(self):
"""
Test that we can GET /library/ to list all libraries visible to the current user
with a 5 items per page using pagination.
"""
# Create some more libraries
page_size = 5
libraries = [LibraryFactory.create() for _ in range(10)]

response = self.client.get_json(LIBRARY_REST_URL, {
"pagination": "true",
"page_size": page_size
})

self.assertEqual(response.status_code, 200)
response_data = parse_json(response)
lib_list = response_data["results"]

self.assertEqual(response_data["count"], len(libraries))
self.assertEqual(len(lib_list), page_size)

stored_libraries = self.store.get_libraries()
selected_libs = [
{
'display_name': lib.display_name,
'library_key': text_type(lib.location.library_key),
} for lib in stored_libraries[0:page_size]
]

self.assertListEqual(selected_libs, lib_list)

def test_list_paginated_libraries_with_a_specific_page_selected(self):
"""
Test that we can GET /library/ to list all libraries visible to the current user
with a 5 items per page on a specific page using pagination.
"""
# Create some more libraries
page = 2
page_size = 5
libraries = [LibraryFactory.create() for _ in range(15)]

response = self.client.get_json(LIBRARY_REST_URL, {
"pagination": "true",
"page_size": page_size,
"page": page
})

self.assertEqual(response.status_code, 200)
response_data = parse_json(response)
lib_list = response_data["results"]

self.assertEqual(response_data["count"], len(libraries))
self.assertEqual(len(lib_list), page_size)

offset = (page - 1) * page_size
limit = page * page_size
stored_libraries = self.store.get_libraries()
selected_libs = [
{
'display_name': lib.display_name,
'library_key': text_type(lib.location.library_key),
} for lib in stored_libraries[offset:limit]
]

self.assertListEqual(selected_libs, lib_list)

@ddt.data("delete", "put")
def test_bad_http_verb(self, verb):
"""
Expand Down
4 changes: 4 additions & 0 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@
# use the ratelimit backend to prevent brute force attacks
AUTHENTICATION_BACKENDS = [
'rules.permissions.ObjectPermissionBackend',
'openedx.core.djangoapps.content_libraries.auth.LtiAuthenticationBackend',
'openedx.core.djangoapps.oauth_dispatch.dot_overrides.backends.EdxRateLimitedAllowAllUsersModelBackend',
'bridgekeeper.backends.RulePermissionBackend',
]
Expand Down Expand Up @@ -1587,6 +1588,9 @@

# Database-backed Organizations App (http://github.com/edx/edx-organizations)
'organizations',

# Content Library LTI 1.3 Support.
'pylti1p3.contrib.django.lti1p3_tool_config'
]


Expand Down
Loading