NDOJ/judge/admin/volunteer.py

68 lines
2 KiB
Python
Raw Normal View History

2022-07-29 08:53:35 +00:00
from operator import attrgetter
2022-05-03 02:44:14 +00:00
from django.contrib import admin
from django.utils.html import format_html
from django.urls import reverse
from django.utils.translation import gettext, gettext_lazy as _, ungettext
2022-07-29 08:53:35 +00:00
from django.forms import ModelForm
2022-05-03 02:44:14 +00:00
from judge.models import VolunteerProblemVote
2022-07-29 08:53:35 +00:00
from judge.widgets import AdminSelect2MultipleWidget
class VolunteerProblemVoteForm(ModelForm):
class Meta:
widgets = {
"types": AdminSelect2MultipleWidget,
}
2022-05-03 02:44:14 +00:00
2022-05-14 17:57:27 +00:00
2022-05-03 02:44:14 +00:00
class VolunteerProblemVoteAdmin(admin.ModelAdmin):
2022-07-29 08:53:35 +00:00
form = VolunteerProblemVoteForm
2022-05-14 17:57:27 +00:00
fields = (
"voter",
2022-07-29 08:53:35 +00:00
"problem_link",
2022-05-14 17:57:27 +00:00
"time",
"thinking_points",
"knowledge_points",
2022-07-29 08:53:35 +00:00
"types",
2022-05-14 17:57:27 +00:00
"feedback",
)
2022-07-29 08:53:35 +00:00
readonly_fields = ("time", "problem_link", "voter")
2022-05-14 17:57:27 +00:00
list_display = (
"voter",
"problem_link",
"thinking_points",
"knowledge_points",
2022-07-29 08:53:35 +00:00
"show_types",
2022-05-14 17:57:27 +00:00
"feedback",
)
2022-07-29 08:53:35 +00:00
search_fields = (
"voter__user__username",
"problem__code",
"problem__name",
)
2022-05-14 17:57:27 +00:00
date_hierarchy = "time"
2022-05-03 02:44:14 +00:00
def problem_link(self, obj):
2022-07-29 08:53:35 +00:00
if self.request.user.is_superuser:
url = reverse("admin:judge_problem_change", args=(obj.problem.id,))
else:
url = reverse("problem_detail", args=(obj.problem.code,))
return format_html(f"<a href='{url}'>{obj.problem}</a>")
2022-05-14 17:57:27 +00:00
problem_link.short_description = _("Problem")
problem_link.admin_order_field = "problem__code"
2022-07-29 08:53:35 +00:00
def show_types(self, obj):
return ", ".join(map(attrgetter("name"), obj.types.all()))
show_types.short_description = _("Types")
def get_queryset(self, request):
self.request = request
if request.user.is_superuser:
return super().get_queryset(request)
queryset = VolunteerProblemVote.objects.prefetch_related("voter")
return queryset.filter(voter=request.profile).distinct()