Skip to content

Commit

Permalink
feat: adds views to soft-delete and restore a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Sep 20, 2024
1 parent 494d1a7 commit 5a71333
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
URL_PREFIX = '/api/libraries/v2/{lib_key}/'
URL_LIB_COLLECTIONS = URL_PREFIX + 'collections/'
URL_LIB_COLLECTION = URL_LIB_COLLECTIONS + '{collection_key}/'
URL_LIB_COLLECTION_RESTORE = URL_LIB_COLLECTIONS + '{collection_key}/restore/'
URL_LIB_COLLECTION_COMPONENTS = URL_LIB_COLLECTION + 'components/'


Expand Down Expand Up @@ -330,15 +331,33 @@ def test_update_invalid_library_collection(self):

def test_delete_library_collection(self):
"""
Test deleting a Content Library Collection
Note: Currently not implemented and should return a 405
Test soft-deleting and restoring a Content Library Collection
"""
resp = self.client.delete(
URL_LIB_COLLECTION.format(lib_key=self.lib2.library_key, collection_key=self.col3.key)
)
assert resp.status_code == 204

assert resp.status_code == 405
resp = self.client.get(
URL_LIB_COLLECTION.format(lib_key=self.lib2.library_key, collection_key=self.col3.key)
)
assert resp.status_code == 404

resp = self.client.post(
URL_LIB_COLLECTION_RESTORE.format(lib_key=self.lib2.library_key, collection_key=self.col3.key)
)
assert resp.status_code == 204

resp = self.client.get(
URL_LIB_COLLECTION.format(lib_key=self.lib2.library_key, collection_key=self.col3.key)
)
# Check that correct Content Library Collection data retrieved
expected_collection = {
"title": "Collection 3",
"description": "Description for Collection 3",
}
assert resp.status_code == 200
self.assertDictContainsEntries(resp.data, expected_collection)

def test_get_components(self):
"""
Expand Down
30 changes: 24 additions & 6 deletions openedx/core/djangoapps/content_libraries/views_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED
from rest_framework.status import HTTP_204_NO_CONTENT

from opaque_keys.edx.locator import LibraryLocatorV2
from openedx_learning.api import authoring as authoring_api
Expand Down Expand Up @@ -163,13 +163,31 @@ def partial_update(self, request, *args, **kwargs) -> Response:
@convert_exceptions
def destroy(self, request, *args, **kwargs) -> Response:
"""
Deletes a Collection that belongs to a Content Library
Note: (currently not allowed)
Soft-deletes a Collection that belongs to a Content Library
"""
# TODO: Implement the deletion logic and emit event signal
collection = super().get_object()
assert collection.learning_package_id
authoring_api.delete_collection(
collection.learning_package_id,
collection.key,
hard_delete=False,
)
return Response(None, status=HTTP_204_NO_CONTENT)

return Response(None, status=HTTP_405_METHOD_NOT_ALLOWED)
@convert_exceptions
@action(detail=True, methods=['post'], url_path='restore', url_name='collection-restore')
def restore(self, request, *args, **kwargs) -> Response:
"""
Restores a soft-deleted Collection that belongs to a Content Library
"""
content_library = self.get_content_library()
assert content_library.learning_package_id
collection_key = kwargs["key"]
authoring_api.restore_collection(
content_library.learning_package_id,
collection_key,
)
return Response(None, status=HTTP_204_NO_CONTENT)

@convert_exceptions
@action(detail=True, methods=['delete', 'patch'], url_path='components', url_name='components-update')
Expand Down

0 comments on commit 5a71333

Please sign in to comment.