from django.contrib import admin
from django.contrib.auth.models import User

from vehicles.models import *

class _vhc_admin(admin.ModelAdmin):

    list_display = ('sName', 'nBases', 'nStatus', "bLucas")
    ordering = ["nOrder"]


    search_fields = ['sName']
    list_filter = [
        "sName",
        "nBases",
        "nStatus"
    ]


    #fields = ["sBases", "dtDate",'type', "user","nHour", "nMinutes", "sRemarques"]
    fieldsets = (
        ('Informations liées au véhicule', {
            'fields': ('sName', "nBases")
        }),
        ('Statut et équipement', {
            'fields': ('nStatus', 'bLucas')
        }),
        ('Informations diverses', {
            'fields': ("nOrder", "nType")
        }),
    )

class _vhc_infos_admin(admin.ModelAdmin):
    always_show_username = True
    save_as = True
    list_display = ('Vehicle', 'nType', 'sDesc')

    list_filter = [
        "Vehicle",
        "nType",
        "bEnabled"
    ]



    search_fields = ['Vehicle']


    #fields = ["Vehicle", "nType",'sTitle', "sDesc","dtStart", "dtEnd", "Author"]
    fieldsets = (
        ('Informations liées au véhicule', {
            'fields': ('Vehicle',)
        }),
        ('Information sur le message', {
            'fields': ('nType','sDesc')
        }),
        ('Information supplémentaires et validité', {
            'fields': ("dtStart","dtEnd", 'Author', "bEnabled")
        }),
    )

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        field = super(_vhc_infos_admin, self).formfield_for_foreignkey(db_field, request, **kwargs)
        if db_field.remote_field.model == User:
            field.label_from_instance = self.get_user_label
        return field

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        field = super(_vhc_infos_admin, self).formfield_for_manytomany(db_field, request, **kwargs)
        if db_field.remote_field.model == User:
            field.label_from_instance = self.get_user_label
        return field

    def get_user_label(self, user):
        name = user.get_full_name()
        username = user.username
        if not self.always_show_username:
            return name or username
        return (name and name != username and '%s (%s)' % (name, username) or username)



admin.site.register(Vehicles_infos, _vhc_infos_admin)
admin.site.register(Vehicles, _vhc_admin)
