from django.contrib import admin
from .models import *

from import_export.admin import ImportExportModelAdmin
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
from unfold.contrib.forms.widgets import ArrayWidget, WysiwygWidget
from django.contrib.postgres.fields import ArrayField
from utils.admin import ModelAdmin
from unfold.widgets import UnfoldAdminTextareaWidget
@admin.register(BlogModel)
class BlogModelAdmin(ModelAdmin, ImportExportModelAdmin):
    import_form_class = ImportForm
    export_form_class = ExportForm
    search_fields = ['title', 'content', 'summery', ]
    list_filter = ['category', 'is_published', 'author']

    compressed_fields = True
    warn_unsaved_form = True

    formfield_overrides = {
        models.TextField: {
            "widget": WysiwygWidget,
        },
        ArrayField: {
            "widget": ArrayWidget,
        }
    }

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        if db_field.name == "summery":
            kwargs["widget"] = UnfoldAdminTextareaWidget()  
        return super().formfield_for_dbfield(db_field, request, **kwargs)

@admin.register(BlogCategoryModel)
class BlogCategoryAdmin(ModelAdmin):
    def has_view_permission(self, request, obj=None):
        return request.user.is_superuser
    
    def has_add_permission(self, request, obj=None):    
        return request.user.is_superuser
    
    def has_view_permission(self, request, obj=None):
        return request.user.is_superuser
    
    def has_change_permission(self, request, obj = ...):
        return request.user.is_superuser
    
    
    def has_delete_permission(self, request, obj = ...):
        return request.user.is_superuser