from django.contrib import admin
from django.utils.html import format_html
from quiz.models import Question
from .option_inline import OptionInline
@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
"""Admin interface for Questions"""
list_display = ['id', 'question_preview', 'exam', 'correct_answer', 'option_count', 'file_source', 'updated_at']
list_filter = ['exam__course', 'exam', 'created_at', 'updated_at']
search_fields = ['text', 'file_path', 'correct_answer']
readonly_fields = ['file_path', 'file_mtime', 'created_at', 'updated_at', 'formatted_mtime']
fieldsets = [
('Question Content', {
'fields': ['exam', 'text', 'correct_answer']
}),
('File Tracking', {
'fields': ['file_path', 'file_mtime', 'formatted_mtime'],
'classes': ['collapse']
}),
('Timestamps', {
'fields': ['created_at', 'updated_at'],
'classes': ['collapse']
}),
]
inlines = [OptionInline]
def question_preview(self, obj):
"""Show question text preview"""
return obj.text[:60] + '...' if len(obj.text) > 60 else obj.text
question_preview.short_description = 'Question'
def option_count(self, obj):
"""Show number of options"""
return obj.options.count()
option_count.short_description = '# Options'
def file_source(self, obj):
"""Show file path with folder highlight"""
if obj.file_path:
parts = obj.file_path.split('/')
if len(parts) > 1:
folder = parts[-2]
filename = parts[-1]
return format_html('{}/{}', folder, filename)
return obj.file_path or '-'
file_source.short_description = 'Source File'
def formatted_mtime(self, obj):
"""Show formatted modification time"""
if obj.file_mtime:
from datetime import datetime
dt = datetime.fromtimestamp(obj.file_mtime)
return dt.strftime('%Y-%m-%d %H:%M:%S')
return '-'
formatted_mtime.short_description = 'File Modified'