Skip to content

Commit

Permalink
feat: add API endpoint for asset usage search (#33092)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinAoki authored Aug 30, 2023
1 parent ddf4ea6 commit 878e720
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
45 changes: 45 additions & 0 deletions cms/djangoapps/contentstore/asset_storage_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,51 @@ def handle_assets(request, course_key_string=None, asset_key_string=None):
return HttpResponseNotFound()


def get_asset_usage_path(request, course_key, asset_key_string):
"""
Get a list of units with ancestors that use given asset.
"""
course_key = CourseKey.from_string(course_key)
if not has_course_author_access(request.user, course_key):
raise PermissionDenied()
asset_location = AssetKey.from_string(asset_key_string) if asset_key_string else None
store = modulestore()
usage_locations = []
static_path = StaticContent.get_static_path_from_location(asset_location)
verticals = store.get_items(
course_key,
qualifiers={
'category': 'vertical'
},
)
blocks = []

for vertical in verticals:
blocks.extend(vertical.get_children())

for block in blocks:
is_video_block = getattr(block, 'category', '') == 'video'
if is_video_block:
handout = getattr(block, 'handout', '')
if handout and str(asset_location) in handout:
unit = block.get_parent()
subsection = unit.get_parent()
subsection_display_name = getattr(subsection, 'display_name', '')
unit_display_name = getattr(unit, 'display_name', '')
xblock_display_name = getattr(block, 'display_name', '')
usage_locations.append(f'{subsection_display_name} - {unit_display_name} / {xblock_display_name}')
else:
data = getattr(block, 'data', '')
if static_path in data or str(asset_location) in data:
unit = block.get_parent()
subsection = unit.get_parent()
subsection_display_name = getattr(subsection, 'display_name', '')
unit_display_name = getattr(unit, 'display_name', '')
xblock_display_name = getattr(block, 'display_name', '')
usage_locations.append(f'{subsection_display_name} - {unit_display_name} / {xblock_display_name}')
return JsonResponse({'usage_locations': usage_locations})


def _get_response_format(request):
return request.GET.get('format') or request.POST.get('format') or 'html'

Expand Down
10 changes: 9 additions & 1 deletion cms/djangoapps/contentstore/views/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from django.views.decorators.csrf import ensure_csrf_cookie
from cms.djangoapps.contentstore.asset_storage_handlers import (
handle_assets,
get_asset_usage_path,
update_course_run_asset as update_course_run_asset_source_function,
get_file_size as get_file_size_source_function,
delete_asset as delete_asset_source_function,
get_asset_json as get_asset_json_source_function,
update_asset as update_asset_source_function,

)

__all__ = ['assets_handler']
__all__ = ['assets_handler', 'asset_usage_path_handler']

REQUEST_DEFAULTS = {
'page': 0,
Expand Down Expand Up @@ -52,6 +54,12 @@ def assets_handler(request, course_key_string=None, asset_key_string=None):
return handle_assets(request, course_key_string, asset_key_string)


@login_required
@ensure_csrf_cookie
def asset_usage_path_handler(request, course_key_string, asset_key_string):
return get_asset_usage_path(request, course_key_string, asset_key_string)


def update_course_run_asset(course_key, upload_file):
"""Exposes service method in asset_storage_handlers without breaking existing bindings/dependencies"""
return update_course_run_asset_source_function(course_key, upload_file)
Expand Down
2 changes: 1 addition & 1 deletion cms/templates/widgets/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ <h3 class="title"><span class="label"><span class="label-prefix sr">${_("Course"
%endif
%if files_uploads_mfe_enabled:
<li class="nav-item nav-course-courseware-uploads">
<a href="${get_files_uploads_url(course_key)}">${_("Files & Media")}</a>
<a href="${get_files_uploads_url(course_key)}">${_("Files & Uploads")}</a>
</li>
%endif
% if not pages_and_resources_mfe_enabled:
Expand Down
3 changes: 3 additions & 0 deletions cms/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
re_path(fr'^assets/{settings.COURSE_KEY_PATTERN}/{settings.ASSET_KEY_PATTERN}?$',
contentstore_views.assets_handler,
name='assets_handler'),
re_path(fr'^assets/{settings.COURSE_KEY_PATTERN}/{settings.ASSET_KEY_PATTERN}/usage',
contentstore_views.asset_usage_path_handler,
name='asset_usage_path_handler'),
re_path(fr'^import/{COURSELIKE_KEY_PATTERN}$', contentstore_views.import_handler,
name='import_handler'),
re_path(fr'^import_status/{COURSELIKE_KEY_PATTERN}/(?P<filename>.+)$',
Expand Down

0 comments on commit 878e720

Please sign in to comment.