get my commet

This commit is contained in:
2024-07-01 00:47:34 +08:00
parent bd59913d33
commit 5bcccf12e4
2 changed files with 20 additions and 3 deletions

View File

@@ -13,4 +13,10 @@ class CreateCommentSerializer(serializers.Serializer):
class CommentSerializer(serializers.ModelSerializer): class CommentSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Comment model = Comment
fields = "__all__" fields = [
"comprehensive_rating",
"description_rating",
"difficulty_rating",
"content",
"create_time",
]

View File

@@ -1,11 +1,11 @@
from django.db.models import Avg, Count from django.db.models import Avg
from django.db.models.functions import Round from django.db.models.functions import Round
from comment.models import Comment from comment.models import Comment
from problem.models import Problem from problem.models import Problem
from utils.api import APIView from utils.api import APIView
from account.decorators import login_required from account.decorators import login_required
from utils.api.api import validate_serializer from utils.api.api import validate_serializer
from comment.serializers import CreateCommentSerializer from comment.serializers import CreateCommentSerializer, CommentSerializer
from submission.models import Submission, JudgeStatus from submission.models import Submission, JudgeStatus
@@ -50,11 +50,22 @@ class CommentAPI(APIView):
def get(self, request): def get(self, request):
problem_id = request.GET.get("problem_id") problem_id = request.GET.get("problem_id")
my_comment = request.GET.get("my_comment")
if my_comment:
comment = (
Comment.objects.select_related("problem")
.filter(user=request.user, problem_id=problem_id, visible=True)
.first()
)
return self.success(CommentSerializer(comment).data)
comments = Comment.objects.select_related("problem").filter( comments = Comment.objects.select_related("problem").filter(
problem_id=problem_id, visible=True problem_id=problem_id, visible=True
) )
if comments.count() == 0: if comments.count() == 0:
return self.success() return self.success()
count = comments.count() count = comments.count()
rating = comments.aggregate( rating = comments.aggregate(
description=Round(Avg("description_rating"), 2), description=Round(Avg("description_rating"), 2),