admin.py 1.8 KB

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