from django.contrib import admin from django.contrib.auth.models import User, Group from django.contrib.contenttypes.admin import GenericStackedInline, GenericTabularInline from django.db.models import Q from django.contrib.contenttypes.models import ContentType from django.contrib.auth.admin import UserAdmin as AuthUserAdmin from django.core.urlresolvers import reverse from django.utils.html import format_html from .forms import AdhesionForm from .models import ProxyUser, Corporation, Adhesion from accounts.models import Profile from banking.admin import PaymentInline class AdhesionInline(GenericTabularInline): model = Adhesion ct_field = 'adherent_type' ct_fk_field = 'adherent_id' form = AdhesionForm min_num = 1 max_num = 1 extra = 0 def has_delete_permission(self, request, obj=None): return False class ProfileInlineDjango(admin.StackedInline): model = Profile def has_add_permission(self, request): return False def has_delete_permission(self, request, obj=None): return False class UserAdmin(AuthUserAdmin): list_display = AuthUserAdmin.list_display + ('adherent_id',) list_filter = (AuthUserAdmin.list_filter[2],) # actif inlines = (ProfileInlineDjango, AdhesionInline,) def adherent_id(self, user): adherent = user.profile.adhesion if adherent: return adherent.id adherent_id.short_description = 'Numéro d’adhérent' def get_fieldsets(self, request, obj=None): return ( AuthUserAdmin.fieldsets[0], AuthUserAdmin.fieldsets[1], (AuthUserAdmin.fieldsets[2][0], { 'classes': ('collapse',), 'fields': ('is_active', 'is_staff', 'is_superuser',), # removing groups and user_permissions }), (AuthUserAdmin.fieldsets[3][0], { 'classes': ('collapse',), 'fields': AuthUserAdmin.fieldsets[3][1]['fields'], }), ) def get_actions(self, request): actions = super().get_actions(request) if 'delete_selected' in actions: del actions['delete_selected'] return actions def has_delete_permission(self, request, obj=None): return obj and not obj.profile.adhesion class AdherentTypeFilter(admin.SimpleListFilter): title = 'type d’adhérent' parameter_name = 'type' def lookups(self, request, model_admin): return ( ('physique', 'Personne physique'), ('morale', 'Personne morale'), ) def queryset(self, request, queryset): if self.value() == 'physique': return queryset.filter(adherent_type__app_label='auth', adherent_type__model='user') if self.value() == 'morale': return queryset.filter(adherent_type__app_label='adhesions', adherent_type__model='corporation') class AdhesionAdmin(admin.ModelAdmin): list_display = ('get_id', 'get_adherent_link', 'type',) list_filter = (AdherentTypeFilter,) fields = ('id',) readonly_fields = ('id',) search_fields = ('id',) #inlines = (PaymentInline,) def get_id(self, obj): return 'ADT%d' % obj.id get_id.short_description = 'Numéro d’adhérent' def get_adherent_link(self, obj): if obj.is_physical(): model_name = 'proxyuser' else: model_name = 'corporation' url = reverse('admin:adhesions_%s_change' % model_name, args=(obj.adherent.id,)) return format_html(u'{}', url, str(obj.adherent)) get_adherent_link.short_description = 'Nom ou raison sociale' def get_search_results(self, request, queryset, search_term): queryset, use_distinct = super().get_search_results(request, queryset, search_term) users = User.objects.filter( Q(username__icontains=search_term) | Q(first_name__icontains=search_term) | Q(last_name__icontains=search_term) ) user_type = ContentType.objects.get_for_model(User) queryset |= Adhesion.objects.filter(adherent_type=user_type, adherent_id__in=users.values_list('pk')) corporations = Corporation.objects.filter(social_reason__icontains=search_term) corporation_type = ContentType.objects.get_for_model(Corporation) queryset |= Adhesion.objects.filter(adherent_type=corporation_type, adherent_id__in=corporations.values_list('pk')) return queryset, use_distinct def get_actions(self, request): actions = super().get_actions(request) if 'delete_selected' in actions: del actions['delete_selected'] return actions def has_add_permission(self, request): return False def has_delete_permission(self, request, obj=None): return False class CorporationAdmin(admin.ModelAdmin): list_display = ('social_reason', 'adherent_id') inlines = (AdhesionInline,) search_fields = ('social_reason',) filter_horizontal = ('members',) def adherent_id(self, corporation): adherent = corporation.adhesion if adherent: return adherent.id adherent_id.short_description = 'Numéro d’adhérent' def get_actions(self, request): actions = super().get_actions(request) if 'delete_selected' in actions: del actions['delete_selected'] return actions def has_delete_permission(self, request, obj=None): return obj and not obj.adhesion admin.site.unregister(User) admin.site.unregister(Group) admin.site.register(ProxyUser, UserAdmin) admin.site.register(Corporation, CorporationAdmin) admin.site.register(Adhesion, AdhesionAdmin)