35 lines
1 KiB
Python
35 lines
1 KiB
Python
|
from rest_framework.response import Response
|
||
|
from rest_framework.views import APIView
|
||
|
from rest_framework.permissions import IsAuthenticated
|
||
|
|
||
|
from apps.archive.models import PostModel
|
||
|
|
||
|
from .serializers import PostListSerializer, PostSerializer
|
||
|
|
||
|
|
||
|
class PostListView(APIView):
|
||
|
permission_classes = [IsAuthenticated]
|
||
|
|
||
|
def get(self, request):
|
||
|
user = request.user.userprofile
|
||
|
|
||
|
if user.show_mature:
|
||
|
posts = PostModel.objects.all().order_by("-date_created")
|
||
|
else:
|
||
|
posts = PostModel.objects.filter(mature=False).order_by("-date_created")
|
||
|
serializer = PostListSerializer(posts)
|
||
|
return Response(serializer.data)
|
||
|
|
||
|
|
||
|
class PostView(APIView):
|
||
|
permission_classes = [IsAuthenticated]
|
||
|
|
||
|
def get(self, request, post_id):
|
||
|
try:
|
||
|
post_instance = PostModel.objects.get(post_id=post_id)
|
||
|
except PostModel.DoesNotExist:
|
||
|
raise NotFound(detail="Post not found.")
|
||
|
|
||
|
serializer = PostSerializer(post_instance)
|
||
|
return Response(serializer.data)
|