admin.py 2.1 KB

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