From 341d998631f764325084db8b2e2bb815b0733fd9 Mon Sep 17 00:00:00 2001 From: Haresh Kainth Date: Thu, 17 Oct 2024 01:06:44 +0100 Subject: [PATCH] chore:enhance search CSV download to include multiple doc types. Updated the `download_search_csv` function to handle multiple document types for both public gateway and legislation searches. This includes collecting results from different sources and combining them before generating the CSV. --- orp/orp_search/views.py | 51 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/orp/orp_search/views.py b/orp/orp_search/views.py index 346189b..40394d6 100644 --- a/orp/orp_search/views.py +++ b/orp/orp_search/views.py @@ -76,7 +76,7 @@ def _decode_url(encoded_url): @require_http_methods(["GET"]) def download_search_csv(request: HttpRequest) -> HttpResponse: search_terms = request.GET.get("search", "") - document_type_terms = request.GET.get("document_type", "") + document_type_terms = request.GET.getlist("document_type", "") publisher_terms = request.GET.getlist("publisher", None) sort_by = request.GET.get("sort", None) @@ -90,8 +90,53 @@ def download_search_csv(request: HttpRequest) -> HttpResponse: if sort_by: config.sort_by = sort_by - public_gateway = PublicGateway() - search_results = public_gateway.search(config) + public_gateway_search_results = [] + legislation_search_results = [] + + if ( + not config.document_types + or "standard" in config.document_types + or "guidance" in config.document_types + ): + public_gateway = PublicGateway() + public_gateway_search_results = public_gateway.search(config) + + # Legislation search + # If config.search_terms is empty then we don't need to + # search for legislation + if ( + "" not in config.search_terms + and not config.document_types + or "legislation" in config.document_types + ): + legislation = Legislation() + legislation_search_results = legislation.search(config) + + search_results = [] + + for result in public_gateway_search_results: + search_results.append( + { + "id": result["id"], + "title": result["title"], + "publisher": result["publisher"], + "description": result["description"], + "type": result["type"], + "date_modified": result["date_modified"], + } + ) + + for result in legislation_search_results: + search_results.append( + { + "id": result["id"], + "title": result["title"], + "publisher": result["publisher"], + "description": "", + "type": result["type"], + "date_modified": result["date_modified"], + } + ) # Convert search_results JSON object to DataFrame # (for demonstration purposes)