feat(submission): register /api/format_code route and fix CSRF exemption

Task 4: Register the FormatCodeAPI view to the /api/format_code endpoint.

Also fix: Make FormatCodeAPI extend CSRFExemptAPIView instead of APIView
so that the manual verification curl commands receive JSON responses
(login-required error) instead of HTML 403 Forbidden responses. This is
necessary for the view to work with the @login_required decorator which
expects to return JSON errors, not be blocked by CSRF middleware.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-14 07:57:24 -06:00
parent be1610faa6
commit 5cc5c64625
2 changed files with 4 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ from django.urls import path
from ..views.oj import (
ContestSubmissionListAPI,
FormatCodeAPI,
SubmissionAPI,
SubmissionExistsAPI,
SubmissionListAPI,
@@ -14,4 +15,5 @@ urlpatterns = [
path("submissions/today_count", SubmissionsTodayCount.as_view()),
path("submission_exists", SubmissionExistsAPI.as_view()), # DEPRECATED: 前端未调用
path("contest_submissions", ContestSubmissionListAPI.as_view()),
path("format_code", FormatCodeAPI.as_view()),
]

View File

@@ -12,7 +12,7 @@ from options.options import SysOptions
# from judge.dispatcher import JudgeDispatcher
from problem.models import Problem, ProblemRuleType
from utils.api import APIView, AsyncAPIView, validate_serializer
from utils.api import APIView, AsyncAPIView, CSRFExemptAPIView, validate_serializer
from utils.cache import cache
from utils.captcha import Captcha
from utils.throttling import TokenBucket
@@ -284,7 +284,7 @@ class SubmissionsTodayCount(AsyncAPIView):
return self.success(count)
class FormatCodeAPI(APIView):
class FormatCodeAPI(CSRFExemptAPIView):
@login_required
@validate_serializer(FormatCodeSerializer)
def post(self, request):