from django.contrib import admin
from django.contrib.auth.models import User, Group
from gestion.models import ErnestoUser
from django.contrib.auth.admin import UserAdmin
from calendrier.models import Event
from propositions.models import Prop

class UserProfileInline(admin.StackedInline):
    model = ErnestoUser
    
def ProfileInfo(field, short_description, boolean=False):
    def getter(self):
        try:
            return getattr(self.profile, field)
        except ErnestoUser.DoesNotExist:
            return ""
    getter.short_description = short_description
    getter.boolean = boolean
    return getter

User.profile_phone = ProfileInfo("phone", "Telephone")
User.profile_instru = ProfileInfo("instru", "Instrument joué")
User.profile_is_ern = ProfileInfo("is_ernesto", "Ernestophoniste")
User.profile_is_chef = ProfileInfo("is_chef", "Chef Fanfare")

class UserProfileAdmin(UserAdmin):
    list_display = ('username', 'first_name', 'last_name', 'email', 'profile_phone', 'profile_instru', 'profile_is_ern', 'profile_is_chef',)
    list_display_links = ('username', 'email', 'first_name', 'last_name',)
    list_filter = ('profile__instru',)
    ordering = ('username',)
    search_fields = ('username', 'first_name', 'last_name', 'profile__phone', 'profile__instru',)
    inlines = [ UserProfileInline, ]
    fieldsets = (('Général', { 'fields': ('username', 'first_name', 'last_name', ) }),
    ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser' )})
)


admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.register(User, UserProfileAdmin)
admin.site.register(Event)
admin.site.register(Prop)
# Register your models here.