Skip to content
Snippets Groups Projects
Commit 416caa89 authored by Andreas Burbach's avatar Andreas Burbach
Browse files

pagination for statement feed

parent bd0b1846
No related branches found
No related tags found
1 merge request!3Feeding chicken with chicken
Pipeline #75914 passed
...@@ -6,5 +6,6 @@ urlpatterns = [ ...@@ -6,5 +6,6 @@ urlpatterns = [
path('statements/get/<int:id>/', ShowStatement.as_view(), name="show_statement"), 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/with/hashtag/', ShowStatementsWithHashtag.as_view(), name="show_statement_with_hashtag"),
path('statements/feed/', ShowStatementFeed.as_view(), name="show_statement_feed"), 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"), path('trending/hashtag/', ShowTrendingHashtag.as_view(), name="show_trending_hashtags"),
] ]
\ No newline at end of file
...@@ -82,7 +82,6 @@ class ShowStatementFeed(APIView): ...@@ -82,7 +82,6 @@ class ShowStatementFeed(APIView):
def get(request: Request): def get(request: Request):
""" """
This is for getting the feed. This is for getting the feed.
Todo: Add pagination for infinite scrolling.
:param request: The request containing the token to identify the calling user. :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. :return: Feed for the calling user based on the actions of those the calling account follows.
""" """
...@@ -93,6 +92,47 @@ class ShowStatementFeed(APIView): ...@@ -93,6 +92,47 @@ class ShowStatementFeed(APIView):
return Response(status=status.HTTP_200_OK, data=serializer.data) 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): class ShowTrendingHashtag(APIView):
""" """
This view is for getting the five most trending hashtags. This view is for getting the five most trending hashtags.
...@@ -114,9 +154,7 @@ 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. :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: QuerySet[Dict] = HashtagTagging.objects.values('hashtag')
hashtags_counted: QuerySet[Dict] = hashtags.annotate( hashtags_counted: QuerySet[Dict] = hashtags.annotate(the_count=Count('hashtag')).order_by("-the_count")
the_count=Count('hashtag')
).order_by("-the_count")
counted: Dict = {item["hashtag"]: item["the_count"] for item in hashtags_counted} counted: Dict = {item["hashtag"]: item["the_count"] for item in hashtags_counted}
hashtags: QuerySet[Hashtag] = Hashtag.objects.filter(id__in=counted.keys()) hashtags: QuerySet[Hashtag] = Hashtag.objects.filter(id__in=counted.keys())
if not hashtags_counted: if not hashtags_counted:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment