from django.contrib import admin from django.utils.html import format_html from file.models import File @admin.register(File) class FileAdmin(admin.ModelAdmin): """Admin interface for Files""" list_display = ['id', 'name_with_icon', 'path_display', 'mime_type_display', 'parent_display', 'children_count', 'created_at'] list_filter = ['mime_type', 'created_at', 'user'] search_fields = ['name', 'path', 'mime_type', 'text'] readonly_fields = ['created_at', 'updated_at', 'text_preview'] fieldsets = [ ('File Info', { 'fields': ['name', 'path', 'mime_type', 'parent', 'user'] }), ('Content', { 'fields': ['text_preview', 'external_url'], 'classes': ['collapse'] }), ('Metadata', { 'fields': ['metadata'], 'classes': ['collapse'] }), ('Timestamps', { 'fields': ['created_at', 'updated_at'], 'classes': ['collapse'] }), ] def name_with_icon(self, obj): """Show name with icon based on mime type""" icon = '📁' if obj.mime_type == 'application/x-folder' else '📄' if obj.mime_type.startswith('text/markdown'): icon = '📝' elif obj.mime_type.startswith('application/pdf'): icon = '📕' elif obj.mime_type.startswith('video/'): icon = '🎥' return format_html('{} {}', icon, obj.name) name_with_icon.short_description = 'Name' def path_display(self, obj): """Show path with folder/file distinction""" if obj.path: parts = obj.path.split('/') if len(parts) > 1: folder_path = '/'.join(parts[:-1]) return format_html('{}/{}', folder_path, parts[-1]) return obj.path or '-' path_display.short_description = 'Path' def mime_type_display(self, obj): """Show mime type with color coding""" color = '#999' if obj.mime_type == 'application/x-folder': color = '#3b82f6' elif obj.mime_type.startswith('text/'): color = '#10b981' elif obj.mime_type.startswith('application/pdf'): color = '#ef4444' return format_html('{}', color, obj.mime_type) mime_type_display.short_description = 'MIME Type' def parent_display(self, obj): """Show parent file""" if obj.parent: icon = '📁' if obj.parent.mime_type == 'application/x-folder' else '📄' return format_html('{} {}', icon, obj.parent.name) return '-' parent_display.short_description = 'Parent' def children_count(self, obj): """Show number of child files""" count = obj.children.count() if count > 0: return format_html('{}', count) return '-' children_count.short_description = '# Children' def text_preview(self, obj): """Show text content preview""" if obj.text: preview = obj.text[:200] + '...' if len(obj.text) > 200 else obj.text return format_html('
{}', preview)
return '-'
text_preview.short_description = 'Text Content'