""" Admin Interface to manage the memberships."""
# import stuff we need from django
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from django.contrib.auth.models import Group
from django.utils.translation import ugettext as _
from easy_thumbnails import fields, widgets

from membership.models import Membership, ActivationRequest


def activate_user(modeladmin, request, queryset):
    """Triggers activation of the selects actication requests by hand.

    :param modeladmin: The ModelAdmin that triggered this action.
    :param request: An HttpRequest representing the current request.
    :param queryset: A QuerySet containing the objects selected by the user.
    """
    [activation.activate() for activation in queryset ]


activate_user.short_description = _('Activate selected User')


def cleanup_activation(modeladmin, request, queryset):  # Ignore PyLintBear (W0613)
    """Delete every selected activation request that has been expired.

    :param modeladmin: The ModelAdmin that triggered this action.
    :param request: An HttpRequest representing the current request.
    :param queryset: A QuerySet containing the objects selected by the user.
    """
    for activation in queryset:
        if activation.expired:
            activation.user.delete()


cleanup_activation.short_description = _(
    "Cleanup selected Activation Requests")


class ProxyGroup(Group):
    """A Proxy to list Usergroups from django.contrib.auth here."""

    class Meta:
        """ MetaData to localize this Proxy Group."""
        proxy = True
        verbose_name = _('Group')
        verbose_name_plural = _('Groups')


class MembershipAdmin(UserAdmin):
    """Admin interface to manage membership. e.g. users."""
    formfield_overrides = {
        fields.ThumbnailerImageField: {
            'widget': widgets.ImageClearableFileInput},
    }
    list_filter = ('is_active', 'membership', 'confirmed')
    list_display = (
        'username',
        'first_name',
        'last_name',
        'is_active',
        'membership',
        'confirmed',
        'paid_until',
    )
    list_editable = ('confirmed', 'paid_until',)
    list_display_links = ('username',)
    fieldsets = (
        (None, {'fields': (('username', 'password'), 'gender', 'avatar',
                           ('first_name', 'last_name'), ('email', 'website'))}),
        (_('Membership'), {'classes': ('collapse',),
                           'fields': (
                               ('membership', 'confirmed'), 'birthday',
                               'telephone',
                               'street_name', ('post_code', 'city'))}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    ordering = ('username',)
    search_fields = ('username', 'first_name', 'last_name',)


admin.site.register(Membership, MembershipAdmin)


class RegistrationAdmin(admin.ModelAdmin):
    """To administrag user activation requests.
    Kasu uses an two factor registration via email."""
    list_display = ('username', 'first_name', 'last_name', 'email',
                    'registration_date', 'expired')
    search_fields = ('user__username', 'user__first_name')
    actions = [cleanup_activation, activate_user]


admin.site.register(ActivationRequest, RegistrationAdmin)
admin.site.unregister(Group)
admin.site.register(ProxyGroup, GroupAdmin)
