From 8328814dca604dbfc1bd9f1e2ff19db38463ae0a Mon Sep 17 00:00:00 2001 From: Haydar Ghasemi Date: Mon, 27 Apr 2026 13:58:47 +0330 Subject: [PATCH] FEATURE(wallet): add export excel to panel admin add export excel to panel admin --- apps/wallet/admin.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/apps/wallet/admin.py b/apps/wallet/admin.py index 3f34770..1d73557 100755 --- a/apps/wallet/admin.py +++ b/apps/wallet/admin.py @@ -57,6 +57,40 @@ def export_excel(modeladmin, request, queryset): # return the response return response +@admin.action(description="export excel") +def export_accounts_as_excel(modeladmin, request, queryset): + output = io.BytesIO() + workbook = xlsxwriter.Workbook(output) + worksheet = workbook.add_worksheet() + for col, header in enumerate(['uuid', + 'owner_uuid', + 'owner_type', + 'wallet', + 'balance', + ]): + worksheet.write(0, col, header) + + for row, account in enumerate(queryset, 1): + worksheet.write(row, 0, str(account.uuid)) + worksheet.write(row, 1, str(account.owner_uuid)) + worksheet.write(row, 2, account.owner_type) + worksheet.write(row, 3, str(account.wallet)) + worksheet.write(row, 4, str(account.balance)) + workbook.close() + + # create a response + response = HttpResponse(content_type='application/vnd.ms-excel') + + # tell the browser what the file is named + file_name = jdatetime.date.fromgregorian(date=timezone.now(), locale='fa_IR').strftime("%Y/%m/%d") + response['Content-Disposition'] = f'attachment;filename="{file_name}.xlsx"' + + # put the spreadsheet data into the response + response.write(output.getvalue()) + + # return the response + return response + class TransactionAdmin(admin.ModelAdmin): list_display = ['uuid', 'application', 'splited_amount', 'jalali_datetime', 'payer', 'payer_type', 'payee', 'payee_type', 'state', 'amount', 'created_at', 'updated_at'] search_fields = ['uuid', 'created_at', 'updated_at', 'payer_account__owner_uuid', 'payee_account__owner_uuid', 'application__uuid'] @@ -67,6 +101,7 @@ class AccountAdmin(admin.ModelAdmin): list_display = ['uuid', 'owner_uuid', 'wallet', 'owner_type', 'balance', 'get_name'] search_fields = ['uuid', 'owner_uuid'] list_filter = ['wallet__label', 'owner_type'] + actions = [export_accounts_as_excel] admin.site.register(Transaction, TransactionAdmin) admin.site.register(Account, AccountAdmin)