From 3bda3bed4ae2007763ce9197ba5258618f95d093 Mon Sep 17 00:00:00 2001 From: Sid Verma Date: Sun, 15 Oct 2023 21:03:51 +0000 Subject: [PATCH] feat: Add display_name filtering in GET assets/:course This allows clients to check if a file already exist before overwriting the asset with new data. See openedx/studio-frontend#384 --- .../contentstore/asset_storage_handlers.py | 31 ++++++++++++++++++- .../contentstore/views/tests/test_assets.py | 3 ++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/asset_storage_handlers.py b/cms/djangoapps/contentstore/asset_storage_handlers.py index 8808dc4f0b53..82aef8d5ec76 100644 --- a/cms/djangoapps/contentstore/asset_storage_handlers.py +++ b/cms/djangoapps/contentstore/asset_storage_handlers.py @@ -43,6 +43,7 @@ 'direction': '', 'asset_type': '', 'text_search': '', + 'display_name': '', } @@ -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 @@ -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'])) @@ -223,6 +229,7 @@ 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'), } @@ -230,6 +237,10 @@ 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) @@ -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. diff --git a/cms/djangoapps/contentstore/views/tests/test_assets.py b/cms/djangoapps/contentstore/views/tests/test_assets.py index 2484f3d826fb..914f660f61d3 100644 --- a/cms/djangoapps/contentstore/views/tests/test_assets.py +++ b/cms/djangoapps/contentstore/views/tests/test_assets.py @@ -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')