添加f评论API
This commit is contained in:
17
comment/migrations/0002_remove_comment_visible.py
Normal file
17
comment/migrations/0002_remove_comment_visible.py
Normal 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',
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -35,7 +35,6 @@ class Comment(models.Model):
|
|||||||
verbose_name="综合的分数",
|
verbose_name="综合的分数",
|
||||||
)
|
)
|
||||||
content = models.TextField(null=True, blank=True)
|
content = models.TextField(null=True, blank=True)
|
||||||
visible = models.BooleanField(default=True)
|
|
||||||
create_time = models.DateTimeField(auto_now_add=True)
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@@ -20,3 +20,11 @@ class CommentSerializer(serializers.ModelSerializer):
|
|||||||
"content",
|
"content",
|
||||||
"create_time",
|
"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
8
comment/urls/admin.py
Normal 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
29
comment/views/admin.py
Normal 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()
|
||||||
@@ -53,7 +53,7 @@ class CommentAPI(APIView):
|
|||||||
problem_id = request.GET.get("problem_id")
|
problem_id = request.GET.get("problem_id")
|
||||||
comment = (
|
comment = (
|
||||||
Comment.objects.select_related("problem")
|
Comment.objects.select_related("problem")
|
||||||
.filter(user=request.user, problem_id=problem_id, visible=True)
|
.filter(user=request.user, problem_id=problem_id)
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
if comment:
|
if comment:
|
||||||
@@ -66,7 +66,7 @@ class CommentStatisticsAPI(APIView):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
problem_id = request.GET.get("problem_id")
|
problem_id = request.GET.get("problem_id")
|
||||||
comments = Comment.objects.select_related("problem").filter(
|
comments = Comment.objects.select_related("problem").filter(
|
||||||
problem_id=problem_id, visible=True
|
problem_id=problem_id
|
||||||
)
|
)
|
||||||
if comments.count() == 0:
|
if comments.count() == 0:
|
||||||
return self.success()
|
return self.success()
|
||||||
|
|||||||
@@ -16,4 +16,5 @@ urlpatterns = [
|
|||||||
path("api/admin/", include("utils.urls")),
|
path("api/admin/", include("utils.urls")),
|
||||||
path("api/", include("message.urls.oj")),
|
path("api/", include("message.urls.oj")),
|
||||||
path("api/", include("comment.urls.oj")),
|
path("api/", include("comment.urls.oj")),
|
||||||
|
path("api/admin", include("comment.urls.admin")),
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user