Files
PORTAL_COD2/src/back/tests/admin.py
T

52 lines
1.8 KiB
Python

from django.contrib import admin
from .models import Test, TestAnswer, TestQuestion, TestAttempt, DictEmployee, Testing, TestFile
import nested_admin
class TestAnswerInline(nested_admin.NestedStackedInline):
model = TestAnswer
extra = 0
fields = ('name', 'file', 'right_wrong')
class TestQuestionInline(nested_admin.NestedStackedInline):
model = TestQuestion
extra = 0
inlines = [TestAnswerInline]
fields = ('name', 'file')
class TestFileInline(nested_admin.NestedStackedInline):
model = TestFile
extra = 0
fields = ('file',)
class TestAdmin(nested_admin.NestedModelAdmin):
inlines = [TestFileInline, TestQuestionInline]
class TestAttemptInline(admin.TabularInline):
model = TestAttempt
extra = 0 # не показывать пустые формы
fields = ('score', 'attempt_number', 'completion_date')
readonly_fields = ('score', 'attempt_number', 'completion_date')
can_delete = False
@admin.register(Testing)
class TestingAdmin(admin.ModelAdmin):
# list_display = ('testing_user', 'testing_test', 'score', 'completion_date')
list_display = ('get_user_fio', 'test', 'get_score', 'test_passed', 'completion_date')
fields = ('user','test', 'test_passed', 'completion_date')
inlines = [TestAttemptInline]
def get_user_fio(self, obj):
return obj.user.user.fio
def get_score(self, obj):
last_attempt = TestAttempt.objects.filter(testing=obj).order_by('-attempt_number').first()
if last_attempt:
return last_attempt.score
else:
return None
get_user_fio.admin_order_field = 'user__user__fio'
get_user_fio.short_description = 'Пользователь'
get_score.short_description = 'Результат'
admin.site.register(Test, TestAdmin)