Skip to content

Commit

Permalink
feat: Add display_name filtering in GET assets/:course
Browse files Browse the repository at this point in the history
This allows clients to check if a file already exist before
overwriting the asset with new data. See openedx/studio-frontend#384
  • Loading branch information
mavidser authored and ormsbee committed Oct 26, 2023
1 parent 808390a commit 3bda3be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 30 additions & 1 deletion cms/djangoapps/contentstore/asset_storage_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'direction': '',
'asset_type': '',
'text_search': '',
'display_name': '',
}


Expand All @@ -61,7 +62,9 @@ def handle_assets(request, course_key_string=None, asset_key_string=None):
sort: the asset field to sort by (defaults to 'date_added')
direction: the sort direction (defaults to 'descending')
asset_type: the file type to filter items to (defaults to All)
text_search: string to filter results by file name (defaults to '')
text_search: string to perform a search on filenames (defaults to '')
display_name: string to filter results by exact display name (defaults to '').
Use the display_name parameter multiple times to filter by multiple filenames.
POST
json: create or update an asset. The only updating that can be done is changing the lock state.
PUT
Expand Down Expand Up @@ -172,6 +175,9 @@ def _assets_json(request, course_key):

filter_parameters.update(_get_content_type_filter_for_mongo(request_options['requested_asset_type']))

if request_options['requested_display_names']:
filter_parameters.update(_get_displayname_filter_for_mongo(request_options['requested_display_names']))

if request_options['requested_text_search']:
filter_parameters.update(_get_displayname_search_filter_for_mongo(request_options['requested_text_search']))

Expand Down Expand Up @@ -223,13 +229,18 @@ def _parse_request_to_dictionary(request):
'requested_sort_direction': _get_requested_attribute(request, 'direction'),
'requested_asset_type': _get_requested_attribute(request, 'asset_type'),
'requested_text_search': _get_requested_attribute(request, 'text_search'),
'requested_display_names': _get_requested_attribute_list(request, 'display_name'),
}


def _get_requested_attribute(request, attribute):
return request.GET.get(attribute, REQUEST_DEFAULTS.get(attribute))


def _get_requested_attribute_list(request, attribute):
return request.GET.getlist(attribute, REQUEST_DEFAULTS.get(attribute))


def _get_error_if_invalid_parameters(requested_filter):
"""Function for returning error messages on filters"""
requested_file_types = _get_requested_file_types_from_requested_filter(requested_filter)
Expand Down Expand Up @@ -303,6 +314,24 @@ def _get_mongo_expression_for_type_filter(requested_file_types):
}


def _get_displayname_filter_for_mongo(displaynames):
"""
Construct and return pymongo query dict, filtering for the given list of displaynames.
"""
filters = []

for displayname in displaynames:
filters.append({
'displayname': {
'$eq': displayname,
},
})

return {
'$or': filters,
}


def _get_displayname_search_filter_for_mongo(text_search):
"""
Return a pymongo query dict for the given search string, using case insensitivity.
Expand Down
3 changes: 3 additions & 0 deletions cms/djangoapps/contentstore/views/tests/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def test_json_responses(self):
self.assert_correct_asset_response(self.url + "?page_size=2", 0, 2, 4)
self.assert_correct_asset_response(
self.url + "?page_size=2&page=1", 2, 2, 4)
self.assert_correct_asset_response(self.url + '?display_name=asset-1.txt', 0, 1, 1)
self.assert_correct_asset_response(self.url + '?display_name=asset-1.txt&display_name=asset-2.txt', 0, 2, 2)
self.assert_correct_asset_response(self.url + '?display_name=asset-1.txt&display_name=asset-0.txt', 0, 1, 1)
self.assert_correct_sort_response(self.url, 'date_added', 'asc')
self.assert_correct_sort_response(self.url, 'date_added', 'desc')
self.assert_correct_sort_response(self.url, 'display_name', 'asc')
Expand Down

0 comments on commit 3bda3be

Please sign in to comment.