Skip to content

Commit

Permalink
feat:Add CSV download feature for search results
Browse files Browse the repository at this point in the history
Introduced a new view `download_search_csv` to generate search results as a CSV file. Updated the template to dynamically set the CSV download link with current query parameters. Adjusted the URL configuration to include the route for CSV download.
  • Loading branch information
hareshkainthdbt committed Oct 14, 2024
1 parent 77801a6 commit 31dc044
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 5 additions & 0 deletions orp/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
path("", orp_search_views.search, name="search"),
# If we choose to have a start page with green button, this is it:
# path("", core_views.home, name="home"),
path(
"download_csv/",
orp_search_views.download_search_csv,
name="download_csv",
),
path("details/<str:id>", orp_search_views.details, name="details"),
path("healthcheck/", core_views.health_check, name="healthcheck"),
path(
Expand Down
16 changes: 15 additions & 1 deletion orp/orp_search/templates/orp.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,23 @@ <h2 class="govuk-fieldset__heading">
</button>
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible">
<p class="govuk-body">
<a href="#" class="govuk-link govuk-link--no-visited-state govuk-!-float-right">Download search results as CSV
<a id="download-csv-link" href="#" class="govuk-link govuk-link--no-visited-state govuk-!-float-right">Download search results as CSV
file</a>
</p>

<script>
// Function to get the current query parameters from the address bar
function getQueryParameters() {
return window.location.search;
}

document.addEventListener('DOMContentLoaded', function() {
var csvLink = document.getElementById('download-csv-link');
var queryParams = getQueryParameters();
var baseURL = '{% url "download_csv" %}';
csvLink.href = baseURL + queryParams;

Check warning

Code scanning / CodeQL

Client-side URL redirect Medium

Untrusted URL redirection depends on a
user-provided value
.
});
</script>
</form>
</div>

Expand Down
43 changes: 42 additions & 1 deletion orp/orp_search/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import csv
import logging

import pandas as pd

from orp_search.public_gateway import PublicGateway, SearchDocumentConfig

from django.conf import settings
Expand Down Expand Up @@ -52,6 +55,45 @@ def details(request: HttpRequest, id) -> HttpResponse:
return render(request, template_name="details.html", context=context)


@require_http_methods(["GET"])
def download_search_csv(request: HttpRequest) -> HttpResponse:
search_terms = request.GET.get("query", "")
document_type_terms = request.GET.get("document_type", "")
publisher_terms = request.GET.getlist("publisher", None)
sort_by = request.GET.get("sort", None)

config = SearchDocumentConfig(
search_terms, document_type_terms, dummy=True
)

if publisher_terms:
config.publisher_terms = publisher_terms

if sort_by:
config.sort_by = sort_by

public_gateway = PublicGateway()
search_results = public_gateway.search(config)

# Convert search_results JSON object to DataFrame
# (for demonstration purposes)
search_results_df = pd.DataFrame(search_results)

response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = (
'attachment; filename="search_results.csv"'
)

# Write the DataFrame to the response
writer = csv.writer(response)
writer.writerow(search_results_df.columns) # Write the header

for _, row in search_results_df.iterrows():
writer.writerow(row)

return response


@require_http_methods(["GET"])
def search(request: HttpRequest) -> HttpResponse:
"""Search view.
Expand Down Expand Up @@ -117,7 +159,6 @@ def search(request: HttpRequest) -> HttpResponse:
if sort_by:
config.sort_by = sort_by

# Check if the response is cached
public_gateway = PublicGateway()
search_results = public_gateway.search(config)
context["results_count_total"] = len(search_results)
Expand Down

0 comments on commit 31dc044

Please sign in to comment.