def setUp(self):
        self.c = Client()
        author = RedditUser.objects.create(user=User.objects.create_user(
            username="******", password="******"))

        for i in range(50):
            Submission.objects.create(score=50 - i,
                                      title=get_random_string(length=20),
                                      author=author).save()

        for i in range(1, 50, 10):
            # [1, 11, 21] [31, 41] have upvotes (lists demonstrate pages)
            Vote.create(user=author,
                        vote_object=Submission.objects.get(id=i),
                        vote_value=1).save()

        for i in range(2, 50, 15):
            # [2, 17] [32, 47] have downvotes (lists demonstrate pages)
            Vote.create(user=author,
                        vote_object=Submission.objects.get(id=i),
                        vote_value=-1).save()
    def test_comment_vote_cancel_or_reverse(self):
        submission = Submission.objects.filter(title="vote testing").first()
        user = RedditUser.objects.get(user=User.objects.get(
            username=self.credentials['username']))
        self.assertIsNotNone(submission)
        self.assertIsNotNone(user)
        comment = Comment.objects.filter(submission=submission).first()
        self.assertIsNotNone(comment)
        Vote.create(user=user, vote_object=comment, vote_value=1).save()

        self.c.login(**self.credentials)
        r = self.c.post(reverse('vote'),
                        data={
                            'what': 'comment',
                            'what_id': comment.id,
                            'vote_value': '1'
                        })
        self.assertEqual(r.status_code, 200)
        json_r = json.loads(r.content.decode("utf-8"))
        self.assertIsNone(json_r['error'])
        self.assertEqual(json_r['voteDiff'], -1)

        vote = Vote.objects.get(vote_object_type=comment.get_content_type(),
                                vote_object_id=comment.id,
                                user=user)
        vote.value = 1
        vote.save()

        self.c.login(**self.credentials)
        r = self.c.post(reverse('vote'),
                        data={
                            'what': 'comment',
                            'what_id': comment.id,
                            'vote_value': '-1'
                        })
        self.assertEqual(r.status_code, 200)
        json_r = json.loads(r.content.decode("utf-8"))
        self.assertIsNone(json_r['error'])
        self.assertEqual(json_r['voteDiff'], -2)
예제 #3
0
    def setUp(self):
        self.c = Client()
        author = RedditUser.objects.create(
            user=User.objects.create_user(username="******",
                                          password="******"))

        for i in range(50):
            Submission.objects.create(score=50 - i,
                                      title=get_random_string(length=20),
                                      author=author).save()

        for i in range(1, 50, 10):
            # [1, 11, 21] [31, 41] have upvotes (lists demonstrate pages)
            Vote.create(user=author,
                        vote_object=Submission.objects.get(id=i),
                        vote_value=1).save()

        for i in range(2, 50, 15):
            # [2, 17] [32, 47] have downvotes (lists demonstrate pages)
            Vote.create(user=author,
                        vote_object=Submission.objects.get(id=i),
                        vote_value=-1).save()
예제 #4
0
    def test_comment_vote_cancel_or_reverse(self):
        submission = Submission.objects.filter(title="vote testing").first()
        user = RedditUser.objects.get(
            user=User.objects.get(username=self.credentials['username']))
        self.assertIsNotNone(submission)
        self.assertIsNotNone(user)
        comment = Comment.objects.filter(submission=submission).first()
        self.assertIsNotNone(comment)
        Vote.create(user=user, vote_object=comment, vote_value=1).save()

        self.c.login(**self.credentials)
        r = self.c.post(reverse('vote'),
                        data={
                            'what': 'comment',
                            'what_id': comment.id,
                            'vote_value': '1'
                        })
        self.assertEqual(r.status_code, 200)
        json_r = json.loads(r.content.decode("utf-8"))
        self.assertIsNone(json_r['error'])
        self.assertEqual(json_r['voteDiff'], -1)

        vote = Vote.objects.get(vote_object_type=comment.get_content_type(),
                                vote_object_id=comment.id,
                                user=user)
        vote.value = 1
        vote.save()

        self.c.login(**self.credentials)
        r = self.c.post(reverse('vote'),
                        data={
                            'what': 'comment',
                            'what_id': comment.id,
                            'vote_value': '-1'
                        })
        self.assertEqual(r.status_code, 200)
        json_r = json.loads(r.content.decode("utf-8"))
        self.assertIsNone(json_r['error'])
        self.assertEqual(json_r['voteDiff'], -2)
예제 #5
0
    def setUp(self):
        self.c = Client()
        self.credentials = {'username': '******',
                            'password': '******'}
        author = RedditUser.objects.create(
            user=User.objects.create_user(**self.credentials)
        )

        submission = Submission.objects.create(
            id=1,
            score=1,
            title=get_random_string(length=12),
            author=author
        )

        for _ in range(3):
            Comment.objects.create(
                author_name=author.user.username,
                author=author,
                submission=submission,
                html_comment="root comment"
            )

        # Add some replies
        parent = Comment.objects.get(id=1)
        for _ in range(2):
            Comment.objects.create(
                author_name=author.user.username,
                author=author,
                submission=submission,
                parent=parent,
                html_comment="reply comment"
            )

        # add upvote to one root comment,
        Vote.create(
            user=author,
            vote_object=Comment.objects.get(id=1),
            vote_value=1
        ).save()

        # and downvote to one reply comment
        Vote.create(
            user=author,
            vote_object=Comment.objects.get(id=5),
            vote_value=-1
        ).save()

        # add upvote to the submission
        Vote.create(
            user=author,
            vote_object=submission,
            vote_value=1
        ).save()
    def setUp(self):
        self.c = Client()
        self.credentials = {'username': '******',
                            'password': '******'}
        author = RedditUser.objects.create(
            user=User.objects.create_user(**self.credentials)
        )

        submission = Submission.objects.create(
            id=1,
            score=1,
            title=get_random_string(length=12),
            author=author
        )

        for _ in range(3):
            Comment.objects.create(
                author_name=author.user.username,
                author=author,
                submission=submission,
                html_comment="root comment"
            )

        # Add some replies
        parent = Comment.objects.get(id=1)
        for _ in range(2):
            Comment.objects.create(
                author_name=author.user.username,
                author=author,
                submission=submission,
                parent=parent,
                html_comment="reply comment"
            )

        # add upvote to one root comment,
        Vote.create(
            user=author,
            vote_object=Comment.objects.get(id=1),
            vote_value=1
        ).save()

        # and downvote to one reply comment
        Vote.create(
            user=author,
            vote_object=Comment.objects.get(id=5),
            vote_value=-1
        ).save()

        # add upvote to the submission
        Vote.create(
            user=author,
            vote_object=submission,
            vote_value=1
        ).save()
예제 #7
0
def vote(request):
    # The type of object we're voting on, can be 'submission' or 'comment'
    vote_object_type = request.POST.get('what', None)

    # The ID of that object as it's stored in the database, positive int
    vote_object_id = request.POST.get('what_id', None)

    # The value of the vote we're writing to that object, -1 or 1
    # Passing the same value twice will cancel the vote i.e. set it to 0
    new_vote_value = request.POST.get('vote_value', None)

    # By how much we'll change the score, used to modify score on the fly
    # client side by the javascript instead of waiting for a refresh.
    vote_diff = 0

    if not request.user.is_authenticated():
        return HttpResponseForbidden()
    else:
        user = RedditUser.objects.get(user=request.user)

    try:  # If the vote value isn't an integer that's equal to -1 or 1
        # the request is bad and we can not continue.
        new_vote_value = int(new_vote_value)

        if new_vote_value not in [-1, 1]:
            raise ValueError("Wrong value for the vote!")

    except (ValueError, TypeError):
        return HttpResponseBadRequest()

    # if one of the objects is None, 0 or some other bool(value) == False value
    # or if the object type isn't 'comment' or 'submission' it's a bad request
    if not all([vote_object_type, vote_object_id, new_vote_value]) or \
            vote_object_type not in ['comment', 'submission']:
        return HttpResponseBadRequest()

    # Try and get the actual object we're voting on.
    try:
        if vote_object_type == "comment":
            vote_object = Comment.objects.get(id=vote_object_id)

        elif vote_object_type == "submission":
            vote_object = Submission.objects.get(id=vote_object_id)
        else:
            return HttpResponseBadRequest()  # should never happen

    except (Comment.DoesNotExist, Submission.DoesNotExist):
        return HttpResponseBadRequest()

    # Try and get the existing vote for this object, if it exists.
    try:
        vote = Vote.objects.get(vote_object_type=vote_object.get_content_type(),
                                vote_object_id=vote_object.id,
                                user=user)

    except Vote.DoesNotExist:
        # Create a new vote and that's it.
        vote = Vote.create(user=user,
                           vote_object=vote_object,
                           vote_value=new_vote_value)
        vote.save()
        vote_diff = new_vote_value
        return JsonResponse({'error'   : None,
                             'voteDiff': vote_diff})

    # User already voted on this item, this means the vote is either
    # being canceled (same value) or changed (different new_vote_value)
    if vote.value == new_vote_value:
        # canceling vote
        vote_diff = vote.cancel_vote()
        if not vote_diff:
            return HttpResponseBadRequest(
                'Something went wrong while canceling the vote')
    else:
        # changing vote
        vote_diff = vote.change_vote(new_vote_value)
        if not vote_diff:
            return HttpResponseBadRequest(
                'Wrong values for old/new vote combination')

    return JsonResponse({'error'   : None,
                         'voteDiff': vote_diff})