FIX(wallet): export excel
export excel
This commit is contained in:
parent
7b1e9376b4
commit
2d8e0da6fa
3 changed files with 82 additions and 4 deletions
|
|
@ -1,10 +1,66 @@
|
|||
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 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
|
||||
|
||||
class TransactionAdmin(admin.ModelAdmin):
|
||||
list_display = ['uuid', 'amount', 'created_at', 'updated_at', 'payer', 'payer_type', 'payee', 'payee_type', 'state', 'application']
|
||||
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 = ['state']
|
||||
list_filter = ['application', 'state']
|
||||
actions = [export_excel]
|
||||
|
||||
class AccountAdmin(admin.ModelAdmin):
|
||||
list_display = ['uuid', 'owner_uuid', 'wallet', 'owner_type', 'balance', 'get_name']
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ from django.db.models import F
|
|||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from collections import defaultdict
|
||||
from django.db.models import Count, Sum
|
||||
import jdatetime
|
||||
|
||||
from rest_framework.exceptions import APIException
|
||||
from django.conf import settings
|
||||
|
|
@ -182,6 +181,27 @@ class Transaction(BaseModel):
|
|||
|
||||
# unique_together = ('application', 'detail_id',)
|
||||
|
||||
@property
|
||||
def jalali_datetime(self):
|
||||
def to_jalali(value):
|
||||
if value:
|
||||
return jdatetime.datetime.fromgregorian(date=value, locale='fa_IR').strftime("%Y/%m/%d-%H:%M:%S")
|
||||
else:
|
||||
return value
|
||||
|
||||
return to_jalali(self.created_at)
|
||||
|
||||
@property
|
||||
def splited_amount(self):
|
||||
def amount_split_3digit(value):
|
||||
text = str(value)[::-1]
|
||||
return ','.join([l[::-1] for l in [text[i:i + 3] for i in range(0, len(text), 3)][::-1]])
|
||||
|
||||
if self.amount:
|
||||
amount_str = amount_split_3digit(str(self.amount))
|
||||
return amount_str
|
||||
return
|
||||
|
||||
def get_payer(self):
|
||||
return self.payer_account.get_owner()
|
||||
|
||||
|
|
|
|||
|
|
@ -179,3 +179,5 @@ zope-event==6.1
|
|||
# via gevent
|
||||
zope-interface==8.2
|
||||
# via gevent
|
||||
xlsxwriter==3.2.3
|
||||
# via -r requirements.in
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue