From 03d775bf5b66b91a6569aafe026fcfad2fa1e28d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 25 2015 08:26:15 +0000 Subject: Merge pull request #54 from NerdsvilleCEO/add-simple-revote Add simple revote --- diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py index 3ccc00e..4b65e75 100644 --- a/fedora_elections/elections.py +++ b/fedora_elections/elections.py @@ -112,7 +112,7 @@ def vote(election_alias): if election.voting_type.startswith('range'): return vote_range(election, revote) elif election.voting_type == 'simple': - return vote_simple(election) + return vote_simple(election, revote) elif election.voting_type == 'select': return vote_select(election, revote) elif election.voting_type == 'irc': @@ -200,7 +200,7 @@ def vote_select(election, revote): for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] - process_vote_by_cand_name(candidates, cand_name, election, votes, revote) + process_vote(candidates, election, votes, revote, cand_name) flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() @@ -219,9 +219,9 @@ def vote_select(election, revote): usernamemap=usernamemap, nextaction=next_action) -def vote_simple(election): +def vote_simple(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() @@ -233,20 +233,12 @@ def vote_simple(election): if form.validate_on_submit(): 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=candidate.data, - value=1, - ) - SESSION.add(new_vote) - SESSION.commit() - + candidates = [ + candidate + for candidate in form + if candidate and candidate.short_name not in ['csrf_token', 'action'] + ] + process_vote(candidates, election, votes, revote, value=1) flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() @@ -283,7 +275,7 @@ def vote_irc(election, revote): for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] - process_vote_by_cand_name(candidates, cand_name, election, votes, revote) + process_vote(candidates, election, votes, revote, cand_name) flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() @@ -379,38 +371,30 @@ def election_results_text(election_alias): stats=stats, ) -def process_vote(candidates, election, votes, revote): +def process_vote(candidates, election, votes, revote, cand_name=None, value=None): for index in range(len(candidates)): candidate = candidates[index] if revote: vote = votes[index] - vote.value = int(candidate.data) + if value is not None: + vote.candidate_id = candidate.data + else: + vote.value = value if value else 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=candidate.short_name, - value=int(candidate.data), - ) - SESSION.add(new_vote) - SESSION.commit() + if value is not None: + cand_id = candidate.data + elif cand_name: + cand_id = cand_name[candidate.short_name] + else: + cand_id = candidate.short_name -def process_vote_by_cand_name(candidates, cand_name, election, votes, revote): - 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), + timestamp=datetime.utcnow(), + candidate_id=cand_id, + value= value if value else int(candidate.data), ) SESSION.add(new_vote) SESSION.commit() diff --git a/fedora_elections/models.py b/fedora_elections/models.py index b3e72cc..31d6fea 100644 --- a/fedora_elections/models.py +++ b/fedora_elections/models.py @@ -136,7 +136,7 @@ class Election(BASE): @property def locked(self): - return datetime.now() >= self.start_date + return datetime.utcnow() >= self.start_date @classmethod def search(cls, session, alias=None, shortdesc=None, diff --git a/fedora_elections/templates/index.html b/fedora_elections/templates/index.html index abfcdd1..5d15ef8 100644 --- a/fedora_elections/templates/index.html +++ b/fedora_elections/templates/index.html @@ -32,7 +32,7 @@ Vote now! - {% elif election.voting_type != "simple" %} + {% else %} Change your vote! diff --git a/tests/test_flask_elections.py b/tests/test_flask_elections.py index 90a0230..fd24ce9 100644 --- a/tests/test_flask_elections.py +++ b/tests/test_flask_elections.py @@ -155,17 +155,6 @@ class FlaskElectionstests(ModelFlasktests): '' in output.data) - user = FakeUser(['voters'], username='toshio') - with user_set(fedora_elections.APP, user): - - # Election in progress - output = self.app.get( - '/vote/test_election3', follow_redirects=True) - self.assertTrue( - 'class="message">You have already voted in the election!Current elections' in output.data) - def test_election_results(self): """ Test the election_results function - the preview part. """ output = self.app.get( diff --git a/tests/test_flask_simple_voting.py b/tests/test_flask_simple_voting.py index 1eaa339..c0ea957 100644 --- a/tests/test_flask_simple_voting.py +++ b/tests/test_flask_simple_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_election5', 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_simple_revote(self): + """ Test the vote_simple 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_election5') + csrf_token = retrieve_csrf.data.split( + 'name="csrf_token" type="hidden" value="')[1].split('">')[0] + # Valid input + data = { + 'candidate': 8, + 'action': 'submit', + 'csrf_token': csrf_token, + } + + self.app.post('/vote/test_election5', data=data, follow_redirects=True) + vote = fedora_elections.models.Vote + votes = vote.of_user_on_election(self.session, "nerdsville", '5') + self.assertEqual(votes[0].candidate_id, 8) + #Let's not do repetition of what is tested above we aren't testing the + #functionality of voting as that has already been asserted + + #Next, we need to try revoting + # Valid input + newdata = { + 'candidate': 9, + 'action': 'submit', + 'csrf_token': csrf_token, + } + output = self.app.post('/vote/test_election5', 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", '5') + self.assertEqual(votes[0].candidate_id, 9) + + #If we haven't failed yet, HOORAY! + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(