wallet/apps/wallet/admin.py
2026-04-27 13:58:47 +03:30

108 lines
4.5 KiB
Python
Executable file

from django.contrib import admin
from .models import Transaction, Account, Wallet
from django.http import HttpResponse
import io
import xlsxwriter
import jdatetime
from django.utils import timezone
from rangefilter.filters import DateTimeRangeFilter
# from utils.accounts_client import get_user_detailed_info
@admin.action(description="export excel")
def export_excel(modeladmin, request, queryset):
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output)
worksheet = workbook.add_worksheet()
for col, header in enumerate(['uuid',
'application',
'amount',
'jalali_datetime',
'payer',
'payer_type',
'payee',
'payee_type',
'state',
'created_at',
'updated_at',
'details',
]):
worksheet.write(0, col, header)
for row, tranaction in enumerate(queryset, 1):
worksheet.write(row, 0, str(tranaction.uuid))
worksheet.write(row, 1, str(tranaction.application))
worksheet.write(row, 2, tranaction.amount)
worksheet.write(row, 3, str(tranaction.jalali_datetime))
worksheet.write(row, 4, str(tranaction.payer))
worksheet.write(row, 5, str(tranaction.payer_type))
worksheet.write(row, 6, str(tranaction.payee))
worksheet.write(row, 7, str(tranaction.payee_type))
worksheet.write(row, 8, str(tranaction.state))
worksheet.write(row, 9, str(tranaction.created_at))
worksheet.write(row, 10, str(tranaction.updated_at))
worksheet.write(row, 11, str(tranaction.details))
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
@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']
list_filter = ['application', 'state', ('created_at', DateTimeRangeFilter), ('updated_at', DateTimeRangeFilter),]
actions = [export_excel]
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)
admin.site.register(Wallet)