from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from minecraft_manager.api.models import Token


class TokenActiveFilter(admin.SimpleListFilter):
    title = _('Active')
    parameter_name = 'active'

    def lookups(self, request, model_admin):
        return (
            ('0', _('Active')),
            ('1', _('Inactive')),
        )

    def queryset(self, request, queryset):
        if self.value() == '0':
            return queryset.filter(active=True)
        if self.value() == '1':
            return queryset.filter(active=False)


class TokenAdmin(admin.ModelAdmin):
    list_filter = (TokenActiveFilter,)
    list_display = ('display', 'active', 'web_get_permission', 'web_post_permission', 'plugin_get_permission',
                    'plugin_post_permission', 'form_get_permission', 'form_post_permission', 'model_get_permission',
                    'model_post_permission', 'stats_get_permission', 'stats_post_permission')
    fieldsets = (
        (None, {
            'fields': ('key', 'active', 'description')
        }),
        ('Permissions', {
            'fields': ('web_get_permission', 'web_post_permission', 'plugin_get_permission', 'plugin_post_permission',
                       'form_get_permission', 'form_post_permission', 'model_get_permission', 'model_post_permission',
                       'stats_get_permission', 'stats_post_permission')
        })
    )


def register():
    try:
        admin.site.register(Token, TokenAdmin)
    except admin.sites.AlreadyRegistered:
        pass

