from django.contrib import admin
from django.contrib.auth.models import User

# Register your models here.
from bigsansar.contrib.sites.forms import create_domainform, customaddpageform, custompageform
from bigsansar.contrib.sites.models import domains, pages


class domainadmin(admin.ModelAdmin):
    form = create_domainform
    list_display = ['domain', 'publish_date', 'visitor']

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=request.user.id)

    def get_form(self, request, obj=None, **kwargs):
        ModelForm = super(domainadmin, self).get_form(request, obj, **kwargs)

        class ModelFormWithRequest(ModelForm):
            def __new__(cls, *args, **kwargs):
                kwargs['request'] = request
                return ModelForm(*args, **kwargs)

        return ModelFormWithRequest


class pageadmin(admin.ModelAdmin):
    form = custompageform
    prepopulated_fields = {"slug": ("title",)}
    list_display = ['title', 'domain', 'publish_date', 'visitor']


admin.site.register(domains, domainadmin)
admin.site.register(pages, pageadmin)
