fix
This commit is contained in:
19
reaction/services.py
Normal file
19
reaction/services.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from django.db.models import Count
|
||||
|
||||
from reaction.models import Reaction, ReactionType
|
||||
|
||||
# 并列票数时按 ReactionType 的定义顺序取靠前的那个,跟前端 REACTIONS 的顺序一致
|
||||
TYPE_ORDER = {t.value: index for index, t in enumerate(ReactionType)}
|
||||
|
||||
|
||||
def get_top_reactions(problem_ids):
|
||||
"""返回 {problem_id: {"type": ..., "count": ...}},只包含有评价的题目。"""
|
||||
if not problem_ids:
|
||||
return {}
|
||||
top = {}
|
||||
rows = Reaction.objects.filter(problem_id__in=problem_ids).values("problem_id", "type").annotate(count=Count("id"))
|
||||
for row in rows:
|
||||
current = top.get(row["problem_id"])
|
||||
if current is None or (-row["count"], TYPE_ORDER[row["type"]]) < (-current["count"], TYPE_ORDER[current["type"]]):
|
||||
top[row["problem_id"]] = {"type": row["type"], "count": row["count"]}
|
||||
return top
|
||||
@@ -1,7 +0,0 @@
|
||||
from django.urls import path
|
||||
|
||||
from ..views.admin import ReactionStatsAPI
|
||||
|
||||
urlpatterns = [
|
||||
path("reaction", ReactionStatsAPI.as_view()),
|
||||
]
|
||||
@@ -1,25 +0,0 @@
|
||||
from django.db.models import Count, F, Max, Q
|
||||
|
||||
from account.decorators import super_admin_required
|
||||
from reaction.models import Reaction, ReactionType
|
||||
from utils.api import APIView
|
||||
|
||||
|
||||
class ReactionStatsAPI(APIView):
|
||||
@super_admin_required
|
||||
def get(self, request):
|
||||
queryset = (
|
||||
Reaction.objects.values(pid=F("problem___id"), title=F("problem__title"))
|
||||
.annotate(users=Count("user", distinct=True))
|
||||
.annotate(last_time=Max("create_time"))
|
||||
.annotate(**{t.value: Count("id", filter=Q(type=t.value)) for t in ReactionType})
|
||||
.order_by("-users")
|
||||
)
|
||||
|
||||
problem_id = request.GET.get("problem")
|
||||
if problem_id:
|
||||
queryset = queryset.filter(problem___id__iexact=problem_id, problem__contest_id__isnull=True)
|
||||
|
||||
data = self.paginate_data(request, queryset)
|
||||
data["results"] = list(data["results"])
|
||||
return self.success(data)
|
||||
Reference in New Issue
Block a user