Skip to content

Commit

Permalink
search: export results in csv
Browse files Browse the repository at this point in the history
Signed-off-by: pamfilos <[email protected]>
  • Loading branch information
pamfilos committed Sep 19, 2024
1 parent 48ce75f commit d6929c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
17 changes: 17 additions & 0 deletions scoap3/articles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ def __init__(self, *args, **kwargs):
},
}

def get_queryset(self):
get_all = self.request.query_params.get("all", "false").lower() == "true"
search = super().get_queryset()

if get_all and self.request.user.is_staff:
search = search.extra(size=10000)

return search

def list(self, request, *args, **kwargs):
get_all = request.query_params.get("all", "false").lower() == "true"

if get_all and self.request.user.is_staff:
self.pagination_class = None

return super().list(request, *args, **kwargs)

def get_serializer_class(self):
requested_renderer_format = self.request.accepted_media_type
if "text/csv" in requested_renderer_format:
Expand Down
5 changes: 4 additions & 1 deletion scoap3/utils/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ class ArticleCSVRenderer(CSVRenderer):
}

def render(self, data, accepted_media_type=None, renderer_context=None):
modified_data = data.get("results", [])
if "results" in data:
modified_data = data.get("results", [])
else:
modified_data = data
return super().render(modified_data, accepted_media_type, renderer_context)
16 changes: 14 additions & 2 deletions ui/src/components/search/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Result } from "@/types";
import ResultItem from "./ResultItem";
import SearchPagination from "./SearchPagination";
import { useRouter } from "next/navigation";
import { getSearchUrl } from "@/utils/utils";
import { getSearchUrl, getApiUrl } from "@/utils/utils";

interface SearchResultsProps {
results: Result[];
Expand Down Expand Up @@ -42,10 +42,22 @@ const SearchResults: React.FC<SearchResultsProps> = ({
);
};

const downloadUrl = () => {
let exportSearchParams = getSearchUrl({
...params,
all: true,
format: "csv"
})
const export_search_url = getApiUrl()+"/"+exportSearchParams;
return export_search_url;
}

return (
<div>
<div className="mt-4 mb-6 flex justify-center md:justify-between items-center flex-col md:flex-row">
<p className="flex items-center md:mb-0 mb-3">Found {count} results.</p>
<p className="flex items-center md:mb-0 mb-3">
<a href={downloadUrl()} download="scoap3-search-export" >Found {count} results.</a>
</p>
<SearchPagination count={count} params={params} />
<div className="sort flex items-center">
{count > 0 && (
Expand Down

0 comments on commit d6929c1

Please sign in to comment.