admin.py 2.1 KB

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