Skip to content

Commit

Permalink
Add export for donations
Browse files Browse the repository at this point in the history
  • Loading branch information
tkw1536 committed Sep 19, 2023
1 parent 4b911db commit 8e6e371
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
26 changes: 19 additions & 7 deletions alumni/admin/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any, List, Iterator, Optional, Callable
from typing import Any, List, Iterator, Optional, Callable, Tuple
from django.contrib.admin import ModelAdmin
from django.http import HttpRequest
from django.db.models import QuerySet
Expand Down Expand Up @@ -94,6 +94,7 @@ def to_excel(value: Any) -> ExcelCellType:
def export_as_xslx_action(
description: str = "Export selected objects as XSLX file",
fields: Optional[Iterator[str]] = None,
extra_fields: Optional[Iterator[Tuple[str, Callable]]] = None,
header: bool = True,
) -> Callable[[ModelAdmin, HttpRequest, QuerySet], HttpResponse]:
"""
Expand All @@ -111,6 +112,12 @@ def export_as_xslx(
else:
field_names = fields

# get the extra names
if not extra_fields:
extra_names = []
else:
extra_names = [n for (n, _) in extra_fields]

# Create a response header
response = HttpResponse(
content_type="application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Expand All @@ -126,24 +133,29 @@ def export_as_xslx(

# Write the header (if desired)
if header:

def makeHeaderCell(field):
c = cell.Cell(ws, value=field)
c.font = styles.Font(bold=True)
return c

ws.append([makeHeaderCell(field) for field in field_names])
ws.append([makeHeaderCell(field) for field in field_names] + extra_names)

# Write each of the rows
for row in queryset.values_list(*field_names):

copy = queryset.all()
for (raw, row) in zip(copy, queryset.values_list(*field_names)):
def makeCell(prop):
try:
return to_excel(prop)
except:
return str(prop)

ws.append([makeCell(c) for c in row])

cells = [makeCell(c) for c in row]
if extra_fields:
extra_cells = [makeCell(f(raw)) for (_, f) in extra_fields]
else:
extra_cells = []

ws.append(cells+extra_cells)

# adjust column widths
# adapted from https://stackoverflow.com/a/39530676
Expand Down
6 changes: 5 additions & 1 deletion donations/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
from donations.models import DonationTarget, Donation

from alumni.admin.actions import export_as_xslx_action

def deactivate(modeladmin, request, queryset):
queryset.update(active=False)
Expand All @@ -15,6 +15,10 @@ class DonationAdmin(admin.ModelAdmin):
list_filter = ("target", "completed")
date_hierarchy = "completed"

actions = [
export_as_xslx_action("Export as XSLX", fields=["completed", "target"], extra_fields=[("amount", lambda x:x.amount)]),
]


class DonationTargetAdmin(admin.ModelAdmin):
list_display = ("label", "active")
Expand Down

0 comments on commit 8e6e371

Please sign in to comment.