Add: posts api app
This commit is contained in:
parent
141e08f0f4
commit
a5b0580be6
6 changed files with 197 additions and 0 deletions
34
backend/api/posts/views.py
Normal file
34
backend/api/posts/views.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
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)
|
Loading…
Add table
Add a link
Reference in a new issue