This commit is contained in:
2026-05-26 21:25:26 -06:00
parent 8731012f47
commit 57c0572fd9
38 changed files with 1507 additions and 476 deletions

View File

@@ -1,45 +1,44 @@
from django.core.cache import cache
from django.db.models import Avg, Count
from django.db.models.functions import Round
from account.decorators import login_required
from django.db.models import Avg, Count
from django.db.models.functions import Round
from account.decorators import login_required
from comment.models import Comment
from comment.serializers import CommentSerializer, CreateCommentSerializer
from problem.models import Problem
from submission.models import JudgeStatus, Submission
from utils.api import APIView
from utils.api.api import validate_serializer
from utils.constants import CacheKey
from submission.models import JudgeStatus, Submission
from utils.api import AsyncAPIView
from utils.api.api import validate_serializer
from utils.async_helpers import async_cache_delete, async_cache_get, async_cache_set
from utils.constants import CacheKey
class CommentAPI(APIView):
class CommentAPI(AsyncAPIView):
@validate_serializer(CreateCommentSerializer)
@login_required
def post(self, request):
async def post(self, request):
data = request.data
try:
problem = Problem.objects.get(id=data["problem_id"], visible=True)
problem = await Problem.objects.aget(id=data["problem_id"], visible=True)
except Problem.DoesNotExist:
self.error("problem is not exists")
return self.error("problem is not exists")
try:
submission = (
Submission.objects.select_related("problem")
.filter(
user_id=request.user.id,
problem_id=data["problem_id"],
result__in=[JudgeStatus.ACCEPTED, JudgeStatus.AST_CHECK_FAILED],
)
.first()
submission = await (
Submission.objects.select_related("problem")
.filter(
user_id=request.user.id,
problem_id=data["problem_id"],
result__in=[JudgeStatus.ACCEPTED, JudgeStatus.AST_CHECK_FAILED],
)
except Submission.DoesNotExist:
self.error("submission is not exists or not accepted")
.afirst()
)
if not submission:
return self.error("submission is not exists or not accepted")
language = submission.language
if language == "Python3":
language = "Python"
Comment.objects.create(
await Comment.objects.acreate(
user=request.user,
problem=problem,
submission=submission,
@@ -49,32 +48,35 @@ class CommentAPI(APIView):
comprehensive_rating=data["comprehensive_rating"],
content=data["content"],
)
cache.delete(f"{CacheKey.comment_stats}:{problem.id}")
return self.success()
await async_cache_delete(f"{CacheKey.comment_stats}:{problem.id}")
return self.success()
@login_required
def get(self, request):
async def get(self, request):
problem_id = request.GET.get("problem_id")
comment = (
comment = await (
Comment.objects.select_related("problem")
.filter(user=request.user, problem_id=problem_id)
.first()
)
if comment:
return self.success(CommentSerializer(comment).data)
else:
return self.success()
.afirst()
)
if comment:
return self.success(await self.async_serialize_data(CommentSerializer, comment))
else:
return self.success()
class CommentStatisticsAPI(APIView):
def get(self, request):
problem_id = request.GET.get("problem_id")
cache_key = f"{CacheKey.comment_stats}:{problem_id}"
cached = cache.get(cache_key)
if cached is not None:
return self.success(cached)
class CommentStatisticsAPI(AsyncAPIView):
async def get(self, request):
problem_id = request.GET.get("problem_id")
cache_key = f"{CacheKey.comment_stats}:{problem_id}"
cached = await async_cache_get(cache_key)
if cached is not None:
return self.success(cached)
agg = Comment.objects.filter(problem_id=problem_id).aggregate(
from asgiref.sync import sync_to_async
agg = await sync_to_async(
Comment.objects.filter(problem_id=problem_id).aggregate
)(
count=Count("id"),
description=Round(Avg("description_rating"), 2),
difficulty=Round(Avg("difficulty_rating"), 2),
@@ -88,5 +90,5 @@ class CommentStatisticsAPI(APIView):
"difficulty": agg["difficulty"],
"comprehensive": agg["comprehensive"],
}}
cache.set(cache_key, data, 3600)
return self.success(data)
await async_cache_set(cache_key, data, 3600)
return self.success(data)