feat: 添加批注功能
后端: - Comment 模型(支持日记/任务/经验三种内容类型) - CommentSerializer 和 CommentViewSet - API: /api/comments/ - 批注 CRUD - API: /api/comments/by_content/?content_type=diary&object_id=1 - 按内容获取批注 - 日记/任务/经验序列化器嵌套显示批注 前端: - 批注样式(comments-section, comment-item) - 添加批注输入框 使用方式: - 北极星可以在任何日记/任务/经验下添加批注 - 批注会显示在内容下方 - 支持查看历史批注
This commit is contained in:
@@ -2,8 +2,11 @@ from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from django.utils import timezone
|
||||
from .models import DiaryEntry, DailyProgress, Experience, Task
|
||||
from .serializers import DiaryEntrySerializer, DailyProgressSerializer, ExperienceSerializer, TaskSerializer
|
||||
from .models import DiaryEntry, DailyProgress, Experience, Task, Comment
|
||||
from .serializers import (
|
||||
DiaryEntrySerializer, DailyProgressSerializer, ExperienceSerializer,
|
||||
TaskSerializer, CommentSerializer
|
||||
)
|
||||
|
||||
class DiaryEntryViewSet(viewsets.ModelViewSet):
|
||||
queryset = DiaryEntry.objects.all()
|
||||
@@ -146,3 +149,24 @@ class TaskViewSet(viewsets.ModelViewSet):
|
||||
task = self.get_object()
|
||||
task.mark_completed()
|
||||
return Response(TaskSerializer(task).data)
|
||||
|
||||
|
||||
class CommentViewSet(viewsets.ModelViewSet):
|
||||
queryset = Comment.objects.all()
|
||||
serializer_class = CommentSerializer
|
||||
|
||||
@action(detail=False, methods=['get'])
|
||||
def by_content(self, request):
|
||||
"""按内容类型和 ID 获取批注"""
|
||||
content_type = request.query_params.get('content_type')
|
||||
object_id = request.query_params.get('object_id')
|
||||
|
||||
if content_type and object_id:
|
||||
comments = Comment.objects.filter(
|
||||
content_type=content_type,
|
||||
object_id=object_id
|
||||
)
|
||||
serializer = self.get_serializer(comments, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
return Response([])
|
||||
|
||||
Reference in New Issue
Block a user