from django.contrib import admin
from django.utils.safestring import mark_safe

from ..models.content_config import ContentConfig
from .mixins import SingletonAdminMixin


@admin.register(ContentConfig)
class ContentConfigAdmin(SingletonAdminMixin, admin.ModelAdmin):

    fieldsets = (('内容配置', {
        'fields': ('auto_summary', 'summary_length', 'comments_enabled',
                   'min_tag_length', 'date_hierarchy')
    }), )

    # 自定义界面标题
    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        extra_context['title'] = "编辑内容扩展配置"
        return super().change_view(request,
                                   object_id,
                                   form_url,
                                   extra_context=extra_context)

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        field = super().formfield_for_dbfield(db_field, request, **kwargs)
        if db_field.name == 'min_tag_length':
            # 保持HTML格式的help_text
            field.help_text = mark_safe(db_field.help_text)
            # 添加自定义样式
            field.widget.attrs['style'] = 'width:120px;'
        return field
