fixes fixes fixes tenaancy
This commit is contained in:
@@ -1,12 +1,113 @@
|
||||
"""
|
||||
Admin interface for auth models
|
||||
"""
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
||||
from igny8_core.admin.base import AccountAdminMixin
|
||||
from .models import User, Account, Plan, Subscription, Site, Sector, SiteUserAccess, Industry, IndustrySector, SeedKeyword, PasswordResetToken
|
||||
|
||||
|
||||
class AccountAdminForm(forms.ModelForm):
|
||||
"""Custom form for Account admin with dynamic payment method choices from PaymentMethodConfig"""
|
||||
|
||||
class Meta:
|
||||
model = Account
|
||||
fields = '__all__'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
from igny8_core.business.billing.models import PaymentMethodConfig, AccountPaymentMethod
|
||||
|
||||
if self.instance and self.instance.pk:
|
||||
# Get country from billing_country, fallback to wildcard '*' for global
|
||||
country = self.instance.billing_country or '*'
|
||||
|
||||
# Get enabled payment methods for this country OR global (*)
|
||||
available_methods = PaymentMethodConfig.objects.filter(
|
||||
country_code__in=[country, '*'],
|
||||
is_enabled=True
|
||||
).order_by('country_code', 'sort_order').values_list('payment_method', 'display_name')
|
||||
|
||||
if available_methods:
|
||||
# Build choices from PaymentMethodConfig
|
||||
choices = []
|
||||
seen = set()
|
||||
for method_type, display_name in available_methods:
|
||||
if method_type not in seen:
|
||||
choices.append((method_type, display_name or method_type.replace('_', ' ').title()))
|
||||
seen.add(method_type)
|
||||
else:
|
||||
# Fallback to model choices if no configs
|
||||
choices = Account.PAYMENT_METHOD_CHOICES
|
||||
|
||||
self.fields['payment_method'].widget = forms.Select(choices=choices)
|
||||
|
||||
# Get current default from AccountPaymentMethod
|
||||
default_method = AccountPaymentMethod.objects.filter(
|
||||
account=self.instance,
|
||||
is_default=True,
|
||||
is_enabled=True
|
||||
).first()
|
||||
|
||||
if default_method:
|
||||
self.fields['payment_method'].initial = default_method.type
|
||||
self.fields['payment_method'].help_text = f'✓ Current: {default_method.display_name} ({default_method.get_type_display()})'
|
||||
else:
|
||||
self.fields['payment_method'].help_text = 'Select from available payment methods based on country'
|
||||
|
||||
def save(self, commit=True):
|
||||
"""When payment_method changes, update/create AccountPaymentMethod"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod, PaymentMethodConfig
|
||||
|
||||
instance = super().save(commit=False)
|
||||
|
||||
if commit:
|
||||
instance.save()
|
||||
|
||||
# Get selected payment method
|
||||
selected_type = self.cleaned_data.get('payment_method')
|
||||
|
||||
if selected_type:
|
||||
# Get config for display name and instructions
|
||||
country = instance.billing_country or '*'
|
||||
config = PaymentMethodConfig.objects.filter(
|
||||
country_code__in=[country, '*'],
|
||||
payment_method=selected_type,
|
||||
is_enabled=True
|
||||
).first()
|
||||
|
||||
# Create or update AccountPaymentMethod
|
||||
account_method, created = AccountPaymentMethod.objects.get_or_create(
|
||||
account=instance,
|
||||
type=selected_type,
|
||||
defaults={
|
||||
'display_name': config.display_name if config else selected_type.replace('_', ' ').title(),
|
||||
'is_default': True,
|
||||
'is_enabled': True,
|
||||
'instructions': config.instructions if config else '',
|
||||
'country_code': instance.billing_country or '',
|
||||
}
|
||||
)
|
||||
|
||||
if not created:
|
||||
# Update existing and set as default
|
||||
account_method.is_default = True
|
||||
account_method.is_enabled = True
|
||||
if config:
|
||||
account_method.display_name = config.display_name
|
||||
account_method.instructions = config.instructions
|
||||
account_method.save()
|
||||
|
||||
# Unset other methods as default
|
||||
AccountPaymentMethod.objects.filter(
|
||||
account=instance
|
||||
).exclude(id=account_method.id).update(is_default=False)
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
@admin.register(Plan)
|
||||
class PlanAdmin(admin.ModelAdmin):
|
||||
"""Plan admin - Global, no account filtering needed"""
|
||||
@@ -33,6 +134,7 @@ class PlanAdmin(admin.ModelAdmin):
|
||||
|
||||
@admin.register(Account)
|
||||
class AccountAdmin(AccountAdminMixin, admin.ModelAdmin):
|
||||
form = AccountAdminForm
|
||||
list_display = ['name', 'slug', 'owner', 'plan', 'status', 'credits', 'created_at']
|
||||
list_filter = ['status', 'plan']
|
||||
search_fields = ['name', 'slug']
|
||||
|
||||
Reference in New Issue
Block a user