fix comment api
This commit is contained in:
17
comment/migrations/0002_alter_comment_options.py
Normal file
17
comment/migrations/0002_alter_comment_options.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Generated by Django 5.0.6 on 2024-06-30 13:51
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('comment', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name='comment',
|
||||||
|
options={'ordering': ('-create_time',)},
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -42,6 +42,6 @@ class Comment(models.Model):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = "comment"
|
db_table = "comment"
|
||||||
ordering = ("create_time",)
|
ordering = ("-create_time",)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class CreateCommentSerializer(serializers.Serializer):
|
|||||||
description_rating = serializers.IntegerField()
|
description_rating = serializers.IntegerField()
|
||||||
difficulty_rating = serializers.IntegerField()
|
difficulty_rating = serializers.IntegerField()
|
||||||
comprehensive_rating = serializers.IntegerField()
|
comprehensive_rating = serializers.IntegerField()
|
||||||
content = serializers.CharField()
|
content = serializers.CharField(required=False, allow_blank=True)
|
||||||
|
|
||||||
|
|
||||||
class CommentSerializer(serializers.ModelSerializer):
|
class CommentSerializer(serializers.ModelSerializer):
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from django.db.models import Avg
|
from django.db.models import Avg
|
||||||
|
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
|
||||||
@@ -14,7 +15,7 @@ class CommentAPI(APIView):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
data = request.data
|
data = request.data
|
||||||
try:
|
try:
|
||||||
problem = Problem.objects.get(id=data.problem_id, visible=True)
|
problem = Problem.objects.get(id=data["problem_id"], visible=True)
|
||||||
except Problem.DoesNotExist:
|
except Problem.DoesNotExist:
|
||||||
self.error("problem is not exists")
|
self.error("problem is not exists")
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ class CommentAPI(APIView):
|
|||||||
Submission.objects.select_related("problem")
|
Submission.objects.select_related("problem")
|
||||||
.filter(
|
.filter(
|
||||||
user_id=request.user.id,
|
user_id=request.user.id,
|
||||||
problem_id=data.problem_id,
|
problem_id=data["problem_id"],
|
||||||
result=JudgeStatus.ACCEPTED,
|
result=JudgeStatus.ACCEPTED,
|
||||||
)
|
)
|
||||||
.first()
|
.first()
|
||||||
@@ -44,10 +45,10 @@ class CommentAPI(APIView):
|
|||||||
submission=submission,
|
submission=submission,
|
||||||
problem_solved=problem_solved,
|
problem_solved=problem_solved,
|
||||||
language=language,
|
language=language,
|
||||||
description_rating=data.description_rating,
|
description_rating=data["description_rating"],
|
||||||
difficulty_rating=data.difficulty_rating,
|
difficulty_rating=data["difficulty_rating"],
|
||||||
comprehensive_rating=data.comprehensive_rating,
|
comprehensive_rating=data["comprehensive_rating"],
|
||||||
content=data.content,
|
content=data["content"],
|
||||||
)
|
)
|
||||||
return self.success()
|
return self.success()
|
||||||
|
|
||||||
@@ -59,11 +60,9 @@ class CommentAPI(APIView):
|
|||||||
if comments.count() == 0:
|
if comments.count() == 0:
|
||||||
return self.success()
|
return self.success()
|
||||||
rating = comments.aggregate(
|
rating = comments.aggregate(
|
||||||
description=Avg("description_rating"),
|
description=Round(Avg("description_rating"), 2),
|
||||||
difficulty=Avg("difficulty_rating"),
|
difficulty=Round(Avg("difficulty_rating"), 2),
|
||||||
comprehensive=Avg("comprehensive_rating"),
|
comprehensive=Round(Avg("comprehensive_rating"), 2),
|
||||||
)
|
)
|
||||||
contents = comments.filter(content__isnull=False).values_list(
|
contents = comments.exclude(content="").values_list("content", flat=True)
|
||||||
"content", flat=True
|
return self.success({"rating": rating, "contents": list(contents)})
|
||||||
)
|
|
||||||
return self.success({rating: rating, contents: contents})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user