#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
from django.contrib import admin
from wifiadmin.main.models import MyUser, MySession, Log
from django import forms
from django.db import models
from main.forms import AddUserForm
from main.utils import wificmd

class UserAdmin(admin.ModelAdmin):
    list_display = ['userName', 'firstName', 'lastName', 'active', 'expire', 'comment']
    formfield_overrides = {
                           #models.CharField: {'widget': forms.PasswordInput}, 
                           }
    #list_editable = ['active',]    
    #readonly_fields = ['userName']
    list_filter = ['active', 'expire']
    list_per_page = 20
    
    form = AddUserForm
    
    class Media:
        js = ['wifiadmin/js/utils.js',]
        css = {
            "all": ("wifiadmin/css/style.css",)
        }
    

class SessionAdmin(admin.ModelAdmin):
    list_display = ['user', 'ip', 'start', 'end', 'online']
    list_filter = ['online', 'start', 'end']
    actions = ['disconnectUser']

    def disconnectUser(self, request, queryset):
        for session in queryset:
            if session.online:
                session.online = False
                session.end = datetime.datetime.now()        
                wificmd('delete', session.ip)
                session.save()
        self.message_user(request, u"Выбранные пользователи отключены" )
    
    disconnectUser.short_description = u"Отключить пользователей"

    

class LogAdmin(admin.ModelAdmin):
    list_display = ['date','type', 'msg']
    list_filter = ['date', 'type']
    
admin.site.register(MyUser,UserAdmin)
admin.site.register(MySession,SessionAdmin)
admin.site.register(Log,LogAdmin)

