bulk actions & some next audits docs
This commit is contained in:
@@ -5,6 +5,7 @@ NOTE: Most billing models are registered in modules/billing/admin.py
|
||||
with full workflow functionality. This file contains legacy/minimal registrations.
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.contrib import messages
|
||||
from django.utils.html import format_html
|
||||
from unfold.admin import ModelAdmin
|
||||
from igny8_core.admin.base import AccountAdminMixin, Igny8ModelAdmin
|
||||
@@ -49,8 +50,22 @@ from .models import (
|
||||
# PaymentMethodConfig and AccountPaymentMethod are kept here as they're not duplicated
|
||||
# or have minimal implementations that don't conflict
|
||||
|
||||
from import_export.admin import ExportMixin
|
||||
from import_export import resources
|
||||
|
||||
|
||||
class AccountPaymentMethodResource(resources.ModelResource):
|
||||
"""Resource class for exporting Account Payment Methods"""
|
||||
class Meta:
|
||||
model = AccountPaymentMethod
|
||||
fields = ('id', 'display_name', 'type', 'account__name', 'is_default',
|
||||
'is_enabled', 'is_verified', 'country_code', 'created_at')
|
||||
export_order = fields
|
||||
|
||||
|
||||
@admin.register(AccountPaymentMethod)
|
||||
class AccountPaymentMethodAdmin(Igny8ModelAdmin):
|
||||
class AccountPaymentMethodAdmin(ExportMixin, Igny8ModelAdmin):
|
||||
resource_class = AccountPaymentMethodResource
|
||||
list_display = [
|
||||
'display_name',
|
||||
'type',
|
||||
@@ -64,6 +79,12 @@ class AccountPaymentMethodAdmin(Igny8ModelAdmin):
|
||||
list_filter = ['type', 'is_default', 'is_enabled', 'is_verified', 'country_code']
|
||||
search_fields = ['display_name', 'account__name', 'account__id']
|
||||
readonly_fields = ['created_at', 'updated_at']
|
||||
actions = [
|
||||
'bulk_enable',
|
||||
'bulk_disable',
|
||||
'bulk_set_default',
|
||||
'bulk_delete_methods',
|
||||
]
|
||||
fieldsets = (
|
||||
('Payment Method', {
|
||||
'fields': ('account', 'type', 'display_name', 'is_default', 'is_enabled', 'is_verified', 'country_code')
|
||||
@@ -75,4 +96,48 @@ class AccountPaymentMethodAdmin(Igny8ModelAdmin):
|
||||
'fields': ('created_at', 'updated_at'),
|
||||
'classes': ('collapse',)
|
||||
}),
|
||||
)
|
||||
)
|
||||
def bulk_enable(self, request, queryset):
|
||||
updated = queryset.update(is_enabled=True)
|
||||
self.message_user(request, f'{updated} payment method(s) enabled.', messages.SUCCESS)
|
||||
bulk_enable.short_description = 'Enable selected payment methods'
|
||||
|
||||
def bulk_disable(self, request, queryset):
|
||||
updated = queryset.update(is_enabled=False)
|
||||
self.message_user(request, f'{updated} payment method(s) disabled.', messages.SUCCESS)
|
||||
bulk_disable.short_description = 'Disable selected payment methods'
|
||||
|
||||
def bulk_set_default(self, request, queryset):
|
||||
from django import forms
|
||||
|
||||
if 'apply' in request.POST:
|
||||
method_id = request.POST.get('payment_method')
|
||||
if method_id:
|
||||
method = AccountPaymentMethod.objects.get(pk=method_id)
|
||||
# Unset all others for this account
|
||||
AccountPaymentMethod.objects.filter(account=method.account).update(is_default=False)
|
||||
method.is_default = True
|
||||
method.save()
|
||||
self.message_user(request, f'{method.display_name} set as default for {method.account.name}.', messages.SUCCESS)
|
||||
return
|
||||
|
||||
class PaymentMethodForm(forms.Form):
|
||||
payment_method = forms.ModelChoiceField(
|
||||
queryset=queryset,
|
||||
label="Select Payment Method to Set as Default"
|
||||
)
|
||||
|
||||
from django.shortcuts import render
|
||||
return render(request, 'admin/bulk_action_form.html', {
|
||||
'title': 'Set Default Payment Method',
|
||||
'queryset': queryset,
|
||||
'form': PaymentMethodForm(),
|
||||
'action': 'bulk_set_default',
|
||||
})
|
||||
bulk_set_default.short_description = 'Set as default'
|
||||
|
||||
def bulk_delete_methods(self, request, queryset):
|
||||
count = queryset.count()
|
||||
queryset.delete()
|
||||
self.message_user(request, f'{count} payment method(s) deleted.', messages.SUCCESS)
|
||||
bulk_delete_methods.short_description = 'Delete selected payment methods'
|
||||
Reference in New Issue
Block a user