def test_translate_praw_exceptions(raised_exception, is_anonymous, expected_exception): """Test that exceptions are translated correctly""" user = Mock(is_anonymous=is_anonymous) if expected_exception is None: with translate_praw_exceptions(user): if raised_exception is not None: raise raised_exception else: with pytest.raises(expected_exception): with translate_praw_exceptions(user): if raised_exception is not None: raise raised_exception
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): """GET a single comment and it's children""" with translate_praw_exceptions(request.user): comment = self.get_object() # comment.refresh() raises if the comment # isn't found try: comment.refresh() except PRAWException: raise NotFound() users = _lookup_users_for_comments([comment]) subscriptions = lookup_subscriptions_for_comments( [comment], self.request.user) serialized_comment_tree = GenericCommentSerializer( [comment] + comment.replies.list(), context={ **self.get_serializer_context(), "users": users, "comment_subscriptions": subscriptions, }, many=True, ).data return Response(serialized_comment_tree)
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): # we don't want to let this through to Reddit, because it blows up :/ if len(kwargs["channel_name"]) == 1: return Response({}, status=status.HTTP_404_NOT_FOUND) with translate_praw_exceptions(request.user): return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs): """Create a new post""" with translate_praw_exceptions(request.user): serializer = PostSerializer(data=request.data, context=self.get_serializer_context()) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): # pylint: disable=unused-argument """Create a report""" with translate_praw_exceptions(request.user): serializer = ReportSerializer( data=request.data, context=self.get_serializer_context() ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
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 put(self, request, *args, **kwargs): # Deny permission if the user is not a superuser and is attempting to update the channel title. channel = self.get_object() if ( not self.request.user.is_superuser and self.request.data.get("title") != channel.title ): raise PermissionDenied() with translate_praw_exceptions(request.user): return super().put(request, *args, **kwargs)
def patch(self, request, *args, **kwargs): # pylint: disable=unused-argument """Update a post""" with translate_praw_exceptions(request.user): post = self.get_object() serializer = PostSerializer( instance=post, data=request.data, context=self.get_serializer_context(), partial=True, ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def get(self, request, *args, **kwargs): """Get post""" with translate_praw_exceptions(request.user): post = self.get_object() users = lookup_users_for_posts([post]) if not post.author or post.author.name not in users: raise NotFound() subscriptions = lookup_subscriptions_for_posts([post], request.user) return Response( PostSerializer( instance=post, context={ **self.get_serializer_context(), "users": users, "post_subscriptions": subscriptions, }, ).data)
def patch(self, request, *args, **kwargs): # pylint: disable=unused-argument """Update a comment""" with translate_praw_exceptions(request.user): comment = self.get_object() subscriptions = lookup_subscriptions_for_comments( [comment], self.request.user) serializer = CommentSerializer( instance=comment, data=request.data, context={ **self.get_serializer_context(), "comment_subscriptions": subscriptions, }, partial=True, ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
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 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, } )
def patch(self, request, *args, **kwargs): # Deny permission if the user is not a superuser and is attempting to update the channel title. if not self.request.user.is_superuser and "title" in self.request.data: raise PermissionDenied() with translate_praw_exceptions(request.user): return super().patch(request, *args, **kwargs)
def post(self, request, *args, **kwargs): with translate_praw_exceptions(request.user): return super().post(request, *args, **kwargs)