Skip to content

Commit

Permalink
126891 Add excel export to cases admin view
Browse files Browse the repository at this point in the history
  • Loading branch information
remyvdwereld committed Sep 27, 2024
1 parent 9ddf11d commit 3062c5b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
45 changes: 42 additions & 3 deletions app/apps/cases/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.db.models import Exists, OuterRef
from django.http import HttpResponse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from openpyxl import Workbook


class LabelThemeModelChoiceField(forms.ModelChoiceField):
Expand Down Expand Up @@ -101,6 +103,45 @@ def remove_advertisement_linklist_items(modeladmin, request, queryset):
citizen_report.save()


def export_queryset_to_excel(modeladmin, request, queryset):
wb = Workbook()
ws = wb.active
ws.title = "Zaken"
headers = ["Zaak ID", "Address", "Thema", "Aanleiding", "Startdatum", "Einddatum"]
ws.append(headers)

for case in queryset:
# Format the dates in 'DD-MM-YYYY' format
start_date_formatted = (
case.start_date.strftime("%d-%m-%Y") if case.start_date else ""
)
end_date_formatted = case.end_date.strftime("%d-%m-%Y") if case.end_date else ""

ws.append(
[
case.id,
case.address.full_address,
case.theme.name,
case.reason.name,
start_date_formatted,
end_date_formatted,
]
)

# Create an HTTP response with the Excel file
response = HttpResponse(
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
response["Content-Disposition"] = "attachment; filename=aza_cases_export.xlsx"

wb.save(response)
return response


# Short description for the action, which will be displayed in the admin action dropdown
export_queryset_to_excel.short_description = "Export Cases to Excel"


@admin.register(CaseDocument)
class CaseDocumentAdmin(admin.ModelAdmin):
list_display = (
Expand Down Expand Up @@ -179,9 +220,7 @@ class CaseAdmin(admin.ModelAdmin):
"address__street_name",
"address__postal_code",
)
actions = [
create_main_worflow_for_case,
]
actions = [create_main_worflow_for_case, export_queryset_to_excel]


@admin.register(CaseTheme)
Expand Down
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jsonschema==4.4.0
kombu<6.0
model-bakery==1.18.1
mozilla-django-oidc==4.0.1
openpyxl
packaging
prompt-toolkit==3.0.19
psycopg2==2.9.1
Expand Down

0 comments on commit 3062c5b

Please sign in to comment.