admin.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 UserAdmin(AuthUserAdmin):
  30. list_display = AuthUserAdmin.list_display + ('adherent_id',)
  31. list_filter = (AuthUserAdmin.list_filter[2],) # actif
  32. inlines = (ProfileInlineDjango, AdhesionInline,)
  33. def adherent_id(self, user):
  34. adherent = user.profile.adhesion
  35. if adherent:
  36. return adherent.id
  37. adherent_id.short_description = 'Numéro d’adhérent'
  38. def get_fieldsets(self, request, obj=None):
  39. return (
  40. AuthUserAdmin.fieldsets[0],
  41. AuthUserAdmin.fieldsets[1],
  42. (AuthUserAdmin.fieldsets[2][0], {
  43. 'classes': ('collapse',),
  44. 'fields': ('is_active', 'is_staff', 'is_superuser',), # removing groups and user_permissions
  45. }),
  46. (AuthUserAdmin.fieldsets[3][0], {
  47. 'classes': ('collapse',),
  48. 'fields': AuthUserAdmin.fieldsets[3][1]['fields'],
  49. }),
  50. )
  51. def get_actions(self, request):
  52. actions = super().get_actions(request)
  53. if 'delete_selected' in actions:
  54. del actions['delete_selected']
  55. return actions
  56. def has_delete_permission(self, request, obj=None):
  57. return obj and not obj.profile.adhesion
  58. class AdherentTypeFilter(admin.SimpleListFilter):
  59. title = 'type d’adhérent'
  60. parameter_name = 'type'
  61. def lookups(self, request, model_admin):
  62. return (
  63. ('physique', 'Personne physique'),
  64. ('morale', 'Personne morale'),
  65. )
  66. def queryset(self, request, queryset):
  67. if self.value() == 'physique':
  68. return queryset.filter(adherent_type__app_label='auth',
  69. adherent_type__model='user')
  70. if self.value() == 'morale':
  71. return queryset.filter(adherent_type__app_label='adhesions',
  72. adherent_type__model='corporation')
  73. class AdhesionAdmin(admin.ModelAdmin):
  74. list_display = ('get_id', 'get_adherent_link', 'type',)
  75. list_filter = (AdherentTypeFilter,)
  76. fields = ('id',)
  77. readonly_fields = ('id',)
  78. search_fields = ('id',)
  79. #inlines = (PaymentInline,)
  80. def get_id(self, obj):
  81. return 'ADT%d' % obj.id
  82. get_id.short_description = 'Numéro d’adhérent'
  83. def get_adherent_link(self, obj):
  84. if obj.is_physical():
  85. model_name = 'proxyuser'
  86. else:
  87. model_name = 'corporation'
  88. url = reverse('admin:adhesions_%s_change' % model_name, args=(obj.adherent.id,))
  89. return format_html(u'<a href="{}">{}</a>', url, str(obj.adherent))
  90. get_adherent_link.short_description = 'Nom ou raison sociale'
  91. def get_search_results(self, request, queryset, search_term):
  92. queryset, use_distinct = super().get_search_results(request, queryset, search_term)
  93. users = User.objects.filter(
  94. Q(username__icontains=search_term)
  95. | Q(first_name__icontains=search_term)
  96. | Q(last_name__icontains=search_term)
  97. )
  98. user_type = ContentType.objects.get_for_model(User)
  99. queryset |= Adhesion.objects.filter(adherent_type=user_type,
  100. adherent_id__in=users.values_list('pk'))
  101. corporations = Corporation.objects.filter(social_reason__icontains=search_term)
  102. corporation_type = ContentType.objects.get_for_model(Corporation)
  103. queryset |= Adhesion.objects.filter(adherent_type=corporation_type,
  104. adherent_id__in=corporations.values_list('pk'))
  105. return queryset, use_distinct
  106. def get_actions(self, request):
  107. actions = super().get_actions(request)
  108. if 'delete_selected' in actions:
  109. del actions['delete_selected']
  110. return actions
  111. def has_add_permission(self, request):
  112. return False
  113. def has_delete_permission(self, request, obj=None):
  114. return False
  115. class CorporationAdmin(admin.ModelAdmin):
  116. list_display = ('social_reason', 'adherent_id')
  117. inlines = (AdhesionInline,)
  118. search_fields = ('social_reason',)
  119. filter_horizontal = ('members',)
  120. def adherent_id(self, corporation):
  121. adherent = corporation.adhesion
  122. if adherent:
  123. return adherent.id
  124. adherent_id.short_description = 'Numéro d’adhérent'
  125. def get_actions(self, request):
  126. actions = super().get_actions(request)
  127. if 'delete_selected' in actions:
  128. del actions['delete_selected']
  129. return actions
  130. def has_delete_permission(self, request, obj=None):
  131. return obj and not obj.adhesion
  132. admin.site.unregister(User)
  133. admin.site.unregister(Group)
  134. admin.site.register(ProxyUser, UserAdmin)
  135. admin.site.register(Corporation, CorporationAdmin)
  136. admin.site.register(Adhesion, AdhesionAdmin)