50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
|
|
from django.shortcuts import render
|
||
|
|
from rest_framework import viewsets, permissions, status
|
||
|
|
from rest_framework.response import Response
|
||
|
|
from rest_framework.decorators import action
|
||
|
|
from django.contrib.contenttypes.models import ContentType
|
||
|
|
from .models import Article, Comment, Like, Rating, Favorite
|
||
|
|
from .serializers import ArticleSerializer, CommentSerializer, RatingSerializer
|
||
|
|
|
||
|
|
|
||
|
|
class ArticleViewSet(viewsets.ModelViewSet):
|
||
|
|
queryset = Article.objects.filter(publish_status='published')
|
||
|
|
serializer_class = ArticleSerializer
|
||
|
|
permission_classes = [permissions.AllowAny]
|
||
|
|
|
||
|
|
def get_queryset(self):
|
||
|
|
queryset = Article.objects.all()
|
||
|
|
region_id = self.request.query_params.get('region')
|
||
|
|
content_type = self.request.query_params.get('type')
|
||
|
|
|
||
|
|
if region_id:
|
||
|
|
queryset = queryset.filter(region_id=region_id)
|
||
|
|
if content_type:
|
||
|
|
queryset = queryset.filter(content_type=content_type)
|
||
|
|
|
||
|
|
return queryset
|
||
|
|
|
||
|
|
def perform_create(self, serializer):
|
||
|
|
article = serializer.save(author=self.request.user)
|
||
|
|
article.submit_for_moderator_review()
|
||
|
|
|
||
|
|
|
||
|
|
class CommentViewSet(viewsets.ModelViewSet):
|
||
|
|
queryset = Comment.objects.filter(is_visible=True)
|
||
|
|
serializer_class = CommentSerializer
|
||
|
|
permission_classes = [permissions.AllowAny]
|
||
|
|
|
||
|
|
def perform_create(self, serializer):
|
||
|
|
comment = serializer.save(author=self.request.user)
|
||
|
|
# 提交 AI 审核
|
||
|
|
comment.save()
|
||
|
|
|
||
|
|
|
||
|
|
class RatingViewSet(viewsets.ModelViewSet):
|
||
|
|
queryset = Rating.objects.all()
|
||
|
|
serializer_class = RatingSerializer
|
||
|
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
||
|
|
|
||
|
|
def perform_create(self, serializer):
|
||
|
|
serializer.save(user=self.request.user)
|