Fix non-nominal voting on assignment polls with type votes

This commit is contained in:
Joshua Sangmeister 2020-09-10 12:09:05 +02:00
parent 04477d9ebd
commit 8d92353047
2 changed files with 20 additions and 23 deletions

View File

@ -478,7 +478,7 @@ class AssignmentPollViewSet(BasePollViewSet):
options_data = data
def create_votes_type_votes(self, data, poll, user):
def create_votes_type_votes(self, data, poll, vote_weight, vote_user):
"""
Helper function for handle_(named|pseudoanonymous)_vote
Assumes data is already validated
@ -493,36 +493,28 @@ class AssignmentPollViewSet(BasePollViewSet):
continue
weight = Decimal(amount)
if config["users_activate_vote_weight"]:
weight *= user.vote_weight
weight *= vote_weight
vote = AssignmentVote.objects.create(
option=option, user=user, weight=weight, value="Y"
option=option, user=vote_user, weight=weight, value="Y"
)
inform_changed_data(vote, no_delete_on_restriction=True)
else: # global_no or global_abstain
option = options[0]
weight = (
user.vote_weight if config["users_activate_vote_weight"] else Decimal(1)
)
weight = vote_weight if config["users_activate_vote_weight"] else Decimal(1)
vote = AssignmentVote.objects.create(
option=option, user=user, weight=weight, value=data
option=option, user=vote_user, weight=weight, value=data
)
inform_changed_data(vote, no_delete_on_restriction=True)
inform_changed_data(option)
inform_changed_data(poll)
poll.voted.add(user)
def create_votes_types_yn_yna(self, data, poll, check_user, vote_user):
def create_votes_types_yn_yna(self, data, poll, vote_weight, vote_user):
"""
check_user is used for the voted-array and weight of the vote,
vote_user is the one put into the vote
Helper function for handle_(named|pseudoanonymous)_vote
Assumes data is already validated
"""
options = poll.get_options()
weight = (
check_user.vote_weight
if config["users_activate_vote_weight"]
else Decimal(1)
)
weight = vote_weight if config["users_activate_vote_weight"] else Decimal(1)
for option_id, result in data.items():
option = options.get(pk=option_id)
vote = AssignmentVote.objects.create(
@ -531,30 +523,28 @@ class AssignmentPollViewSet(BasePollViewSet):
inform_changed_data(vote, no_delete_on_restriction=True)
inform_changed_data(option, no_delete_on_restriction=True)
poll.voted.add(check_user)
def add_user_to_voted_array(self, user, poll):
VotedModel = AssignmentPoll.voted.through
VotedModel.objects.create(assignmentpoll=poll, user=user)
def handle_named_vote(self, data, poll, user):
if poll.pollmethod == AssignmentPoll.POLLMETHOD_VOTES:
self.create_votes_type_votes(data, poll, user)
self.create_votes_type_votes(data, poll, user.vote_weight, user)
elif poll.pollmethod in (
AssignmentPoll.POLLMETHOD_YN,
AssignmentPoll.POLLMETHOD_YNA,
):
self.create_votes_types_yn_yna(data, poll, user, user)
self.create_votes_types_yn_yna(data, poll, user.vote_weight, user)
def handle_pseudoanonymous_vote(self, data, poll, user):
if poll.pollmethod == AssignmentPoll.POLLMETHOD_VOTES:
self.create_votes_type_votes(data, poll, user)
self.create_votes_type_votes(data, poll, user.vote_weight, None)
elif poll.pollmethod in (
AssignmentPoll.POLLMETHOD_YN,
AssignmentPoll.POLLMETHOD_YNA,
):
self.create_votes_types_yn_yna(data, poll, user, None)
self.create_votes_types_yn_yna(data, poll, user.vote_weight, None)
def convert_option_data(self, poll, data):
poll_options = poll.get_options()

View File

@ -984,6 +984,7 @@ class VoteAssignmentPollNamedYNA(VoteAssignmentPollBaseTestClass):
self.assertEqual(poll.votescast, Decimal("1"))
self.assertEqual(poll.state, AssignmentPoll.STATE_STARTED)
self.assertEqual(poll.amount_users_voted_with_individual_weight(), Decimal("1"))
self.assertTrue(self.admin in poll.voted.all())
option1 = poll.options.get(pk=1)
option2 = poll.options.get(pk=2)
option3 = poll.options.get(pk=3)
@ -1238,6 +1239,7 @@ class VoteAssignmentPollNamedVotes(VoteAssignmentPollBaseTestClass):
self.assertEqual(poll.votesinvalid, Decimal("0"))
self.assertEqual(poll.votescast, Decimal("1"))
self.assertEqual(poll.state, AssignmentPoll.STATE_STARTED)
self.assertTrue(self.admin in poll.voted.all())
option1 = poll.options.get(pk=1)
option2 = poll.options.get(pk=2)
self.assertEqual(option1.yes, Decimal("1"))
@ -1695,6 +1697,7 @@ class VoteAssignmentPollPseudoanonymousVotes(VoteAssignmentPollBaseTestClass):
self.assertEqual(poll.votesinvalid, Decimal("0"))
self.assertEqual(poll.votescast, Decimal("1"))
self.assertEqual(poll.state, AssignmentPoll.STATE_STARTED)
self.assertTrue(self.admin in poll.voted.all())
option1 = poll.options.get(pk=1)
option2 = poll.options.get(pk=2)
self.assertEqual(option1.yes, Decimal("1"))
@ -1703,6 +1706,8 @@ class VoteAssignmentPollPseudoanonymousVotes(VoteAssignmentPollBaseTestClass):
self.assertEqual(option2.yes, Decimal("0"))
self.assertEqual(option2.no, Decimal("0"))
self.assertEqual(option2.abstain, Decimal("0"))
for vote in poll.get_votes():
self.assertIsNone(vote.user)
def test_change_vote(self):
self.add_candidate()
@ -1756,6 +1761,8 @@ class VoteAssignmentPollPseudoanonymousVotes(VoteAssignmentPollBaseTestClass):
self.assertEqual(option2.yes, Decimal("1"))
self.assertEqual(option2.no, Decimal("0"))
self.assertEqual(option2.abstain, Decimal("0"))
for vote in poll.get_votes():
self.assertIsNone(vote.user)
def test_multiple_votes_wrong_amount(self):
self.setup_for_multiple_votes()