from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.forms import model_to_dict
from django.utils.timezone import now

from main.forms import AdminLanProfileForm, AdminProfileForm, AdminLanForm
from .models import Profile, Lan, LanProfile

admin.site.unregister(User)


@admin.register(LanProfile)
class LanProfileAdmin(admin.ModelAdmin):
    list_filter = ('lan__name', 'paytype', 'paid')
    list_display = ('profile', 'lan', 'seat', 'get_paytype', 'paid')
    search_fields = ('profile__user__first_name', 'profile__user__username', 'seat')
    form = AdminLanProfileForm

    def get_paytype(self, obj):
        return obj.get_paytype_display()

    get_paytype.short_description = LanProfile._meta.get_field('paytype').verbose_name


class ProfileInline(admin.StackedInline):
    model = Profile
    form = AdminProfileForm


@admin.register(User)
class MyUserAdmin(UserAdmin):
    list_display = ('username', 'email', 'first_name', 'get_grade', 'is_staff')

    def get_grade(self, user):
        return user.profile.grade

    get_grade.short_description = 'Klasse'

    inlines = [
        ProfileInline
    ]

    list_filter = UserAdmin.list_filter + ('profile__grade',)


@admin.register(Lan)
class LanAdmin(admin.ModelAdmin):
    list_display = ('name', 'start', 'seats_count', 'is_open')
    form = AdminLanForm

    fieldsets = (
        ('Tider', {
            'fields': (('start', 'end'), 'open')
        }),
        ('Pladser', {
            'fields': ('seats',)
        }),
        ('Tekst', {
            'fields': ('name', 'schedule', 'blurb')
        }),
        ('Betaling', {
            'fields': ('paytypes', 'price')
        }),
    )

    def get_changeform_initial_data(self, request):
        try:
            prev_lan = Lan.objects.filter(start__lt=now()).order_by("-start")[0]
            return model_to_dict(prev_lan, ['blurb', 'seats', 'schedule'])
        except (Lan.DoesNotExist, AttributeError, IndexError):
            return {}

@@ -157,16 +157,62 @@
 to_dict%0A
+from django.utils.safestring import mark_safe%0A
 from dja
@@ -861,16 +861,338 @@
 e_name%0A%0A
+    actions = %5B'paid', 'not_paid'%5D%0A%0A    def paid(self, request, queryset):%0A        queryset.update(paid=True)%0A    paid.short_description = %22Mak%C3%A9r som betalt.%22%0A%0A    def not_paid (self, request, queryset):%0A        queryset.update(paid=False)%0A    not_paid.short_description = mark_safe(mark_safe(%22Mark%C3%A9r som ikke betalt.%22))%0A%0A
 %0Aclass P

Add paid/not_paid actions to LanProfile