feat: SQL 题测试点脚本回显接口

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 19:29:51 -06:00
parent 803d6ca58d
commit deae73cd88
2 changed files with 37 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ from ..views.admin import (
ProblemFlowchartAIGen, ProblemFlowchartAIGen,
ProblemVisibleAPI, ProblemVisibleAPI,
SQLTestCasePreviewAPI, SQLTestCasePreviewAPI,
SQLTestCaseScriptsAPI,
StuckProblemsAPI, StuckProblemsAPI,
TestCaseAPI, TestCaseAPI,
TopACTrendAPI, TopACTrendAPI,
@@ -16,6 +17,7 @@ from ..views.admin import (
urlpatterns = [ urlpatterns = [
path("test_case", TestCaseAPI.as_view()), path("test_case", TestCaseAPI.as_view()),
path("sql_test_case_preview", SQLTestCasePreviewAPI.as_view()), path("sql_test_case_preview", SQLTestCasePreviewAPI.as_view()),
path("sql_test_case_scripts", SQLTestCaseScriptsAPI.as_view()),
path("problem", ProblemAPI.as_view()), path("problem", ProblemAPI.as_view()),
path("problem/visible", ProblemVisibleAPI.as_view()), path("problem/visible", ProblemVisibleAPI.as_view()),
path("problem/stuck", StuckProblemsAPI.as_view()), path("problem/stuck", StuckProblemsAPI.as_view()),

View File

@@ -690,3 +690,38 @@ class SQLTestCasePreviewAPI(APIView):
except SQLCaseError as e: except SQLCaseError as e:
return self.error(e.message) return self.error(e.message)
return self.success(display) return self.success(display)
class SQLTestCaseScriptsAPI(APIView, TestCaseZipProcessor):
@problem_permission_required
def get(self, request):
problem_id = request.GET.get("problem_id")
if not problem_id:
return self.error("Parameter error, problem_id is required")
try:
problem = Problem.objects.get(id=problem_id)
except Problem.DoesNotExist:
return self.error("Problem does not exists")
if problem.contest:
ensure_created_by(problem.contest, request.user)
else:
ensure_created_by(problem, request.user)
test_case_dir = os.path.join(settings.TEST_CASE_DIR, problem.test_case_id)
try:
with open(os.path.join(test_case_dir, "info"), encoding="utf-8") as f:
info = json.load(f)
except (OSError, json.JSONDecodeError):
return self.error("测试点信息读取失败")
if not info.get("sql"):
return self.error("该题的测试点不是 SQL 类型")
scripts = []
for name in self.filter_sql_name_list(os.listdir(test_case_dir)):
try:
with open(os.path.join(test_case_dir, name), encoding="utf-8") as f:
scripts.append({"name": name, "content": f.read()})
except OSError:
return self.error(f"测试点脚本 {name} 读取失败")
return self.success(scripts)