def get(self, request, *args, **kwargs): # pylint: disable=unused-argument """Get front page posts""" listing_params = get_listing_params(self.request) api = Api(user=self.request.user) paginated_posts = api.front_page(listing_params) pagination, posts = get_pagination_and_reddit_obj_list( paginated_posts, listing_params) users = lookup_users_for_posts(posts) posts = proxy_posts([ post for post in posts if post.author and post.author.name in users ]) subscriptions = lookup_subscriptions_for_posts(posts, self.request.user) return Response({ "posts": PostSerializer( posts, context={ **self.get_serializer_context(), "users": users, "post_subscriptions": subscriptions, }, many=True, ).data, "pagination": pagination, })
def get(self, request, *args, **kwargs): """Get list for posts and attach User objects to them""" with translate_praw_exceptions(request.user): listing_params = get_listing_params(self.request) api = Api(user=request.user) paginated_posts = api.list_posts(self.kwargs["channel_name"], listing_params) pagination, posts = get_pagination_and_reddit_obj_list( paginated_posts, listing_params) users = lookup_users_for_posts(posts) posts = proxy_posts([ post for post in posts if post.author and post.author.name in users ]) subscriptions = lookup_subscriptions_for_posts(posts, request.user) return Response({ "posts": PostSerializer( posts, many=True, context={ **self.get_serializer_context(), "users": users, "post_subscriptions": subscriptions, }, ).data, "pagination": pagination, })
def test_get_pagination_and_posts_empty_page(mocker): """Test that we get an empty pagination and posts if there is no data""" posts = mocker.Mock() # pylint: disable=protected-access posts._next_batch.side_effect = StopIteration() assert get_pagination_and_reddit_obj_list(posts, DEFAULT_LISTING_PARAMS) == ( { "sort": POSTS_SORT_HOT }, [], )
def test_get_pagination_and_posts_small_page(mocker): """Test that we get an empty pagination and if there are less posts than page length""" posts = mocker.Mock() items = range(10) listing = mocker.MagicMock() listing.__iter__.return_value = items listing.after = None listing.before = None posts._listing = listing assert get_pagination_and_reddit_obj_list(posts, DEFAULT_LISTING_PARAMS) == ( { "sort": POSTS_SORT_HOT }, list(items), ) posts._next_batch.assert_called_once_with()
def test_get_pagination_and_posts_before_first_page(mocker): """Test that we get an pagination with before to the first page""" posts = mocker.Mock() items = range(10) listing = mocker.MagicMock() listing.__iter__.return_value = items listing.after = "def" listing.before = None posts._listing = listing assert get_pagination_and_reddit_obj_list( posts, ListingParams("xyz", None, 26, POSTS_SORT_HOT)) == ({ "after": "def", "after_count": 25, "sort": POSTS_SORT_HOT }, list(items)) posts._next_batch.assert_called_once_with()
def test_get_pagination_and_posts_before_and_after(mocker): """Test that we get an pagination with before and after""" posts = mocker.Mock() items = range(10) listing = mocker.MagicMock() listing.__iter__.return_value = items listing.after = "def" listing.before = "abc" posts._listing = listing assert get_pagination_and_reddit_obj_list( posts, ListingParams(None, None, 25, POSTS_SORT_HOT)) == ( { "before": "abc", "before_count": 26, "after": "def", "after_count": 50, "sort": POSTS_SORT_HOT, }, list(items), ) posts._next_batch.assert_called_once_with()
def get(self, request, *args, **kwargs): """View method for HTTP GET request""" with translate_praw_exceptions(request.user): api = self.request.channel_api profile_username = self.kwargs["username"] profile_user = User.objects.get(username=profile_username) object_type = self.kwargs["object_type"] listing_params = get_listing_params(self.request) if object_type == "posts": serializer_cls = BasePostSerializer listing_getter = api.list_user_posts else: serializer_cls = BaseCommentSerializer listing_getter = api.list_user_comments object_listing = listing_getter(profile_username, listing_params) pagination, user_objects = get_pagination_and_reddit_obj_list( object_listing, listing_params ) if object_type == "posts": user_objects = proxy_posts(user_objects) return Response( { object_type: serializer_cls( user_objects, many=True, context={ **self.get_serializer_context(), "users": {profile_username: profile_user}, }, ).data, "pagination": pagination, } )