from django.contrib import admin from django.contrib.contenttypes.admin import GenericStackedInline from .forms import AdherentForm from .models import Corporation, Adherent from banking.admin import PaymentInline, ValidatedPaymentInline, PendingOrNewPaymentInline class AdherentInline(GenericStackedInline): model = Adherent ct_field = 'adherent_type' ct_fk_field = 'adherent_id' form = AdherentForm max_num = 1 extra = 0 class AdherentTypeFilter(admin.SimpleListFilter): title = 'type d’adhérent' parameter_name = 'type' def lookups(self, request, model_admin): return ( ('physique', 'Personne physique'), ('morale', 'Personne morale'), ) def queryset(self, request, queryset): if self.value() == 'physique': return queryset.filter(adherent_type__app_label='auth', adherent_type__model='user') if self.value() == 'morale': return queryset.filter(adherent_type__app_label='adhesions', adherent_type__model='corporation') class AdherentAdmin(admin.ModelAdmin): list_display = ('id', 'get_name', 'type',) list_filter = (AdherentTypeFilter,) fields = ('id',) readonly_fields = ('id',) def get_form(self, request, obj=None, **kwargs): # get_inlines does not exists :-( if request.user.has_perm('banking.validate_payment'): self.inlines = (PaymentInline,) else: self.inlines = (ValidatedPaymentInline, PendingOrNewPaymentInline,) return super().get_form(request, obj, **kwargs) def has_add_permission(self, request): return False class CorporationAdmin(admin.ModelAdmin): list_display = ('social_reason', 'adherent_id') inlines = (AdherentInline,) def adherent_id(self, corporation): adherent = corporation.adherent if adherent: return adherent.id adherent_id.short_description = 'Numéro d’adhérent' admin.site.register(Corporation, CorporationAdmin) admin.site.register(Adherent, AdherentAdmin)