Commit 416caa89 authored by Andreas Burbach's avatar Andreas Burbach
Browse files

pagination for statement feed

parent bd0b1846
Pipeline #75914 passed with stages
in 4 minutes
......@@ -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
......@@ -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:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment