admin.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 PaymentInline, 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', 'get_name', 'type',)
  36. list_filter = (AdherentTypeFilter,)
  37. fields = ('id',)
  38. readonly_fields = ('id',)
  39. def get_form(self, request, obj=None, **kwargs):
  40. # get_inlines does not exists :-(
  41. if request.user.has_perm('banking.validate_payment'):
  42. self.inlines = (PaymentInline,)
  43. else:
  44. self.inlines = (ValidatedPaymentInline, PendingOrNewPaymentInline,)
  45. return super().get_form(request, obj, **kwargs)
  46. def has_add_permission(self, request):
  47. return False
  48. class CorporationAdmin(admin.ModelAdmin):
  49. list_display = ('social_reason', 'adherent_id')
  50. inlines = (AdherentInline,)
  51. def adherent_id(self, corporation):
  52. adherent = corporation.adherent
  53. if adherent:
  54. return adherent.id
  55. adherent_id.short_description = 'Numéro d’adhérent'
  56. admin.site.register(Corporation, CorporationAdmin)
  57. admin.site.register(Adherent, AdherentAdmin)