def _get_notification_data(self, current_notification, last_notification): # pylint: disable=unused-argument """ Gets the data for this notification Args: current_notification (NotificationBase): current notification we're sending for last_notification (NotificationBase): last notification that was triggered for this NotificationSettings Raises: InvalidTriggerFrequencyError: if the frequency is invalid for the frontpage digest """ event = CommentEvent.objects.get( email_notification=current_notification) api_client = api.Api(self.user) comment = api_client.get_comment(event.comment_id) parent = comment.parent() is_comment_reply = isinstance(parent, Comment) ctx = {"current_user": self.user} post = proxy_post(comment.submission) if not is_comment_reply: parent = proxy_post(parent) return { "is_comment_reply": is_comment_reply, "post": PostSerializer(post, context=ctx).data, "parent": (CommentSerializer(parent, context=ctx) if is_comment_reply else PostSerializer(parent, context=ctx)).data, "comment": CommentSerializer(comment, context=ctx).data, }
def test_post_edit_url(): """Cannot update the URL for a post""" with pytest.raises(ValidationError) as ex: PostSerializer( context={"request": Mock(), "view": Mock(kwargs={"post_id": "post"})} ).update(Mock(), {"url": "url"}) assert ex.value.args[0] == "Cannot edit url for a post"
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 test_post_both_text_and_url(): """We can't create a post with both text and url specified""" with pytest.raises(ValidationError) as ex: PostSerializer().create({"title": "title", "text": "text", "url": "url"}) assert ( ex.value.args[0] == "Not more than one of text, url, or article_content can be provided" )
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 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 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 test_post_validate_removed(): """removed must be a bool""" with pytest.raises(ValidationError) as ex: PostSerializer().validate_removed("not a bool") assert ex.value.args[0] == "removed must be a bool"
def test_post_validate_url(): """url must be a string""" with pytest.raises(ValidationError) as ex: PostSerializer().validate_url(["not a string"]) assert ex.value.args[0] == "url must be a string"
def get_post(self, instance): """Returns the post if this report was for one""" if isinstance(instance, Submission): return PostSerializer(proxy_post(instance), context=self.context).data return None