Esempio n. 1
0
def load_comment_content(cli, size, fake, *_, **__):
    """
    Load content's comments
    """
    nb_avg_posts = size * 20
    cli.stdout.write(f"Nombres de messages à poster en moyenne : {nb_avg_posts}")
    tps1 = time.time()
    contents = list(PublishableContent.objects.filter(sha_public__isnull=False))
    nb_contents = len(contents)
    profiles = list(Profile.objects.all())
    nb_users = len(profiles)
    for i in range(0, nb_contents):
        nb_posts = randint(0, nb_avg_posts * 2)
        post = None
        for j in range(0, nb_posts):
            post = ContentReactionFactory(
                related_content=contents[i], author=profiles[j % nb_users].user, position=j + 1
            )
            post.text = fake.paragraph(nb_sentences=5, variable_nb_sentences=True)
            post.text_html = emarkdown(post.text)
            post.save()
            sys.stdout.write(f"Contenu {i + 1}/{nb_contents}  \tCommentaire {j + 1}/{nb_posts}  \r")
            sys.stdout.flush()
        contents[i].last_note = post
        contents[i].save()
    tps2 = time.time()
    cli.stdout.write(f"\nFait en {tps2 - tps1:.3f} sec")
Esempio n. 2
0
    def test_get_content_reaction_voters(self):
        author = ProfileFactory()
        profile = ProfileFactory()
        profile2 = ProfileFactory()

        upvoted_reaction = ContentReactionFactory(author=author.user, position=2, related_content=self.content)
        upvoted_reaction.like += 2
        upvoted_reaction.save()
        CommentVote.objects.create(user=profile.user, comment=upvoted_reaction, positive=True)

        downvoted_reaction = ContentReactionFactory(author=author.user, position=3, related_content=self.content)
        downvoted_reaction.dislike += 2
        downvoted_reaction.save()
        anon_limit = CommentVote.objects.create(user=profile.user, comment=downvoted_reaction, positive=False)

        CommentVote.objects.create(user=profile2.user, comment=upvoted_reaction, positive=True)
        CommentVote.objects.create(user=profile2.user, comment=downvoted_reaction, positive=False)

        equal_reaction = ContentReactionFactory(author=author.user, position=4, related_content=self.content)
        equal_reaction.like += 1
        equal_reaction.dislike += 1
        equal_reaction.save()

        CommentVote.objects.create(user=profile.user, comment=equal_reaction, positive=True)
        CommentVote.objects.create(user=profile2.user, comment=equal_reaction, positive=False)

        self.client.force_login(profile.user)

        # on first message we should see 2 likes and 0 anonymous
        response = self.client.get(reverse("api:content:reaction-karma", args=[upvoted_reaction.pk]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(2, len(response.data["like"]["users"]))
        self.assertEqual(0, len(response.data["dislike"]["users"]))
        self.assertEqual(2, response.data["like"]["count"])
        self.assertEqual(0, response.data["dislike"]["count"])

        # on second message we should see 2 dislikes and 0 anonymous
        response = self.client.get(reverse("api:content:reaction-karma", args=[downvoted_reaction.pk]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(0, len(response.data["like"]["users"]))
        self.assertEqual(2, len(response.data["dislike"]["users"]))
        self.assertEqual(0, response.data["like"]["count"])
        self.assertEqual(2, response.data["dislike"]["count"])

        # on third message we should see 1 like and 1 dislike and 0 anonymous
        response = self.client.get(reverse("api:content:reaction-karma", args=[equal_reaction.pk]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(1, len(response.data["like"]["users"]))
        self.assertEqual(1, len(response.data["dislike"]["users"]))
        self.assertEqual(1, response.data["like"]["count"])
        self.assertEqual(1, response.data["dislike"]["count"])

        # Now we change the settings to keep anonymous the first [dis]like
        previous_limit = settings.VOTES_ID_LIMIT
        settings.VOTES_ID_LIMIT = anon_limit.pk
        # and we run the same tests
        # on first message we should see 1 like and 1 anonymous
        response = self.client.get(reverse("api:content:reaction-karma", args=[upvoted_reaction.pk]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(1, len(response.data["like"]["users"]))
        self.assertEqual(0, len(response.data["dislike"]["users"]))
        self.assertEqual(2, response.data["like"]["count"])
        self.assertEqual(0, response.data["dislike"]["count"])

        # on second message we should see 1 dislikes and 1 anonymous
        response = self.client.get(reverse("api:content:reaction-karma", args=[downvoted_reaction.pk]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(0, len(response.data["like"]["users"]))
        self.assertEqual(1, len(response.data["dislike"]["users"]))
        self.assertEqual(0, response.data["like"]["count"])
        self.assertEqual(2, response.data["dislike"]["count"])

        # on third message we should see 1 like and 1 dislike and 0 anonymous
        response = self.client.get(reverse("api:content:reaction-karma", args=[equal_reaction.pk]))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(1, len(response.data["like"]["users"]))
        self.assertEqual(1, len(response.data["dislike"]["users"]))
        self.assertEqual(1, response.data["like"]["count"])
        self.assertEqual(1, response.data["dislike"]["count"])
        settings.VOTES_ID_LIMIT = previous_limit