admin.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from django.contrib import admin
  2. from django.contrib.contenttypes.admin import GenericStackedInline
  3. from django.contrib.auth.models import User
  4. from .forms import AdherentForm
  5. from .models import Profile, Corporation, Adherent
  6. from banking.admin import ValidatedPaymentInline, PendingOrNewPaymentInline
  7. class ProfileInline(admin.StackedInline):
  8. model = Profile
  9. def has_add_permission(self, request):
  10. return False
  11. def has_delete_permission(self, request, obj=None):
  12. return False
  13. class AdherentInline(GenericStackedInline):
  14. model = Adherent
  15. ct_field = 'adherent_type'
  16. ct_fk_field = 'adherent_id'
  17. form = AdherentForm
  18. max_num = 1
  19. extra = 0
  20. class AdherentTypeFilter(admin.SimpleListFilter):
  21. title = 'type d’adhérent'
  22. parameter_name = 'type'
  23. def lookups(self, request, model_admin):
  24. return (
  25. ('physique', 'Personne physique'),
  26. ('morale', 'Personne morale'),
  27. )
  28. def queryset(self, request, queryset):
  29. if self.value() == 'physique':
  30. return queryset.filter(adherent_type__app_label='auth',
  31. adherent_type__model='user')
  32. if self.value() == 'morale':
  33. return queryset.filter(adherent_type__app_label='adhesions',
  34. adherent_type__model='corporation')
  35. class AdherentAdmin(admin.ModelAdmin):
  36. list_display = ('id', 'adherent_name', 'type',)
  37. list_filter = (AdherentTypeFilter,)
  38. inlines = (ValidatedPaymentInline, PendingOrNewPaymentInline,)
  39. fields = ('id',)
  40. readonly_fields = ('id',)
  41. def adherent_name(self, obj):
  42. return str(obj)
  43. adherent_name.short_description = 'Nom ou raison sociale'
  44. def has_add_permission(self, request):
  45. return False
  46. class CorporationAdmin(admin.ModelAdmin):
  47. inlines = (AdherentInline,)
  48. admin.site.register(Corporation, CorporationAdmin)
  49. admin.site.register(Adherent, AdherentAdmin)