from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.db import models
from django.utils.translation import ugettext_lazy as _

from .models import (
    Colaborator, Company, Event, Invite, Invoice, Link, Message, Payment,
    Notification, Role, User
)


# TabularInline

class ColaboratorInline(admin.TabularInline):
    autocomplete_fields = ('user', 'company', )
    extra = 0
    fields = ('user', 'company', 'is_active', 'roles', 'permissions')
    model = Colaborator


class LinkInline(admin.TabularInline):
    extra = 0
    model = Link


class PaymentInline(admin.TabularInline):
    extra = 1
    formfield_overrides = {
        models.TextField: {'widget': forms.TextInput},
    }
    model = Payment


# ModelAdmin

class ColaboratorAdmin(admin.ModelAdmin):
    list_display = ('user', 'company', 'date_joined', 'is_active', )
    list_filter = ('company', )


class CompanyAdmin(admin.ModelAdmin):
    autocomplete_fields = (
        'user',
    )
    inlines = (
        ColaboratorInline,
    )
    list_display = ('name', 'user', )
    search_fields = ('name', )


class EventAdmin(admin.ModelAdmin):
    autocomplete_fields = (
        'share_with',
    )
    model = Event
    list_display = (
        '__str__', 'company', 'user', 'date_start', 'date_finish'
    )
    list_filter = ('company', )


class InvoiceAdmin(admin.ModelAdmin):
    autocomplete_fields = (
        'company',
    )
    inlines = (PaymentInline, )
    list_display = ('__str__', 'company', 'total')
    list_filter = ('company', )
    search_fields = ('id', )


class InviteAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'email', 'user')
    list_filter = ('company', )
    search_fields = ('name', 'email')


class LinkModelAdmin(admin.ModelAdmin):
    list_display = ('destination', 'message', 'user')
    list_filter = ('company', )
    model = Link


class MessageAdmin(admin.ModelAdmin):
    inlines = (
        LinkInline,
    )
    list_display = (
        'from_', 'to', 'company', 'date_creation', 'direction'
    )
    list_filter = ('company', 'direction')
    model = Message

    def has_add_permission(self, request):
        return False


class NotificationAdmin(admin.ModelAdmin):
    list_display = (
        'user', 'company', 'date_creation', 'content',
    )
    list_filter = (
        'company',
    )
    model = Notification

    def has_add_permission(self, request):
        return False


class RoleAdmin(admin.ModelAdmin):
    filter_horizontal = ('permissions',)
    list_display = ('name', 'company')
    list_filter = ('company', )
    search_fields = ('name', )


class UserAdmin(UserAdmin):
    autocomplete_fields = (
        'company',
    )
    fieldsets = (
        (
            None, {
                'fields': ('username', 'password')
            },
        ),
        (
            _("Personal info"), {
                'fields': (
                    'first_name', 'last_name', 'email', 'company', 'timezone',
                    'language', 'photo',
                )
            },
        ),
        (
            _("Permissions"), {
                'fields': (
                    'is_active', 'is_staff', 'is_superuser',
                    'groups', 'user_permissions'
                )
            },
        ),
        (
            _("Important dates"), {
                'fields': (
                    'last_login', 'date_joined'
                )
            },
        ),
    )
    inlines = (ColaboratorInline, )
    list_filter = (
        'is_staff', 'is_superuser', 'is_active', 'company'
    )


admin.site.register(Company, CompanyAdmin)
admin.site.register(Colaborator, ColaboratorAdmin)
admin.site.register(Event, EventAdmin)
admin.site.register(Invite, InviteAdmin)
admin.site.register(Invoice, InvoiceAdmin)
admin.site.register(Link, LinkModelAdmin)
admin.site.register(Message, MessageAdmin)
admin.site.register(Notification, NotificationAdmin)
admin.site.register(Role, RoleAdmin)
admin.site.register(User, UserAdmin)

@@ -787,17 +787,45 @@
 elAdmin%0A
+@admin.register(Colaborator)
 %0A
-
 class Co
@@ -957,24 +957,49 @@
 mpany', )%0A%0A%0A
+@admin.register(Company)%0A
 class Compan
@@ -1190,24 +1190,47 @@
 'name', )%0A%0A%0A
+@admin.register(Event)%0A
 class EventA
@@ -1450,24 +1450,49 @@
 mpany', )%0A%0A%0A
+@admin.register(Invoice)%0A
 class Invoic
@@ -1712,24 +1712,48 @@
  ('id', )%0A%0A%0A
+@admin.register(Invite)%0A
 class Invite
@@ -1901,24 +1901,46 @@
  'email')%0A%0A%0A
+@admin.register(Link)%0A
 class LinkMo
@@ -2068,24 +2068,49 @@
 el = Link%0A%0A%0A
+@admin.register(Message)%0A
 class Messag
@@ -2393,24 +2393,54 @@
 urn False%0A%0A%0A
+@admin.register(Notification)%0A
 class Notifi
@@ -2689,16 +2689,38 @@
 False%0A%0A%0A
+@admin.register(Role)%0A
 class Ro
@@ -2891,16 +2891,38 @@
 e', )%0A%0A%0A
+@admin.register(User)%0A
 class Us
@@ -3870,441 +3870,10 @@
 ny'%0A
+
     )%0A
-%0A%0Aadmin.site.register(Company, CompanyAdmin)%0Aadmin.site.register(Colaborator, ColaboratorAdmin)%0Aadmin.site.register(Event, EventAdmin)%0Aadmin.site.register(Invite, InviteAdmin)%0Aadmin.site.register(Invoice, InvoiceAdmin)%0Aadmin.site.register(Link, LinkModelAdmin)%0Aadmin.site.register(Message, MessageAdmin)%0Aadmin.site.register(Notification, NotificationAdmin)%0Aadmin.site.register(Role, RoleAdmin)%0Aadmin.site.register(User, UserAdmin)%0A

Use decorator instead of function