From 1667763a8af6b6e2aa76fa3536e90f69d50882ba Mon Sep 17 00:00:00 2001 From: Joshua Santos Date: Sep 01 2015 09:44:22 +0000 Subject: Add select revote --- diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py index 1e6706c..4ca1ec3 100644 --- a/fedora_elections/elections.py +++ b/fedora_elections/elections.py @@ -114,7 +114,7 @@ def vote(election_alias): elif election.voting_type == 'simple': return vote_simple(election) elif election.voting_type == 'select': - return vote_select(election) + return vote_select(election, revote) elif election.voting_type == 'irc': return vote_irc(election) else: # pragma: no cover @@ -182,9 +182,9 @@ def vote_range(election, revote): nextaction=next_action) -def vote_select(election): +def vote_select(election, revote): votes = models.Vote.of_user_on_election( - SESSION, flask.g.fas_user.username, election.id, count=True) + SESSION, flask.g.fas_user.username, election.id) num_candidates = election.candidates.count() @@ -213,20 +213,27 @@ def vote_select(election): flask.flash('Too many candidates submitted', 'error') else: if form.action.data == 'submit': - for candidate in form: - if candidate.short_name in ['csrf_token', 'action']: - continue - - new_vote = models.Vote( - election_id=election.id, - voter=flask.g.fas_user.username, - timestamp=datetime.now(), - candidate_id=cand_name[candidate.short_name], - value=int(candidate.data), - ) - SESSION.add(new_vote) - SESSION.commit() - + candidates = [ + candidate + for candidate in form + if candidate and candidate.short_name not in ['csrf_token', 'action'] + ] + for index in range(len(candidates)): + candidate = candidates[index] + if revote: + vote = votes[index] + vote.value = int(candidate.data) + SESSION.add(vote) + else: + new_vote = models.Vote( + election_id=election.id, + voter=flask.g.fas_user.username, + timestamp=datetime.now(), + candidate_id=cand_name[candidate.short_name], + value=int(candidate.data), + ) + SESSION.add(new_vote) + SESSION.commit() flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() diff --git a/tests/test_flask_select_voting.py b/tests/test_flask_select_voting.py index 73d2ca0..845d94e 100644 --- a/tests/test_flask_select_voting.py +++ b/tests/test_flask_select_voting.py @@ -55,14 +55,6 @@ class FlaskSimpleElectionstests(ModelFlasktests): self.setup_db() - user = FakeUser(['packager'], username='toshio') - with user_set(fedora_elections.APP, user): - output = self.app.get( - '/vote/test_election6', follow_redirects=True) - self.assertTrue( - 'class="message">You have already voted in the election!Next 1 elections' in output.data) self.assertTrue('

Last 2 elections

' in output.data) + def test_vote_select_revote(self): + """ Test the vote_range function - the re-voting part. """ + #First we need to vote + self.setup_db() + + user = FakeUser(['voters'], username='nerdsville') + with user_set(fedora_elections.APP, user): + retrieve_csrf = self.app.post('/vote/test_election6') + csrf_token = retrieve_csrf.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + + # Valid input + data = { + 'Kevin': True, + 'action': 'submit', + 'csrf_token': csrf_token, + } + self.app.post('/vote/test_election6', data=data, follow_redirects=True) + vote = fedora_elections.models.Vote + votes = vote.of_user_on_election(self.session, "nerdsville", '6') + self.assertEqual(votes[0].candidate.name, "Toshio") + self.assertEqual(votes[0].value, 0) + self.assertEqual(votes[1].candidate.name, "Kevin") + self.assertEqual(votes[1].value, 1) + + #Next, we need to try revoting + newdata = { + 'Toshio': True, + 'action': 'submit', + 'csrf_token': csrf_token, + } + output = self.app.post('/vote/test_election6', data=newdata, follow_redirects=True) + #Next, we need to check if the vote has been recorded + self.assertEqual(output.status_code, 200) + self.assertTrue( + 'class="message">Your vote has been recorded. Thank you!Current elections' in output.data) + self.assertTrue('

Next 1 elections

' in output.data) + self.assertTrue('

Last 2 elections

' in output.data) + vote = fedora_elections.models.Vote + votes = vote.of_user_on_election(self.session, "nerdsville", '6') + self.assertEqual(votes[0].value, 1) + self.assertEqual(votes[1].value, 0) + + + #If we haven't failed yet, HOORAY! + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(