ipg/apps/pg/admin.py
2025-10-15 17:53:22 +03:30

128 lines
No EOL
4.9 KiB
Python

import io
import jdatetime
import xlsxwriter
from django.contrib import admin, messages
from django.db.models import Sum
from django.http import HttpResponse
from django.utils import timezone
from django.utils.html import format_html
from django.shortcuts import redirect
from django.urls import path
from utils.wallet_client import deposit_to_user_wallet_verify
from .constans import StateChoices
from .models import PaymentRequest, PNA, Fake
class PNAAdmin(admin.ModelAdmin):
list_display = ['pna_status_code', 'pna_status_message', 'jajali_datetime', 'payment_request']
list_filter = ['pna_status']
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
@admin.action(description="change to success")
def change_to_success(model_admin, request, queryset):
for obj in queryset:
obj.change_state(StateChoices.WALLET_SUBMIT_SUCCESS, StateChoices.SUCCESS, 'change to success by admin')
@admin.action(description="change to failed")
def change_to_failed(model_admin, request, queryset):
for obj in queryset:
obj.change_state(StateChoices.WALLET_SUBMIT_SUCCESS, StateChoices.FAILED, 'change to failed by admin')
class PaymentRequestAdmin(admin.ModelAdmin):
list_display = ['state', 'check_success_again', 'uuid', 'splited_amount', 'user_id', 'jajali_datetime', 'created_at', 'application', 'gateway']
search_fields = ['application__uuid','application__client_id', 'amount', 'user__uuid']
list_filter = ['application', 'state', 'gateway']
actions = [export_exel, change_to_success, change_to_failed]
def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}
cl = self.get_changelist_instance(request)
queryset = cl.result_list
totals = queryset.aggregate(
total_amount=Sum('amount'),
)
extra_context['totals'] = totals
return super().changelist_view(request, extra_context=extra_context)
def check_success_again(self, obj):
if obj.state == StateChoices.WALLET_SUBMIT_SUCCESS:
return format_html(
'<a class="button" href="{}">بررسی دوباره موفقیت</a>',
f'{obj.uuid}/check_success_again/'
)
return ''
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('<uuid:payment_request_uuid>/check_success_again/', self.admin_site.admin_view(self.check_success_again_view))
]
return custom_urls + urls
def check_success_again_view(self, request, payment_request_uuid):
verify_response = deposit_to_user_wallet_verify(payment_request_uuid)
if not verify_response:
self.message_user(request, "خطای استعلام", level=messages.ERROR)
else:
if not verify_response.uuid or verify_response.state != 5:
to_pay_failed = PaymentRequest.objects.filter(
pk=payment_request_uuid,
state=StateChoices.WALLET_SUBMIT_SUCCESS.value
).update(
state=StateChoices.FAILED.value,
)
else:
to_pay_success = PaymentRequest.objects.filter(
pk=payment_request_uuid,
state=StateChoices.WALLET_SUBMIT_SUCCESS.value
).update(
state=StateChoices.SUCCESS.value,
)
self.message_user(request, f"درخواست {payment_request_uuid} بررسی شد ✅")
return redirect(request.META.get("HTTP_REFERER", ".."))
class FakeAdmin(admin.ModelAdmin):
list_display = [ 'created_at']
admin.site.register(PaymentRequest, PaymentRequestAdmin)
admin.site.register(PNA, PNAAdmin)
admin.site.register(Fake, FakeAdmin)