Skip to content

Commit

Permalink
feat: api to restore soft-deleted component
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Dec 9, 2024
1 parent 9d9a685 commit 663d5d8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
32 changes: 32 additions & 0 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,38 @@ def delete_library_block(usage_key, remove_from_parent=True):
)


def restore_library_block(usage_key):
"""
Restore the specified library block.
"""
component = get_component_from_usage_key(usage_key)
library_key = usage_key.context_key
affected_collections = authoring_api.get_entity_collections(component.learning_package_id, component.key)

# Set draft version back to the latest available component version id.
authoring_api.set_draft_version(component.pk, component.versioning.latest.pk)

LIBRARY_BLOCK_CREATED.send_event(
library_block=LibraryBlockData(
library_key=library_key,
usage_key=usage_key
)
)

# For each collection, trigger LIBRARY_COLLECTION_UPDATED signal and set background=True to trigger
# collection indexing asynchronously.
#
# To delete the component on collections
for collection in affected_collections:
LIBRARY_COLLECTION_UPDATED.send_event(
library_collection=LibraryCollectionData(
library_key=library_key,
collection_key=collection.key,
background=True,
)
)


def get_library_block_static_asset_files(usage_key) -> list[LibraryXBlockStaticFile]:
"""
Given an XBlock in a content library, list all the static asset files
Expand Down
1 change: 1 addition & 0 deletions openedx/core/djangoapps/content_libraries/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
path('blocks/<str:usage_key_str>/', include([
# Get metadata about a specific XBlock in this library, or delete the block:
path('', views.LibraryBlockView.as_view()),
path('restore/', views.LibraryBlockRestore.as_view()),
# Update collections for a given component
path('collections/', views.LibraryBlockCollectionsView.as_view(), name='update-collections'),
# Get the LTI URL of a specific XBlock
Expand Down
16 changes: 16 additions & 0 deletions openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,22 @@ def delete(self, request, usage_key_str): # pylint: disable=unused-argument
return Response({})


@view_auth_classes()
class LibraryBlockRestore(APIView):
"""
View to restore soft-deleted library xblocks.
"""
@convert_exceptions
def post(self, request, usage_key_str) -> Response:
"""
Restores a soft-deleted library block that belongs to a Content Library
"""
key = LibraryUsageLocatorV2.from_string(usage_key_str)
api.require_permission_for_library_key(key.lib_key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY)
api.restore_library_block(key)
return Response(None, status=status.HTTP_204_NO_CONTENT)


@method_decorator(non_atomic_requests, name="dispatch")
@view_auth_classes()
class LibraryBlockCollectionsView(APIView):
Expand Down

0 comments on commit 663d5d8

Please sign in to comment.