Skip to content

Commit

Permalink
Move comments for posts from post endpoint to paginated sub-endpoint …
Browse files Browse the repository at this point in the history
…'api/v1/post/<pk>/comments/'
  • Loading branch information
Termuellinator committed Oct 17, 2023
1 parent 00af911 commit 0a9e717
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 14 additions & 0 deletions apps/post/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import action

from apps.post import models, serializers, mixins, permissions

Expand Down Expand Up @@ -52,6 +53,19 @@ def list(self, request, *args, **kwargs):
.order_by('-rating', '-upvotes'))
return super().list(self, request, *args, **kwargs)

@action(detail=True, methods=['GET'], url_path='comments')
def comments(self, request, *args, **kwargs):
obj = self.get_object()
queryset = obj.comment_set.all()

page = self.paginate_queryset(queryset)
if page is not None:
serializer = serializers.CommentModelSerializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)


class CategoryViewSet(mixins.DenyDeletionOfDefaultCategoryMixin,
viewsets.ModelViewSet):
Expand Down
2 changes: 0 additions & 2 deletions apps/post/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class PostModelSerializer(serializers.ModelSerializer):
user_id = UserModelSerializer()
userUpVotes = UserModelShortSerializer(many=True)
userDownVotes = UserModelShortSerializer(many=True)
comments = CommentModelSerializer(source="comment_set", many=True)

class Meta:
model = models.Post
Expand All @@ -67,7 +66,6 @@ class Meta:
"user_id",
"cat_id",
"tags",
"comments",
"userUpVotes",
"userDownVotes",
"created_at",
Expand Down

0 comments on commit 0a9e717

Please sign in to comment.