添加f评论API

This commit is contained in:
2024-07-02 21:12:06 +08:00
parent 77cf8db4c4
commit ee10972d6e
7 changed files with 65 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-07-02 12:37
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('comment', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='comment',
name='visible',
),
]

View File

@@ -35,7 +35,6 @@ class Comment(models.Model):
verbose_name="综合的分数",
)
content = models.TextField(null=True, blank=True)
visible = models.BooleanField(default=True)
create_time = models.DateTimeField(auto_now_add=True)
class Meta:

View File

@@ -20,3 +20,11 @@ class CommentSerializer(serializers.ModelSerializer):
"content",
"create_time",
]
class CommentListSerializer(serializers.ModelSerializer):
problem = serializers.SlugRelatedField(read_only=True, slug_field="_id")
class Meta:
model = Comment
fields = "__all__"

8
comment/urls/admin.py Normal file
View File

@@ -0,0 +1,8 @@
from django.urls import path
from ..views.admin import CommentAPI
urlpatterns = [
path("comment", CommentAPI.as_view()),
]

29
comment/views/admin.py Normal file
View File

@@ -0,0 +1,29 @@
from account.decorators import super_admin_required
from comment.serializers import CommentListSerializer
from problem.models import Problem
from utils.api import APIView
from comment.models import Comment
class CommentAPI(APIView):
@super_admin_required
def get(self, request):
comments = Comment.objects.select_related("problem").exclude(content="")
problem_id = request.GET.get("problem_id")
if problem_id:
try:
# 这里如果题目不可见,也需要显示该题目的评论
problem = Problem.objects.get(_id=problem_id, contest_id__isnull=True)
except Problem.DoesNotExist:
return self.error("Problem doesn't exist")
comments = comments.filter(problem=problem)
return self.success(
self.paginate_data(request, comments, CommentListSerializer)
)
@super_admin_required
def delete(self, request):
id = request.GET.get("id")
if id:
Comment.objects.filter(id=id).delete()
return self.success()

View File

@@ -53,7 +53,7 @@ class CommentAPI(APIView):
problem_id = request.GET.get("problem_id")
comment = (
Comment.objects.select_related("problem")
.filter(user=request.user, problem_id=problem_id, visible=True)
.filter(user=request.user, problem_id=problem_id)
.first()
)
if comment:
@@ -66,7 +66,7 @@ class CommentStatisticsAPI(APIView):
def get(self, request):
problem_id = request.GET.get("problem_id")
comments = Comment.objects.select_related("problem").filter(
problem_id=problem_id, visible=True
problem_id=problem_id
)
if comments.count() == 0:
return self.success()