This commit is contained in:
@@ -3,6 +3,7 @@ from unfold.admin import ModelAdmin
|
||||
from unfold.decorators import display
|
||||
from .models import Competition, CompetitionEnrollment, ScoreDimension, Project, ProjectFile, Score, Comment, ScoreFormula
|
||||
|
||||
|
||||
class ScoreDimensionInline(admin.TabularInline):
|
||||
model = ScoreDimension
|
||||
extra = 1
|
||||
@@ -14,17 +15,31 @@ class ScoreDimensionInline(admin.TabularInline):
|
||||
preview = obj.get_formula_preview()
|
||||
return preview if preview else "-"
|
||||
|
||||
|
||||
class ScoreFormulaInline(admin.TabularInline):
|
||||
model = ScoreFormula
|
||||
extra = 1
|
||||
tab = True
|
||||
fields = ('name', 'formula', 'is_active', 'is_default')
|
||||
|
||||
@admin.display(description="公式预览")
|
||||
def formula_preview_display(self, obj):
|
||||
preview = obj.get_formula_preview()
|
||||
return preview[:50] + '...' if len(preview) > 50 else preview if preview else '-'
|
||||
|
||||
|
||||
class ProjectFileInline(admin.TabularInline):
|
||||
model = ProjectFile
|
||||
extra = 0
|
||||
tab = True
|
||||
|
||||
|
||||
@admin.register(Competition)
|
||||
class CompetitionAdmin(ModelAdmin):
|
||||
list_display = ['title', 'status', 'allow_contestant_grading', 'start_time', 'end_time', 'is_active', 'created_at']
|
||||
list_filter = ['status', 'allow_contestant_grading', 'is_active']
|
||||
search_fields = ['title', 'description']
|
||||
inlines = [ScoreDimensionInline]
|
||||
inlines = [ScoreDimensionInline, ScoreFormulaInline]
|
||||
|
||||
fieldsets = (
|
||||
('基本信息', {
|
||||
@@ -37,10 +52,6 @@ class CompetitionAdmin(ModelAdmin):
|
||||
('时间和状态', {
|
||||
'fields': ('start_time', 'end_time', 'status', 'project_visibility', 'allow_contestant_grading', 'is_active')
|
||||
}),
|
||||
('评分计算设置', {
|
||||
'fields': ('score_calculation_type', 'custom_score_formula'),
|
||||
'description': '配置得分计算方式:默认模式使用(维度分数×权重)求和后取评委平均;自定义算式使用下方公式直接计算最终得分。变量格式: dimension_维度ID,如 dimension_1, dimension_2'
|
||||
}),
|
||||
)
|
||||
|
||||
actions = ['make_published', 'make_ended']
|
||||
@@ -53,6 +64,13 @@ class CompetitionAdmin(ModelAdmin):
|
||||
queryset.update(status='ended')
|
||||
make_ended.short_description = "结束选中比赛"
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('competition/admin/css/competition-admin.css',)
|
||||
}
|
||||
js = ('competition/admin/js/competition-admin.js',)
|
||||
|
||||
|
||||
@admin.register(CompetitionEnrollment)
|
||||
class CompetitionEnrollmentAdmin(ModelAdmin):
|
||||
list_display = ['competition', 'user_info_display', 'role', 'status', 'created_at']
|
||||
@@ -77,6 +95,7 @@ class CompetitionEnrollmentAdmin(ModelAdmin):
|
||||
queryset.update(status='rejected')
|
||||
reject_enrollment.short_description = "拒绝申请"
|
||||
|
||||
|
||||
@admin.register(Project)
|
||||
class ProjectAdmin(ModelAdmin):
|
||||
list_display = ['id', 'title', 'competition', 'contestant_info_display', 'status', 'final_score', 'created_at']
|
||||
@@ -108,6 +127,7 @@ class ProjectAdmin(ModelAdmin):
|
||||
nickname = user.nickname or "无昵称"
|
||||
return f"{phone} ({nickname})"
|
||||
|
||||
|
||||
@admin.register(Score)
|
||||
class ScoreAdmin(ModelAdmin):
|
||||
list_display = ['project', 'judge_info_display', 'dimension', 'score', 'created_at']
|
||||
@@ -124,6 +144,7 @@ class ScoreAdmin(ModelAdmin):
|
||||
nickname = user.nickname or "无昵称"
|
||||
return f"{phone} ({nickname})"
|
||||
|
||||
|
||||
@admin.register(Comment)
|
||||
class CommentAdmin(ModelAdmin):
|
||||
list_display = ['project', 'judge_info_display', 'content_preview', 'created_at']
|
||||
@@ -146,10 +167,6 @@ class CommentAdmin(ModelAdmin):
|
||||
|
||||
|
||||
class ScoreFormulaAdmin(ModelAdmin):
|
||||
"""
|
||||
评分公式管理
|
||||
提供可视化公式编辑功能
|
||||
"""
|
||||
list_display = ['name', 'competition', 'formula_preview_display', 'is_active', 'is_default', 'created_at']
|
||||
list_filter = ['competition', 'is_active', 'is_default']
|
||||
search_fields = ['name', 'description', 'formula', 'competition__title']
|
||||
@@ -173,25 +190,6 @@ class ScoreFormulaAdmin(ModelAdmin):
|
||||
preview = obj.get_formula_preview()
|
||||
return preview[:100] + '...' if len(preview) > 100 else preview if preview else '-'
|
||||
|
||||
def get_form_kwargs(self, request, *args, **kwargs):
|
||||
kwargs = super().get_form_kwargs(request, *args, **kwargs)
|
||||
return kwargs
|
||||
|
||||
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
|
||||
extra_context = extra_context or {}
|
||||
|
||||
if request.method == 'GET' and not object_id:
|
||||
competition_id = request.GET.get('competition')
|
||||
if competition_id:
|
||||
try:
|
||||
from .models import ScoreDimension
|
||||
dimensions = ScoreDimension.objects.filter(competition_id=competition_id)
|
||||
extra_context['dimensions'] = dimensions
|
||||
except:
|
||||
pass
|
||||
|
||||
return super().changeform_view(request, object_id, form_url, extra_context)
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('competition/admin/css/formula-editor.css',)
|
||||
|
||||
Reference in New Issue
Block a user