From 30a856dcb9d7157be82cc21fbd6759adc32d7c41 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Mon, 4 Apr 2022 17:04:40 -0500 Subject: [PATCH] add vote mean and std to problem admin --- judge/admin/problem.py | 17 +++++++++++++++-- judge/models/problem.py | 1 + locale/vi/LC_MESSAGES/django.po | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/judge/admin/problem.py b/judge/admin/problem.py index 1cad785..3d81d96 100644 --- a/judge/admin/problem.py +++ b/judge/admin/problem.py @@ -3,7 +3,8 @@ from operator import attrgetter from django import forms from django.contrib import admin from django.db import transaction -from django.db.models import Q +from django.db.models import Q, Avg +from django.db.models.aggregates import StdDev from django.forms import ModelForm from django.urls import reverse_lazy from django.utils.html import format_html @@ -131,7 +132,7 @@ class ProblemAdmin(VersionAdmin): (_('Justice'), {'fields': ('banned_users',)}), (_('History'), {'fields': ('change_message',)}), ) - list_display = ['code', 'name', 'show_authors', 'points', 'is_public', 'show_public'] + list_display = ['code', 'name', 'show_authors', 'points', 'vote_mean', 'vote_std', 'is_public', 'show_public'] ordering = ['code'] search_fields = ('code', 'name', 'authors__user__username', 'curators__user__username') inlines = [LanguageLimitInline, ProblemClarificationInline, ProblemSolutionInline, ProblemTranslationInline] @@ -198,6 +199,10 @@ class ProblemAdmin(VersionAdmin): def get_queryset(self, request): queryset = Problem.objects.prefetch_related('authors__user') + queryset = queryset.annotate( + _vote_mean=Avg('problem_points_votes__points'), + _vote_std=StdDev('problem_points_votes__points') + ) if request.user.has_perm('judge.edit_all_problem'): return queryset @@ -237,6 +242,14 @@ class ProblemAdmin(VersionAdmin): return form.cleaned_data['change_message'] return super(ProblemAdmin, self).construct_change_message(request, form, *args, **kwargs) + def vote_mean(self, obj): + return round(obj._vote_mean, 1) if obj._vote_mean is not None else None + vote_mean.admin_order_field = '_vote_mean' + + def vote_std(self, obj): + return round(obj._vote_std, 1) if obj._vote_std is not None else None + vote_std.admin_order_field = '_vote_std' + class ProblemPointsVoteAdmin(admin.ModelAdmin): list_display = ('vote_points', 'voter', 'voter_rating', 'voter_point', 'problem_name', 'problem_code', 'problem_points') diff --git a/judge/models/problem.py b/judge/models/problem.py index f9e10ec..1d6fc0e 100644 --- a/judge/models/problem.py +++ b/judge/models/problem.py @@ -1,4 +1,5 @@ from operator import attrgetter +from math import sqrt from django.conf import settings from django.contrib.contenttypes.fields import GenericRelation diff --git a/locale/vi/LC_MESSAGES/django.po b/locale/vi/LC_MESSAGES/django.po index c77acf1..463ec69 100644 --- a/locale/vi/LC_MESSAGES/django.po +++ b/locale/vi/LC_MESSAGES/django.po @@ -749,7 +749,7 @@ msgstr "giới hạn thời gian" #: judge/models/contest.py:76 judge/models/problem.py:136 msgid "publicly visible" -msgstr "hiển thị công khai" +msgstr "công khai" #: judge/models/contest.py:77 msgid ""