def get(self, request, *args, **kwargs): # pylint: disable=unused-argument """Get list for comments and attach User objects to them""" with translate_praw_exceptions(request.user): children = request.query_params.getlist("children") sort = request.query_params.get("sort", COMMENTS_SORT_BEST) # validate the request parameters: each are required try: post_id = request.query_params["post_id"] except KeyError: raise ValidationError("Missing parameter post_id") parent_id = request.query_params.get("parent_id") api = Api(user=self.request.user) comments = api.more_comments(parent_id, post_id, children, sort) users = _lookup_users_for_comments(comments) subscriptions = lookup_subscriptions_for_comments( comments, self.request.user) serialized_comments_list = GenericCommentSerializer( comments, context={ **self.get_serializer_context(), "users": users, "comment_subscriptions": subscriptions, }, many=True, ).data return Response(serialized_comments_list)
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 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): # pylint: disable=unused-argument """List of reports""" with translate_praw_exceptions(request.user): api = Api(user=request.user) reports = api.list_reports(self.kwargs["channel_name"]) serializer = ReportedContentSerializer( reports, many=True, context=self.get_serializer_context() ) return Response(serializer.data)
def delete(self, request, *args, **kwargs): # pylint: disable=unused-argument """ Removes a subscriber from a channel """ api = Api(user=request.user) channel_name = self.kwargs["channel_name"] subscriber_name = self.kwargs["subscriber_name"] api.remove_subscriber(subscriber_name, channel_name) return Response(status=status.HTTP_204_NO_CONTENT)
def populate_user_subscriptions(user_ids): """ Populate channel user roles for a list of user ids Args: user_ids(list of int): List of user ids """ for user in User.objects.filter(id__in=user_ids).iterator(): client = Api(user) channels = client.list_channels() for channel in channels: sync_channel_subscription_model(channel.display_name, user=user)
def get(self, request, *args, **kwargs): # pylint: disable=unused-argument """Get list for comments and attach User objects to them""" with translate_praw_exceptions(request.user): post_id = self.kwargs["post_id"] api = Api(user=self.request.user) sort = request.query_params.get("sort", COMMENTS_SORT_BEST) comments = api.list_comments(post_id, sort).list() users = _lookup_users_for_comments(comments) subscriptions = lookup_subscriptions_for_comments( comments, self.request.user) serialized_comments_list = GenericCommentSerializer( comments, context={ **self.get_serializer_context(), "users": users, "comment_subscriptions": subscriptions, }, many=True, ).data return Response(serialized_comments_list)
def test_list_reports(staff_client, private_channel_and_contributor, reddit_factories, staff_api): """List reported content""" channel, user = private_channel_and_contributor post = reddit_factories.text_post("post", user, channel=channel) comment = reddit_factories.comment("comment", user, post_id=post.id) # report both with a regular user api = Api(user) api.report_comment(comment.id, "spam") api.report_post(post.id, "bad") # report both with a moderator user staff_api.report_comment(comment.id, "spam") staff_api.report_post(post.id, "junk") url = reverse("channel-reports", kwargs={"channel_name": channel.name}) resp = staff_client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json() == [ { "post": None, "comment": { "author_id": user.username, "created": comment.created, "id": comment.id, "parent_id": None, "post_id": post.id, "score": 1, "text": comment.text, "upvoted": False, "downvoted": False, "removed": False, "deleted": False, "subscribed": False, "profile_image": image_uri(user.profile), "author_name": user.profile.name, "author_headline": user.profile.headline, "edited": False, "comment_type": "comment", "num_reports": 2, }, "reasons": ["spam"], }, { "post": { **default_post_response_data(channel, post, user), "num_comments": 1, "num_reports": 2, "upvoted": False, }, "comment": None, "reasons": ["bad", "junk"], }, ]
def record(name, user): """ Record a cassette of some reddit communication. Usage: with record('cassette_name', my_user) as api: api.update_channel('channel', title='new_title') Args: name (str): The name of the new cassette user (django.contrib.auth.models.User): User to authenticate with """ setup_betamax() session = requests.Session() session.verify = False with patch("channels.api._get_session", return_value=session): with Betamax(session).use_cassette(name): api = Api(user) yield api
def __call__(self, request): request.channel_api = SimpleLazyObject(lambda: Api(request.user)) return self.get_response(request)
def api(self): """Returns a Channel API object""" return Api(user=self.request.user)
def get_queryset(self): """Get generator for channels list""" api = Api(user=self.request.user) return api.list_channels()