All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from django.contrib import admin
|
|
from django.utils.safestring import mark_safe
|
|
from quiz.models import QuizUser
|
|
|
|
|
|
@admin.register(QuizUser)
|
|
class QuizUserAdmin(admin.ModelAdmin):
|
|
"""Admin interface for Quiz Users"""
|
|
list_display = ['id', 'session_preview', 'result_count', 'score_percentage', 'created_at']
|
|
list_filter = ['created_at']
|
|
search_fields = ['session_key']
|
|
readonly_fields = ['session_key', 'created_at', 'full_session_key']
|
|
fieldsets = [
|
|
('User Info', {
|
|
'fields': ['full_session_key', 'created_at']
|
|
}),
|
|
]
|
|
|
|
def session_preview(self, obj):
|
|
"""Show session key preview"""
|
|
return f"{obj.session_key[:12]}..."
|
|
session_preview.short_description = 'Session'
|
|
|
|
def result_count(self, obj):
|
|
"""Show number of quiz results"""
|
|
return obj.results.count()
|
|
result_count.short_description = '# Answers'
|
|
|
|
def score_percentage(self, obj):
|
|
"""Show score percentage"""
|
|
total = obj.results.count()
|
|
if total == 0:
|
|
return '-'
|
|
correct = obj.results.filter(is_correct=True).count()
|
|
percentage = (correct / total * 100)
|
|
color = 'green' if percentage >= 70 else 'orange' if percentage >= 50 else 'red'
|
|
return mark_safe(
|
|
f'<span style="color: {color}; font-weight: bold;">{percentage:.1f}%</span> ({correct}/{total})'
|
|
)
|
|
score_percentage.short_description = 'Score'
|
|
|
|
def full_session_key(self, obj):
|
|
"""Show full session key"""
|
|
return obj.session_key
|
|
full_session_key.short_description = 'Full Session Key'
|
|
|