def test_upvote_distribution(self, ):
     distribution_amount = calculate_rsc_per_upvote()
     create_upvote_distribution(1, self.original_paper,
                                PaperVote.objects.first())
     self.assertEquals(AuthorRSC.objects.count(), 1)
     self.assertEquals(AuthorRSC.objects.first().amount,
                       math.floor(distribution_amount * 0.75))
Example #2
0
def get_discussion_vote_item_distribution(instance):
    vote_type = instance.vote_type
    item = instance.item
    item_type = type(item)

    error = TypeError(f"Instance of type {item_type} is not supported")

    if vote_type == ReactionVote.UPVOTE:
        if item_type == Comment:
            vote_type = distributions.CommentUpvoted.name
        elif item_type == Reply:
            vote_type = distributions.ReplyUpvoted.name
        elif item_type == Thread:
            vote_type = distributions.ThreadUpvoted.name
        elif item_type == ResearchhubPost:
            vote_type = distributions.ResearchhubPostUpvoted.name
        else:
            raise error

        return distributions.create_upvote_distribution(
            vote_type, None, instance)
    elif vote_type == ReactionVote.DOWNVOTE:
        if item_type == Comment:
            return distributions.CommentDownvoted
        elif item_type == Reply:
            return distributions.ReplyDownvoted
        elif item_type == Thread:
            return distributions.ThreadDownvoted
        elif item_type == ResearchhubPost:
            return distributions.ResearchhubPostDownvoted
        else:
            raise error
    elif vote_type == ReactionVote.NEUTRAL:
        return distributions.NeutralVote
    def test_reply_upvote_distribution(self):
        if Distribution.objects.count() > 0:
            Distribution.objects.all().delete()

        if AuthorRSC.objects.count() > 0:
            AuthorRSC.objects.all().delete()

        if Author.objects.count() > 0:
            Author.objects.all().delete()

        new_user = self.create_user(
            first_name="First",
            last_name="Last",
            email="*****@*****.**",
        )

        self.user.reputation = 50000
        self.user.save()

        thread = Thread.objects.create(created_by=new_user,
                                       paper=self.original_paper)
        reply_ct = ContentType.objects.get(model="reply")
        comment = Comment.objects.create(created_by=self.user, parent=thread)
        reply = Reply.objects.create(created_by=new_user, parent=comment)
        reply_vote = DiscussionVote.objects.create(item=reply,
                                                   content_type=reply_ct,
                                                   vote_type=1,
                                                   created_by=self.user)

        distribution = create_upvote_distribution(1, None, reply_vote)
        self.assertEquals(Distribution.objects.count(), 1)
        distribution_amount = calculate_rsc_per_upvote()
        self.assertEquals(distribution.amount, distribution_amount)
    def test_author_claim_pot(self, ):
        if Distribution.objects.count() > 0:
            Distribution.objects.all().delete()

        if AuthorRSC.objects.count() > 0:
            AuthorRSC.objects.all().delete()

        self.original_paper.raw_authors = [
            {
                "first_name": "First",
                "last_name": "Last"
            },
            {
                "first_name": "Jimmy",
                "last_name": "Johns"
            },
            {
                "first_name": "Ronald",
                "last_name": "McDonald"
            },
        ]

        self.original_paper.save()

        university = self.create_university()
        author_user = self.create_user(
            first_name="First",
            last_name="Last",
            email="*****@*****.**",
        )

        distribution = create_upvote_distribution(1, self.original_paper,
                                                  PaperVote.objects.first())
        distribution_amount = calculate_rsc_per_upvote()

        university = self.create_university()
        if Author.objects.count() > 0:
            Author.objects.all().delete()

        author = Author.objects.create(
            user=author_user,
            first_name=self.original_paper.raw_authors[0].get("first_name"),
            last_name=self.original_paper.raw_authors[0].get("last_name"),
            university=university,
        )

        self.original_paper.authors.add(author)
        case = AuthorClaimCase.objects.create(target_paper=self.original_paper,
                                              requestor=author.user,
                                              status=APPROVED)

        after_approval_flow.apply((case.id, ), priority=2, countdown=5)

        self.assertEquals(Distribution.objects.count(), 2)
        self.assertEquals(
            Distribution.objects.filter(
                distribution_type="UPVOTE_RSC_POT").first().amount,
            math.floor(distribution_amount * 0.75 / 3),
        )
Example #5
0
def distribute_for_paper_upvoted(sender, instance, created, update_fields,
                                 **kwargs):
    """Distributes reputation to the uploader."""
    timestamp = time()
    recipient = instance.paper.uploaded_by

    if is_eligible_for_paper_upvoted(created, instance.created_by, recipient):
        distributor = Distributor(
            distributions.create_upvote_distribution(
                distributions.PaperUpvoted.name, instance.paper, instance),
            recipient,
            instance,
            timestamp,
            instance.paper.hubs.all(),
        )
        record = distributor.distribute()
    def test_author_claim_distribution(self, ):
        self.original_paper.raw_authors = [
            {
                "first_name": "First",
                "last_name": "Last"
            },
            {
                "first_name": "Jimmy",
                "last_name": "Johns"
            },
            {
                "first_name": "Ronald",
                "last_name": "McDonald"
            },
        ]

        university = self.create_university()
        author_user = self.create_user(
            first_name="First",
            last_name="Last",
            email="*****@*****.**",
        )

        university = self.create_university()
        if Author.objects.count() > 0:
            Author.objects.all().delete()
        author = Author.objects.create(
            user=author_user,
            first_name=self.original_paper.raw_authors[0].get("first_name"),
            last_name=self.original_paper.raw_authors[0].get("last_name"),
            university=university,
        )

        self.original_paper.authors.add(author)
        AuthorClaimCase.objects.create(target_paper=self.original_paper,
                                       requestor=author.user,
                                       status=APPROVED)
        distribution = create_upvote_distribution(1, self.original_paper,
                                                  PaperVote.objects.first())
        distribution_amount = calculate_rsc_per_upvote()
        self.assertEquals(Distribution.objects.count(), 1)
        self.assertEquals(
            Distribution.objects.first().amount,
            math.floor(distribution_amount * 0.75 / 3),
        )
    def test_no_verified_author_distribution(self, ):
        self.original_paper.raw_authors = [
            {
                "first_name": "First",
                "last_name": "Last"
            },
            {
                "first_name": "Jimmy",
                "last_name": "Johns"
            },
            {
                "first_name": "Ronald",
                "last_name": "McDonald"
            },
        ]
        university = self.create_university()
        author_user = self.create_user(
            first_name="First",
            last_name="Last",
            email="*****@*****.**",
        )

        if Author.objects.count() > 0:
            Author.objects.all().delete()

        university = self.create_university()
        author = Author.objects.create(
            user=author_user,
            first_name=self.original_paper.raw_authors[0].get("first_name"),
            last_name=self.original_paper.raw_authors[0].get("last_name"),
            university=university,
        )

        self.original_paper.authors.add(author)
        distribution = create_upvote_distribution(1, self.original_paper,
                                                  PaperVote.objects.first())
        distribution_amount = calculate_rsc_per_upvote()
        self.assertEquals(Distribution.objects.count(), 0)
        self.assertEquals(distribution.amount, distribution_amount * 0.25)
    def test_ineligible_enhanced_distribution(self):
        if Distribution.objects.count() > 0:
            Distribution.objects.all().delete()

        if AuthorRSC.objects.count() > 0:
            AuthorRSC.objects.all().delete()

        if Author.objects.count() > 0:
            Author.objects.all().delete()

        eligible_user = self.create_user(
            first_name="First",
            last_name="Last",
            email="*****@*****.**",
        )

        eligible_user.reputation = 20000
        eligible_user.save()

        distribution_amount = calculate_rsc_per_upvote()
        distribution = create_upvote_distribution(1, self.original_paper,
                                                  PaperVote.objects.first())
        self.assertEquals(distribution.amount, 1)