1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- from django.contrib import admin
- from django.contrib.contenttypes.admin import GenericStackedInline
- from django.contrib.auth.models import User
- from .forms import AdherentForm
- from .models import Profile, Corporation, Adherent
- from banking.admin import PaymentInline
- class ProfileInline(admin.StackedInline):
- model = Profile
- def has_add_permission(self, request):
- return False
- def has_delete_permission(self, request, obj=None):
- return False
- 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', 'adherent_name', 'type',)
- list_filter = (AdherentTypeFilter,)
- inlines = (PaymentInline,)
- fields = ('id',)
- readonly_fields = ('id',)
- def adherent_name(self, obj):
- return str(obj)
- adherent_name.short_description = 'Nom ou raison sociale'
- def has_add_permission(self, request):
- return False
- class CorporationAdmin(admin.ModelAdmin):
- inlines = (AdherentInline,)
- admin.site.register(Corporation, CorporationAdmin)
- admin.site.register(Adherent, AdherentAdmin)
|