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 post(self, request, *args, **kwargs): # pylint: disable=unused-argument """Create a new comment""" with translate_praw_exceptions(request.user): serializer = CommentSerializer( 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 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 test_comment_validate_removed(): """removed must be a bool""" with pytest.raises(ValidationError) as ex: CommentSerializer().validate_removed("not a bool") assert ex.value.args[0] == "removed must be a bool"
def test_comment_only_one_downvote_or_upvote(): """only one of downvoted or upvoted can be true at the same time""" with pytest.raises(ValidationError) as ex: CommentSerializer().validate({"upvoted": True, "downvoted": True}) assert ex.value.args[0] == "upvoted and downvoted cannot both be true"
def test_comment_update_with_comment_id(): """Cannot pass comment_id to a comment, this is provided in the URL""" with pytest.raises(ValidationError) as ex: CommentSerializer().update(Mock(), {"comment_id": "something"}) assert ex.value.args[0] == "comment_id must be provided via URL"
def get_comment(self, instance): """Returns the comment if this report was for one""" if isinstance(instance, Comment): return CommentSerializer(instance, context=self.context).data return None