sum in admin list view

This commit is contained in:
Sayyid Hamid Mahdavi 2025-08-12 17:24:37 +03:30
parent 9462e25de2
commit 91fed41036
3 changed files with 50 additions and 1 deletions

View file

@ -1,5 +1,11 @@
import io
import jdatetime
import xlsxwriter
from django.contrib import admin from django.contrib import admin
from django.db.models import Sum from django.db.models import Sum
from django.http import HttpResponse
from django.utils import timezone
from .models import PaymentRequest, PNA, Fake from .models import PaymentRequest, PNA, Fake
@ -10,10 +16,47 @@ class PNAAdmin(admin.ModelAdmin):
search_fields = ['pna_status_code', 'pna_status_message'] search_fields = ['pna_status_code', 'pna_status_message']
@admin.action(description="export exel")
def export_exel(modeladmin, request, queryset):
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output, options={"remove_timezone": True})
worksheet = workbook.add_worksheet()
for col, header in enumerate([]):
worksheet.write(0, col, header)
for row, payment_request in enumerate(queryset, 1):
worksheet.write(row, 0, payment_request.amount)
worksheet.write(row, 1, str(payment_request.pk))
worksheet.write(row, 2, str(payment_request.user.pk))
worksheet.write(row, 3, payment_request.jajali_datetime)
worksheet.write(row, 4, payment_request.gateway)
worksheet.write(row, 5, payment_request.get_state_display())
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 PaymentRequestAdmin(admin.ModelAdmin): class PaymentRequestAdmin(admin.ModelAdmin):
list_display = ['state', 'uuid', 'splited_amount', 'user_id', 'jajali_datetime', 'application', 'gateway'] list_display = ['state', 'uuid', 'splited_amount', 'user_id', 'jajali_datetime', 'application', 'gateway']
search_fields = ['application__uuid','application__client_id', 'amount', 'user__uuid'] search_fields = ['application__uuid','application__client_id', 'amount', 'user__uuid']
list_filter = ['application', 'state', 'gateway'] list_filter = ['application', 'state', 'gateway']
actions = [export_exel]
def changelist_view(self, request, extra_context=None): def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {} extra_context = extra_context or {}

View file

@ -16,4 +16,6 @@ django-oauth-toolkit
redis redis
celery celery
httpx httpx
django-redis django-redis
xlsxwriter
xlrd

View file

@ -175,6 +175,10 @@ vine==5.1.0
# kombu # kombu
wcwidth==0.2.13 wcwidth==0.2.13
# via prompt-toolkit # via prompt-toolkit
xlrd==2.0.2
# via -r requirements.in
xlsxwriter==3.2.5
# via -r requirements.in
zope-event==5.0 zope-event==5.0
# via gevent # via gevent
zope-interface==7.2 zope-interface==7.2