Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@
ADMINS = [("Mitchell Kotler", "mitch@muckrock.com")]
# https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS
# Chunk size for CSV exports using .iterator() to process large querysets
# without loading all records into memory at once
CSV_EXPORT_CHUNK_SIZE = env.int("CSV_EXPORT_CHUNK_SIZE", default=2000)

# LOGGING
# ------------------------------------------------------------------------------
Expand Down
23 changes: 22 additions & 1 deletion documentcloud/addons/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Django
from django.conf import settings
from django.contrib import admin, messages
from django.db.models import JSONField
from django.forms import widgets
Expand Down Expand Up @@ -128,7 +129,10 @@ def export_runs_as_csv(self, request, queryset):
"""Export selected Add-On Runs to CSV."""
field_names = [
"addon_id",
"addon_name",
"user_id",
"user_name",
"user_email",
"run_id",
"status",
"rating",
Expand All @@ -143,11 +147,28 @@ def export_runs_as_csv(self, request, queryset):
writer = csv.writer(response)
writer.writerow(field_names)

for run in queryset:
limited_queryset = queryset.select_related("addon", "user").only(
"addon_id",
"user_id",
"run_id",
"status",
"rating",
"credits_spent",
"created_at",
"updated_at",
"addon__name",
"user__name",
"user__email",
)

for run in limited_queryset.iterator(chunk_size=settings.CSV_EXPORT_CHUNK_SIZE):
writer.writerow(
[
run.addon_id,
run.addon.name,
run.user_id,
run.user.name,
run.user.email,
run.run_id,
run.status,
run.rating,
Expand Down
3 changes: 2 additions & 1 deletion documentcloud/organizations/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Django
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse

Expand Down Expand Up @@ -41,7 +42,7 @@ def export_ai_credit_logs(self, request, queryset):
writer = csv.writer(response)
writer.writerow(field_names)

for log in queryset:
for log in queryset.iterator(chunk_size=settings.CSV_EXPORT_CHUNK_SIZE):
writer.writerow(
[
str(log.organization),
Expand Down
3 changes: 2 additions & 1 deletion documentcloud/statistics/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Django
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse

Expand Down Expand Up @@ -29,7 +30,7 @@ def export_statistics_as_csv(self, request, queryset):
writer = csv.writer(response)
writer.writerow(field_names)

for obj in queryset:
for obj in queryset.iterator(chunk_size=settings.CSV_EXPORT_CHUNK_SIZE):
row = []
for field_name in field_names:
value = getattr(obj, field_name)
Expand Down
3 changes: 2 additions & 1 deletion documentcloud/users/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Django
from django.conf import settings
from django.contrib import admin
from django.http.response import HttpResponse
from django.urls.conf import re_path
Expand Down Expand Up @@ -47,7 +48,7 @@ def format_date(date):
writer.writerow(["username", "name", "email", "last_login", "date_joined"])
for user in User.objects.only(
"username", "name", "email", "last_login", "created_at"
).iterator(chunk_size=2000):
).iterator(chunk_size=settings.CSV_EXPORT_CHUNK_SIZE):
writer.writerow(
[
user.username,
Expand Down