Skip to content

Commit

Permalink
Add admin action to download a csv with all candidate URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
code-geek committed Nov 29, 2023
1 parent 7ef4239 commit 0ec3dc2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions sde_collections/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
from .tasks import import_candidate_urls_from_api


@admin.action(description="Download candidate URLs as csv")
def download_candidate_urls_as_csv(modeladmin, request, queryset):
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename="candidate_urls.csv"'

writer = csv.writer(response)

if len(queryset) > 1:
messages.add_message(
request, messages.ERROR, "You can only export one collection at a time."
)
return

urls = CandidateURL.objects.filter(collection=queryset.first()).values_list(
"url", flat=True
)

# Write your headers here
writer.writerow(["candidate_url"])
for url in urls:
writer.writerow([url])

return response


@admin.action(description="Import metadata from Sinequa configs")
def import_sinequa_metadata(modeladmin, request, queryset):
for collection in queryset.all():
Expand Down Expand Up @@ -168,6 +193,7 @@ class CollectionAdmin(admin.ModelAdmin, ExportCsvMixin, UpdateConfigMixin):
actions = [
"export_as_csv",
"update_config",
download_candidate_urls_as_csv,
import_candidate_urls_test,
import_candidate_urls_production,
import_candidate_urls_secret_test,
Expand Down

0 comments on commit 0ec3dc2

Please sign in to comment.