diff --git a/social_network/the_social_network/urls/contentUrls.py b/social_network/the_social_network/urls/contentUrls.py index 575325426a1d3e8cb10242c357789588c9e17233..1f3b310972c50e0d3fecbed718d486a72a65036b 100644 --- a/social_network/the_social_network/urls/contentUrls.py +++ b/social_network/the_social_network/urls/contentUrls.py @@ -6,5 +6,6 @@ urlpatterns = [ path('statements/get/<int:id>/', ShowStatement.as_view(), name="show_statement"), path('statements/with/hashtag/', ShowStatementsWithHashtag.as_view(), name="show_statement_with_hashtag"), path('statements/feed/', ShowStatementFeed.as_view(), name="show_statement_feed"), + path('statements/feed/pagination', ShowStatementFeedPagination.as_view(), name="show_statement_feed_pagination"), path('trending/hashtag/', ShowTrendingHashtag.as_view(), name="show_trending_hashtags"), ] \ No newline at end of file diff --git a/social_network/the_social_network/views/contentViews.py b/social_network/the_social_network/views/contentViews.py index dfc9760721e0cc22094c91e49d1ec118490912b2..0af470856a2e776639ed23791afb44d76f5a2c01 100644 --- a/social_network/the_social_network/views/contentViews.py +++ b/social_network/the_social_network/views/contentViews.py @@ -82,7 +82,6 @@ class ShowStatementFeed(APIView): def get(request: Request): """ This is for getting the feed. - Todo: Add pagination for infinite scrolling. :param request: The request containing the token to identify the calling user. :return: Feed for the calling user based on the actions of those the calling account follows. """ @@ -91,6 +90,47 @@ class ShowStatementFeed(APIView): feed: QuerySet[Statement] = Statement.objects.filter(author__in=following) serializer: StatementSerializer = StatementSerializer(instance=feed, many=True) return Response(status=status.HTTP_200_OK, data=serializer.data) + + +class ShowStatementFeedPagination(APIView): + """ + This view is for querying the feed for an calling account. + The feed is generated by the accounts the calling account is following. + To get the feed the calling account must be authenticated. + """ + authentication_classes = [TokenAuthentication] + permission_classes = (IsAuthenticated,) + + @staticmethod + def get(request: Request): + """ + This is for getting the feed with pagination. + Todo: Add pagination for infinite scrolling. + :param request: The request containing the token to identify the calling user. The page number. The size number. + :return: Feed for the calling user based on the actions of those the calling account follows. + """ + page: int = int(request.query_params.get('page', None)) + size: int = int(request.query_params.get('size', None)) + if not page or not size or page <= 0 or size <= 0: + return Response(status=status.HTTP_400_BAD_REQUEST) + + account: Account = Account.objects.get(user=request.user) + following: List[Account] = account.get_related_to() + [account] + feed: QuerySet[Statement] = Statement.objects.filter(author__in=following) + + first = (page - 1) * size + last = first + size + total: int = feed.count() + + if first >= total: + feed = [] + else: + if last >= total: + last = None + feed = feed[first:last] + + serializer: StatementSerializer = StatementSerializer(instance=feed, many=True) + return Response(status=status.HTTP_200_OK, data={"data": serializer.data, "total": total}) class ShowTrendingHashtag(APIView): @@ -114,9 +154,7 @@ class ShowTrendingHashtag(APIView): :return: 200 OK with empty or not empty data section. The data section is empty if there are not hashtags. """ hashtags: QuerySet[Dict] = HashtagTagging.objects.values('hashtag') - hashtags_counted: QuerySet[Dict] = hashtags.annotate( - the_count=Count('hashtag') - ).order_by("-the_count") + hashtags_counted: QuerySet[Dict] = hashtags.annotate(the_count=Count('hashtag')).order_by("-the_count") counted: Dict = {item["hashtag"]: item["the_count"] for item in hashtags_counted} hashtags: QuerySet[Hashtag] = Hashtag.objects.filter(id__in=counted.keys()) if not hashtags_counted: