# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as Admin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import Group
from django.utils.translation import ugettext_lazy as _

from .filters import ChoiceDropdownFilter
from .models import User
from restaurant.models import Restaurant


class RegistrationForm(UserCreationForm):

    """A form for users creation.

    Email, username, password and role are given.
    """

    email = forms.EmailField(required=True)

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = User
        fields = ('role',)

    def save(self, commit=True):
        """Save a new user.

        Return a User object.
        """
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.role = self.cleaned_data['role']
        user.set_is_staff(user.role)
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):

    """A form for users modification."""

    class Meta:

        """Give some options (metadata) attached to the form."""

        model = User
        fields = ('username', 'email', 'phone', 'role', 'status',)

    def save(self, commit=True):
        """Save the provided password in a hashed format and put
        is_active into an appropriate value (according to the
        user's status)

        Return a User object.
        """
        user = super(UserChangeForm, self).save(commit=False)
        user.set_is_active(user.status)
        user.set_is_staff(user.role)
        if commit:
            user.save()
        return user


def delete_selected_users(modeladmin, request, queryset):
    """Block selected users instead of dropping them."""
    for obj in queryset:
        obj.delete()


delete_selected_users.short_description = "Delete selested users"


class UserAdmin(Admin):

    """Represent a model in the admin interface."""

    form = UserChangeForm
    add_form = RegistrationForm

    search_fields = ('username', 'email', 'phone')
    list_display = ('username', 'email', 'phone', 'role', 'status')
    ordering = ['username']
    list_per_page = 10
    list_filter = [('status', ChoiceDropdownFilter), 'role']

    fieldsets = (
        (None, {'fields': ('username', 'email',)}),
        (_('Personal info'), {'fields': ('phone',)}),
        (_('Status'), {'fields': ('status',)}),
        (_('Permissions'), {'fields': ('role', 'user_permissions')}),
    )

    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'fields': ('email', 'username', 'password1', 'password2', 'role')}
         ),
    )

    actions = [delete_selected_users]


def soft_delete(modeladmin, request, queryset):
    """Soft delete function for QuerySet list."""
    for obj in queryset:
        obj.delete()


soft_delete.short_description = "Delete selected items"


class PageAdmin(admin.ModelAdmin):

    """Custom display in restaurant's list."""

    list_display = ('name', '_type_id', 'status', 'tables_count')
    list_per_page = 15
    actions = [soft_delete]
    admin.site.disable_action('delete_selected')
    list_filter = ['status']

    def _type_id(self, obj):
        return obj.type_id
    _type_id.short_description = 'restaurant type'


admin.site.register(User, UserAdmin)
admin.site.register(Restaurant, PageAdmin)
admin.site.unregister(Group)

@@ -2393,22 +2393,46 @@
 ilter), 
+(
 'role'
+, ChoiceDropdownFilter)
 %5D%0A%0A    f
@@ -3469,24 +3469,25 @@
 lter = %5B
+(
 'status'
 %5D%0A%0A    d
@@ -3478,16 +3478,39 @@
 'status'
+, ChoiceDropdownFilter)
 %5D%0A%0A    d

Make all filters dropdown