From 1217ee3eaf224af2e586121e2f6727eda225751f Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Tue, 26 May 2026 02:19:26 -0600 Subject: [PATCH] update --- problem/serializers.py | 6 ++++++ problem/views/oj.py | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/problem/serializers.py b/problem/serializers.py index 8b5f998..701daab 100644 --- a/problem/serializers.py +++ b/problem/serializers.py @@ -170,6 +170,11 @@ class ProblemSerializer(BaseProblemSerializer): class ProblemListSerializer(BaseProblemSerializer): + has_ast_rules = serializers.SerializerMethodField() + + def get_has_ast_rules(self, obj): + return bool(obj.ast_rules) + class Meta: model = Problem fields = [ @@ -184,6 +189,7 @@ class ProblemListSerializer(BaseProblemSerializer): "contest", "allow_flowchart", "show_flowchart", + "has_ast_rules", ] diff --git a/problem/views/oj.py b/problem/views/oj.py index 795c8a8..0cdded0 100644 --- a/problem/views/oj.py +++ b/problem/views/oj.py @@ -2,7 +2,7 @@ import random from datetime import datetime from django.core.cache import cache -from django.db.models import Count, Q +from django.db.models import BooleanField, Case, Count, Q, Value, When from django.db.models.functions import ExtractYear from account.decorators import check_contest_permission @@ -128,10 +128,18 @@ class ProblemAPI(APIView): # 排序 sort = request.GET.get("sort") - if sort and sort != "flowchart": - problems = problems.order_by(sort) - if sort and sort == "flowchart": + if sort == "flowchart": problems = problems.order_by("-allow_flowchart", "-show_flowchart", "-create_time") + elif sort == "ast": + problems = problems.annotate( + _has_ast=Case( + When(ast_rules__isnull=False, then=Value(True)), + default=Value(False), + output_field=BooleanField(), + ) + ).order_by("-_has_ast", "-create_time") + elif sort: + problems = problems.order_by(sort) # 根据profile 为做过的题目添加标记 data = self.paginate_data(request, problems, ProblemListSerializer)