From 57286709b1f671634778996d8de2cd04102be7db Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Mon, 13 Nov 2023 06:56:00 +0300 Subject: [PATCH] Extract build_query to clean up query_media Signed-off-by: Olga Bulat --- api/api/controllers/search_controller.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api/api/controllers/search_controller.py b/api/api/controllers/search_controller.py index 4c5b2015106..fe32b5754f6 100644 --- a/api/api/controllers/search_controller.py +++ b/api/api/controllers/search_controller.py @@ -352,6 +352,16 @@ def build_collection_query( return Q("bool", **search_query) +def build_query( + strategy: SearchStrategy, + search_params: MediaListRequestSerializer, + collection_params: dict[str, str] | None, +) -> Q: + if strategy == "collection": + return build_collection_query(search_params, collection_params) + return build_search_query(search_params) + + def query_media( strategy: SearchStrategy, search_params: MediaListRequestSerializer, @@ -388,10 +398,7 @@ def query_media( """ index = get_index(exact_index, origin_index, search_params) - if strategy == "collection": - query = build_collection_query(search_params, collection_params) - else: - query = build_search_query(search_params) + query = build_query(strategy, search_params, collection_params) s = Search(index=index).query(query) @@ -409,8 +416,8 @@ def query_media( # Sort by `created_on` if the parameter is set or if `strategy` is `collection`. sort_by = search_params.validated_data.get("sort_by") - sort_dir = search_params.validated_data.get("sort_dir", "desc") if strategy == "collection" or sort_by == INDEXED_ON: + sort_dir = search_params.validated_data.get("sort_dir", "desc") s = s.sort({"created_on": {"order": sort_dir}}) # Execute paginated search and tally results