admin.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from django.contrib import admin
  2. from django.contrib.auth.models import User, Group
  3. from django.contrib.contenttypes.admin import GenericStackedInline, GenericTabularInline
  4. from django.db.models import Q
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
  7. from django.core.urlresolvers import reverse
  8. from django.utils.html import format_html
  9. from .forms import AdhesionForm
  10. from .models import ProxyUser, Corporation, Adhesion
  11. from accounts.models import Profile
  12. from banking.admin import PaymentInline
  13. class AdhesionInline(GenericTabularInline):
  14. model = Adhesion
  15. ct_field = 'adherent_type'
  16. ct_fk_field = 'adherent_id'
  17. form = AdhesionForm
  18. min_num = 1
  19. max_num = 1
  20. extra = 0
  21. def has_delete_permission(self, request, obj=None):
  22. return False
  23. class ProfileInlineDjango(admin.StackedInline):
  24. model = Profile
  25. def has_add_permission(self, request):
  26. return False
  27. def has_delete_permission(self, request, obj=None):
  28. return False
  29. class UserIsAdherentFilter(admin.SimpleListFilter):
  30. title = 'status adhérent'
  31. parameter_name = 'adherent'
  32. def lookups(self, request, model_admin):
  33. return (
  34. ('1', 'Oui'),
  35. ('0', 'Non'),
  36. )
  37. def queryset(self, request, queryset):
  38. adhesion_list = Adhesion.objects.values_list('adherent_id')
  39. if self.value() == '0':
  40. return queryset.exclude(pk__in=adhesion_list)
  41. if self.value() == '1':
  42. return queryset.filter(pk__in=adhesion_list)
  43. class UserAdmin(AuthUserAdmin):
  44. list_display = AuthUserAdmin.list_display + ('adherent_id',)
  45. list_filter = (AuthUserAdmin.list_filter[2], UserIsAdherentFilter) # [2] = actif
  46. inlines = (ProfileInlineDjango, AdhesionInline,)
  47. def adherent_id(self, user):
  48. adherent = user.profile.adhesion
  49. if adherent:
  50. return adherent.id
  51. adherent_id.short_description = 'Numéro d’adhérent'
  52. def get_fieldsets(self, request, obj=None):
  53. return (
  54. AuthUserAdmin.fieldsets[0],
  55. AuthUserAdmin.fieldsets[1],
  56. (AuthUserAdmin.fieldsets[2][0], {
  57. 'classes': ('collapse',),
  58. 'fields': ('is_active', 'is_staff', 'is_superuser',), # removing groups and user_permissions
  59. }),
  60. (AuthUserAdmin.fieldsets[3][0], {
  61. 'classes': ('collapse',),
  62. 'fields': AuthUserAdmin.fieldsets[3][1]['fields'],
  63. }),
  64. )
  65. def get_actions(self, request):
  66. actions = super().get_actions(request)
  67. if 'delete_selected' in actions:
  68. del actions['delete_selected']
  69. return actions
  70. def has_delete_permission(self, request, obj=None):
  71. return obj and not obj.profile.adhesion
  72. class AdherentTypeFilter(admin.SimpleListFilter):
  73. title = 'type d’adhérent'
  74. parameter_name = 'type'
  75. def lookups(self, request, model_admin):
  76. return (
  77. ('physique', 'Personne physique'),
  78. ('morale', 'Personne morale'),
  79. )
  80. def queryset(self, request, queryset):
  81. if self.value() == 'physique':
  82. return queryset.filter(adherent_type__app_label='auth',
  83. adherent_type__model='user')
  84. if self.value() == 'morale':
  85. return queryset.filter(adherent_type__app_label='adhesions',
  86. adherent_type__model='corporation')
  87. class AdhesionAdmin(admin.ModelAdmin):
  88. list_display = ('get_id', 'get_adherent_link', 'type',)
  89. list_filter = (AdherentTypeFilter,)
  90. fields = ('id',)
  91. readonly_fields = ('id',)
  92. search_fields = ('id',)
  93. #inlines = (PaymentInline,)
  94. def get_id(self, obj):
  95. return 'ADT%d' % obj.id
  96. get_id.short_description = 'Numéro d’adhérent'
  97. def get_adherent_link(self, obj):
  98. if obj.is_physical():
  99. model_name = 'proxyuser'
  100. else:
  101. model_name = 'corporation'
  102. url = reverse('admin:adhesions_%s_change' % model_name, args=(obj.adherent.id,))
  103. return format_html(u'<a href="{}">{}</a>', url, str(obj.adherent))
  104. get_adherent_link.short_description = 'Nom ou raison sociale'
  105. def get_search_results(self, request, queryset, search_term):
  106. queryset, use_distinct = super().get_search_results(request, queryset, search_term)
  107. users = User.objects.filter(
  108. Q(username__icontains=search_term)
  109. | Q(first_name__icontains=search_term)
  110. | Q(last_name__icontains=search_term)
  111. )
  112. user_type = ContentType.objects.get_for_model(User)
  113. queryset |= Adhesion.objects.filter(adherent_type=user_type,
  114. adherent_id__in=users.values_list('pk'))
  115. corporations = Corporation.objects.filter(social_reason__icontains=search_term)
  116. corporation_type = ContentType.objects.get_for_model(Corporation)
  117. queryset |= Adhesion.objects.filter(adherent_type=corporation_type,
  118. adherent_id__in=corporations.values_list('pk'))
  119. return queryset, use_distinct
  120. def get_actions(self, request):
  121. actions = super().get_actions(request)
  122. if 'delete_selected' in actions:
  123. del actions['delete_selected']
  124. return actions
  125. def has_add_permission(self, request):
  126. return False
  127. def has_delete_permission(self, request, obj=None):
  128. return False
  129. class CorporationAdmin(admin.ModelAdmin):
  130. list_display = ('social_reason', 'adherent_id')
  131. inlines = (AdhesionInline,)
  132. search_fields = ('social_reason',)
  133. filter_horizontal = ('members',)
  134. def adherent_id(self, corporation):
  135. adherent = corporation.adhesion
  136. if adherent:
  137. return adherent.id
  138. adherent_id.short_description = 'Numéro d’adhérent'
  139. def get_actions(self, request):
  140. actions = super().get_actions(request)
  141. if 'delete_selected' in actions:
  142. del actions['delete_selected']
  143. return actions
  144. def has_delete_permission(self, request, obj=None):
  145. return obj and not obj.adhesion
  146. admin.site.unregister(User)
  147. admin.site.unregister(Group)
  148. admin.site.register(ProxyUser, UserAdmin)
  149. admin.site.register(Corporation, CorporationAdmin)
  150. admin.site.register(Adhesion, AdhesionAdmin)