FEATURE(wallet): add export excel to panel admin

add export excel to panel admin
This commit is contained in:
Haydar Ghasemi 2026-04-27 13:58:47 +03:30
parent 5d897b0917
commit 8328814dca

View file

@ -57,6 +57,40 @@ def export_excel(modeladmin, request, queryset):
# return the response # return the response
return 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): class TransactionAdmin(admin.ModelAdmin):
list_display = ['uuid', 'application', 'splited_amount', 'jalali_datetime', 'payer', 'payer_type', 'payee', 'payee_type', 'state', 'amount', 'created_at', 'updated_at'] 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'] 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'] list_display = ['uuid', 'owner_uuid', 'wallet', 'owner_type', 'balance', 'get_name']
search_fields = ['uuid', 'owner_uuid'] search_fields = ['uuid', 'owner_uuid']
list_filter = ['wallet__label', 'owner_type'] list_filter = ['wallet__label', 'owner_type']
actions = [export_accounts_as_excel]
admin.site.register(Transaction, TransactionAdmin) admin.site.register(Transaction, TransactionAdmin)
admin.site.register(Account, AccountAdmin) admin.site.register(Account, AccountAdmin)