Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] pagination 구현 #249

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/search/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from rest_framework.pagination import CursorPagination
from rest_framework.response import Response
from rest_framework import status

class ScrollPagination(CursorPagination):
page_size = 2
ordering = '-created_at'
cursor_query_param = 'cursor'

def get_paginated_response(self, data):
if self.get_previous_link() is None:
return Response(
{
"meta": {"code": 301, "message": "OK"},
"data": {
"next": self.get_next_link(),
"previous": self.get_previous_link(),
"products": data,
},
},
status=status.HTTP_301_MOVED_PERMANENTLY,
)
else:
return Response(
{
"meta": {"code": 200, "message": "OK"},
"data": {
"next": self.get_next_link(),
"previous": self.get_previous_link(),
"products": data,
},
},
status=status.HTTP_200_OK,
)
45 changes: 38 additions & 7 deletions backend/search/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework import status
from summary.models import Summary
from django.db.models import Q
Expand All @@ -11,9 +11,13 @@
KeywordSearchSerializer
)

from .pagination import ScrollPagination

from drf_yasg.utils import swagger_auto_schema

class CategorySearchAPIView(APIView):
class CategorySearchAPIView(ListAPIView):
pagination_class = ScrollPagination

@swagger_auto_schema(operation_summary="카테고리 검색", query_serializer=MainPageCategorySerializer, responses={"200":SearchSerializer})
def get(self, request):
user_id = request.query_params.get('user_id')
Expand All @@ -36,6 +40,13 @@ def get(self, request):
Q(user_id=user_id),
Q(category__category=category)
).prefetch_related('category_set', 'summary_by_time_set')

# 페이징 처리
page = self.paginate_queryset(summaries)

if page is not None:
serializer = SearchSerializer(page, many=True)
return self.get_paginated_response(serializer.data)

# 각 Summary 객체에 대해 정보 추출
for summary in summaries:
Expand Down Expand Up @@ -79,12 +90,15 @@ def get(self, request):

return Response({'summaries': result}, status=status.HTTP_200_OK)

class KeywordSearchView(APIView):
@swagger_auto_schema(operation_summary="키워드 검색", query_serializer=KeywordSearchSerializer, responses={"200":SearchSerializer})
class KeywordSearchView(ListAPIView):
pagination_class = ScrollPagination

@swagger_auto_schema(operation_summary="키워드 검색", query_serializer=KeywordSearchSerializer, responses={"200":SearchSerializer})
def get(self, request):
user_id = request.query_params.get('user_id')
keyword = request.query_params.get('keyword')
category = request.query_params.get('category')


keyword_query = Q(youtube_title__icontains=keyword)|\
Q(youtube_channel__icontains=keyword)|\
Expand All @@ -108,7 +122,14 @@ def get(self, request):
query = keyword_query & user_query & category_query & not_deleted_query

summaries = Summary.objects.filter(query).distinct().prefetch_related('category_set', 'summary_by_time_set')
print(user_id)

# 페이징 처리
page = self.paginate_queryset(summaries)

if page is not None:
serializer = SearchSerializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = SearchSerializer(summaries, many=True)

if not serializer.data:
Expand All @@ -121,7 +142,9 @@ def get(self, request):

return Response({'summaries': serializer.data}, status=status.HTTP_200_OK)

class ChannelSearchView(APIView):
class ChannelSearchView(ListAPIView):
pagination_class = ScrollPagination

@swagger_auto_schema(operation_summary="채널 검색", query_serializer=ChannelSearchSerializer, responses={"200":SearchSerializer})
def get(self, request):
user_id = request.query_params.get('user_id')
Expand All @@ -135,7 +158,15 @@ def get(self, request):
Q(deleted_at__isnull=True),
Q(user_id=user_id),
Q(youtube_channel=channel)
).prefetch_related('category_set', 'summary_by_time_set')
).prefetch_related('category_set', 'summary_by_time_set').order_by('-created_at')

result = []

page = self.paginate_queryset(summaries)

if page is not None:
serializer = SearchSerializer(page, many=True)
return self.get_paginated_response(serializer.data)

# 각 Summary 객체에 대해 정보 추출
for summary in summaries:
Expand Down
Loading