[Fedora Elections] #44: Rework FAS integration
by fedora-badges
#44: Rework FAS integration
-------------------------+-----------------------------
Reporter: pingou | Owner: nigelj
Type: enhancement | Status: new
Priority: major | Milestone: Future Release
Component: General | Version:
Keywords: | Blocked By:
Blocking: |
-------------------------+-----------------------------
At the moment we keep the user's FAS name in memcached but this is not
working so well and quite ofte we're stuck or very slow.
What we could do is add a new table or a new field in the candidate table,
to store locally the FAS name.
This way, when we add/edit candidates, we would query FAS for their name,
store it locally and forget about memcached.
--
Ticket URL: <https://fedorahosted.org/elections/ticket/44>
Fedora Elections <https://fedorahosted.org/elections/>
Fedora Elections
7 years, 2 months
Branch 'develop' - 7 commits - fedora_elections/elections.py fedora_elections/templates nosetests tests/test_flask_select_voting.py
by Pierre-YvesChibon
fedora_elections/elections.py | 41 ++++++++++++++----------
fedora_elections/templates/index.html | 2 -
nosetests | 4 +-
tests/test_flask_select_voting.py | 56 +++++++++++++++++++++++++++++-----
4 files changed, 75 insertions(+), 28 deletions(-)
New commits:
commit 8dfa93269c7888f97fc4ef786571683323470788
Merge: aa3ce6c 095eb79
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Tue Sep 1 12:00:33 2015 +0200
Merge pull request #50 from fedora-infra/fix_tests
Let's not require a specific version of nose to run the tests
commit 095eb79d91b2d30a4abbba454ec56442516aecc5
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Tue Sep 1 10:28:24 2015 +0200
Let's not require a specific version of nose to run the tests
diff --git a/nosetests b/nosetests
index 42c36f1..d050ee4 100755
--- a/nosetests
+++ b/nosetests
@@ -1,10 +1,10 @@
#!/usr/bin/env python
# EASY-INSTALL-ENTRY-SCRIPT: 'nose==1.3.0','console_scripts','nosetests'
-__requires__ = ['nose==1.3.0', 'flask>=0.10']
+__requires__ = ['nose', 'flask>=0.10']
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
- load_entry_point('nose==1.3.0', 'console_scripts', 'nosetests')()
+ load_entry_point('nose', 'console_scripts', 'nosetests')()
)
commit aa3ce6cc434433c2b072b99030ca72e24363d186
Merge: 728c885 cc5e577
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Tue Sep 1 11:48:06 2015 +0200
Merge pull request #49 from NerdsvilleCEO/add-select-revote
Add select revote
commit cc5e57713f14e0f6cb7702eca3b1333aecbf65e2
Author: Joshua Santos <Nerdsville(a)nerdsville.net>
Date: Tue Aug 11 14:54:34 2015 -0700
Update elections.py
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 4ca1ec3..f37cbfa 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -214,10 +214,10 @@ def vote_select(election, revote):
else:
if form.action.data == 'submit':
candidates = [
- candidate
- for candidate in form
- if candidate and candidate.short_name not in ['csrf_token', 'action']
- ]
+ 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:
commit 68f4c1aa7a818a7e693a3484452902d3a0c41bea
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Tue Aug 11 10:28:20 2015 -0700
Add template condition
diff --git a/fedora_elections/templates/index.html b/fedora_elections/templates/index.html
index 2add908..c796319 100644
--- a/fedora_elections/templates/index.html
+++ b/fedora_elections/templates/index.html
@@ -32,7 +32,7 @@
<a href="{{ url_for('vote', election_alias=election.alias) }}">
Vote now!
</a>
- {% elif election.voting_type == 'range' %}
+ {% elif election.voting_type == 'range' or election.voting_type == 'select' %}
<a href="{{ url_for('vote', election_alias=election.alias) }}">
Change your vote!
</a>
commit 2dbf5f16bf42ab9a7547be6b56007f5b54c0397a
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Tue Aug 11 10:25:35 2015 -0700
Fix description of select revote test
diff --git a/tests/test_flask_select_voting.py b/tests/test_flask_select_voting.py
index 845d94e..a9ba872 100644
--- a/tests/test_flask_select_voting.py
+++ b/tests/test_flask_select_voting.py
@@ -161,7 +161,7 @@ class FlaskSimpleElectionstests(ModelFlasktests):
self.assertTrue('<h3>Last 2 elections</h3>' in output.data)
def test_vote_select_revote(self):
- """ Test the vote_range function - the re-voting part. """
+ """ Test the vote_select function - the re-voting part. """
#First we need to vote
self.setup_db()
commit 1667763a8af6b6e2aa76fa3536e90f69d50882ba
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Tue Aug 11 10:24:09 2015 -0700
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!</'
- in output.data)
-
user = FakeUser(['packager'], username='pingou')
with user_set(fedora_elections.APP, user):
output = self.app.get(
@@ -168,6 +160,54 @@ class FlaskSimpleElectionstests(ModelFlasktests):
self.assertTrue('<h3>Next 1 elections</h3>' in output.data)
self.assertTrue('<h3>Last 2 elections</h3>' 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!</'
+ in output.data)
+ self.assertTrue('<h3>Current elections</h3>' in output.data)
+ self.assertTrue('<h3>Next 1 elections</h3>' in output.data)
+ self.assertTrue('<h3>Last 2 elections</h3>' 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(
8 years, 8 months
Branch 'develop' - 20 commits - fedora_elections/elections.py fedora_elections/__init__.py fedora_elections/models.py fedora_elections/templates files/fedora-elections.spec tests/__init__.py tests/test_flask_range_voting.py
by Pierre-YvesChibon
fedora_elections/__init__.py | 2 -
fedora_elections/elections.py | 45 ++++++++++++++------------
fedora_elections/models.py | 2 -
fedora_elections/templates/index.html | 4 ++
files/fedora-elections.spec | 7 +++-
tests/__init__.py | 3 +
tests/test_flask_range_voting.py | 58 +++++++++++++++++++++++++++++-----
7 files changed, 88 insertions(+), 33 deletions(-)
New commits:
commit 728c8852a4f107add514b81abc7ceb8ec0e1e9b2
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Mon Aug 10 18:08:48 2015 -0700
Remove extra whitespace
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 5cfe52b..1e6706c 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -154,12 +154,12 @@ def vote_range(election, revote):
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=candidate.data,
- )
+ election_id=election.id,
+ voter=flask.g.fas_user.username,
+ timestamp=datetime.now(),
+ candidate_id=candidate.short_name,
+ value=candidate.data,
+ )
SESSION.add(new_vote)
SESSION.commit()
commit b250393dd0810dd0321c14ce59fabab12d9a9490
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Mon Aug 10 17:29:46 2015 -0700
Indentation fix
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index ac8c320..5cfe52b 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -154,12 +154,12 @@ def vote_range(election, revote):
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=candidate.data,
- )
+ election_id=election.id,
+ voter=flask.g.fas_user.username,
+ timestamp=datetime.now(),
+ candidate_id=candidate.short_name,
+ value=candidate.data,
+ )
SESSION.add(new_vote)
SESSION.commit()
commit 795df2a6d83e8697f59a8a36101e8ef5d06eada1
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Mon Aug 10 13:33:34 2015 -0700
We have only implememnted range revote, add condition
diff --git a/fedora_elections/templates/index.html b/fedora_elections/templates/index.html
index 5d15ef8..2add908 100644
--- a/fedora_elections/templates/index.html
+++ b/fedora_elections/templates/index.html
@@ -32,7 +32,7 @@
<a href="{{ url_for('vote', election_alias=election.alias) }}">
Vote now!
</a>
- {% else %}
+ {% elif election.voting_type == 'range' %}
<a href="{{ url_for('vote', election_alias=election.alias) }}">
Change your vote!
</a>
commit d4db639baae8356719560aa9adbcc43ece2800f5
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Mon Aug 10 13:28:40 2015 -0700
use list comprehension instead of lambda
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 51e20f1..ac8c320 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -141,12 +141,13 @@ def vote_range(election, revote):
if form.validate_on_submit():
if form.action.data == 'submit':
- candidate_filter = lambda item: item.short_name not in ['csrf_token', 'action']
- candidates = filter(lambda candidate: candidate_filter(candidate), form)
- sorted_form = sorted(candidates, key=lambda candidate: candidate.short_name)
- iter = 0
- for index in range(len(sorted_form)):
- candidate = sorted_form[index]
+ 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 = candidate.data
commit 981ae199fb7b9742661a19e07b7d3e5800336f83
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Mon Aug 10 13:25:14 2015 -0700
no need to sort by cand_id, already done :D
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index fe0945b..51e20f1 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -126,8 +126,6 @@ def vote_range(election, revote):
votes = models.Vote.of_user_on_election(
SESSION, flask.g.fas_user.username, election.id)
- sorted_votes = sorted(votes, key=lambda candidate: candidate.candidate_id)
-
num_candidates = election.candidates.count()
cand_ids = [str(cand.id) for cand in election.candidates]
@@ -150,7 +148,7 @@ def vote_range(election, revote):
for index in range(len(sorted_form)):
candidate = sorted_form[index]
if revote:
- vote = sorted_votes[index]
+ vote = votes[index]
vote.value = candidate.data
SESSION.add(vote)
else:
commit 7d5e55a3c96c11cb1add431f7940b7c1781cdd64
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Aug 5 08:45:27 2015 -0700
Fix tests to use Vote class method for revote tests
diff --git a/tests/test_flask_range_voting.py b/tests/test_flask_range_voting.py
index a6f1eaa..c18aca9 100644
--- a/tests/test_flask_range_voting.py
+++ b/tests/test_flask_range_voting.py
@@ -307,11 +307,10 @@ class FlaskRangeElectionstests(ModelFlasktests):
}
self.app.post('/vote/test_election3', data=data, follow_redirects=True)
vote = fedora_elections.models.Vote
- votes = self.session.query(vote).filter(vote.voter == 'nerdsville')
- sorted_votes = sorted(votes, key=lambda vote: vote.candidate_id)
- self.assertEqual(sorted_votes[0].value, 1)
- self.assertEqual(sorted_votes[1].value, 0)
- self.assertEqual(sorted_votes[2].value, 2)
+ votes = vote.of_user_on_election(self.session, "nerdsville", '3')
+ self.assertEqual(votes[0].value, 1)
+ self.assertEqual(votes[1].value, 0)
+ self.assertEqual(votes[2].value, 2)
#Let's not do repetition of what is tested above we aren't testing the
#functionality of voting as that has already been asserted
@@ -332,11 +331,11 @@ class FlaskRangeElectionstests(ModelFlasktests):
self.assertTrue('<h3>Current elections</h3>' in output.data)
self.assertTrue('<h3>Next 1 elections</h3>' in output.data)
self.assertTrue('<h3>Last 2 elections</h3>' in output.data)
- votes = self.session.query(vote).filter(vote.voter == 'nerdsville')
- sorted_votes = sorted(votes, key=lambda vote: vote.candidate_id)
- self.assertEqual(sorted_votes[0].value, 2)
- self.assertEqual(sorted_votes[1].value, 1)
- self.assertEqual(sorted_votes[2].value, 1)
+ vote = fedora_elections.models.Vote
+ votes = vote.of_user_on_election(self.session, "nerdsville", '3')
+ self.assertEqual(votes[0].value, 2)
+ self.assertEqual(votes[1].value, 1)
+ self.assertEqual(votes[2].value, 1)
#If we haven't failed yet, HOORAY!
commit d8690fa12ef146c5d193b66dc95ce105dbc306c2
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Aug 5 08:40:26 2015 -0700
remove unused dependency
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 2a067ff..fe0945b 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -38,7 +38,6 @@ from fedora_elections import (
is_safe_url, safe_redirect_back,
)
from fedora_elections.utils import build_name_map
-from sqlalchemy import update
def login_required(f):
@wraps(f)
commit 061c5aecb945d0dd84bfc064a694e3db23ac192b
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Aug 5 08:39:16 2015 -0700
sort by candidate_id in Vote class method of_user_on_election
diff --git a/fedora_elections/models.py b/fedora_elections/models.py
index dff7cc4..b3e72cc 100644
--- a/fedora_elections/models.py
+++ b/fedora_elections/models.py
@@ -356,7 +356,7 @@ class Vote(BASE):
).filter(
cls.voter == user
).order_by(
- cls.timestamp
+ cls.candidate_id
)
if count:
commit 1b938fd60199c355866a9a68b97e5793bc3dc23a
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Aug 5 08:38:43 2015 -0700
Use for idx in range for loop
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index d7e512b..2a067ff 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -148,14 +148,12 @@ def vote_range(election, revote):
candidates = filter(lambda candidate: candidate_filter(candidate), form)
sorted_form = sorted(candidates, key=lambda candidate: candidate.short_name)
iter = 0
- for candidate in sorted_form:
-
+ for index in range(len(sorted_form)):
+ candidate = sorted_form[index]
if revote:
- vote = sorted_votes[iter]
+ vote = sorted_votes[index]
vote.value = candidate.data
SESSION.add(vote)
- iter += 1
-
else:
new_vote = models.Vote(
election_id=election.id,
commit ee55ebd7e1730610d68147ee80588f9400ead879
Author: Joshua Santos <nerdsville(a)fed.dev>
Date: Fri Jul 17 14:51:39 2015 -0400
Add revote link
diff --git a/fedora_elections/templates/index.html b/fedora_elections/templates/index.html
index 64b526b..5d15ef8 100644
--- a/fedora_elections/templates/index.html
+++ b/fedora_elections/templates/index.html
@@ -32,6 +32,10 @@
<a href="{{ url_for('vote', election_alias=election.alias) }}">
Vote now!
</a>
+ {% else %}
+ <a href="{{ url_for('vote', election_alias=election.alias) }}">
+ Change your vote!
+ </a>
{% endif %}
{% endif %}
</td>
commit a29d040c98f28e588b80cb4173c150bacd625144
Author: Joshua Santos <nerdsville(a)fed.dev>
Date: Fri Jul 17 14:42:57 2015 -0400
remove nose requirement
diff --git a/requirements.txt b/requirements.txt
index 4352ff8..4810410 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,4 +11,3 @@ SQLAlchemy
wtforms
dogpile.cache
blinker # required by Flask for the unit-tests (for signaling)
-nose===1.3.0
commit 0b43cb068fcd0a7d73cf1e7d740fda4eff639f1f
Author: Joshua Santos <nerdsville(a)fed.dev>
Date: Fri Jul 17 14:41:02 2015 -0400
move comment to two lines
diff --git a/tests/test_flask_range_voting.py b/tests/test_flask_range_voting.py
index e3d72eb..a6f1eaa 100644
--- a/tests/test_flask_range_voting.py
+++ b/tests/test_flask_range_voting.py
@@ -312,7 +312,9 @@ class FlaskRangeElectionstests(ModelFlasktests):
self.assertEqual(sorted_votes[0].value, 1)
self.assertEqual(sorted_votes[1].value, 0)
self.assertEqual(sorted_votes[2].value, 2)
- #Let's not do repetition of what is tested above we aren't testing the functionality of voting as that has already been asserted
+ #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
newdata = {
'4': 2,
commit 3a4af527cd53edba4f122822ce1c2cd9626c50f4
Author: Joshua Santos <nerdsville(a)fed.dev>
Date: Fri Jul 17 14:27:13 2015 -0400
Update revote to use ORM
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 55c1b8d..d7e512b 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -125,7 +125,9 @@ def vote(election_alias):
def vote_range(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)
+
+ sorted_votes = sorted(votes, key=lambda candidate: candidate.candidate_id)
num_candidates = election.candidates.count()
@@ -142,27 +144,27 @@ def vote_range(election, revote):
if form.validate_on_submit():
if form.action.data == 'submit':
- for candidate in form:
- if candidate.short_name in ['csrf_token', 'action']:
- continue
+ candidate_filter = lambda item: item.short_name not in ['csrf_token', 'action']
+ candidates = filter(lambda candidate: candidate_filter(candidate), form)
+ sorted_form = sorted(candidates, key=lambda candidate: candidate.short_name)
+ iter = 0
+ for candidate in sorted_form:
+
if revote:
- vote = update(models.Vote).\
- where(models.Vote.candidate_id == candidate.short_name
- and models.Vote.voter == flask.g.fas_user.username
- and models.Vote.election == election.id).\
- values(value = candidate.data)
- SESSION.execute(vote)
- #break out of candidate loop
- continue
+ vote = sorted_votes[iter]
+ vote.value = candidate.data
+ SESSION.add(vote)
+ iter += 1
- new_vote = models.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=candidate.data,
- )
- SESSION.add(new_vote)
+ )
+ SESSION.add(new_vote)
SESSION.commit()
flask.flash("Your vote has been recorded. Thank you!")
diff --git a/tests/test_flask_range_voting.py b/tests/test_flask_range_voting.py
index 2c7f792..e3d72eb 100644
--- a/tests/test_flask_range_voting.py
+++ b/tests/test_flask_range_voting.py
@@ -293,7 +293,7 @@ class FlaskRangeElectionstests(ModelFlasktests):
#First we need to vote
self.setup_db()
- user = FakeUser(['voters'], username='pingou')
+ user = FakeUser(['voters'], username='nerdsville')
with user_set(fedora_elections.APP, user):
retrieve_csrf = self.app.post('/vote/test_election3')
csrf_token = retrieve_csrf.data.split(
@@ -306,6 +306,12 @@ class FlaskRangeElectionstests(ModelFlasktests):
'csrf_token': csrf_token,
}
self.app.post('/vote/test_election3', data=data, follow_redirects=True)
+ vote = fedora_elections.models.Vote
+ votes = self.session.query(vote).filter(vote.voter == 'nerdsville')
+ sorted_votes = sorted(votes, key=lambda vote: vote.candidate_id)
+ self.assertEqual(sorted_votes[0].value, 1)
+ self.assertEqual(sorted_votes[1].value, 0)
+ self.assertEqual(sorted_votes[2].value, 2)
#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
newdata = {
@@ -324,6 +330,11 @@ class FlaskRangeElectionstests(ModelFlasktests):
self.assertTrue('<h3>Current elections</h3>' in output.data)
self.assertTrue('<h3>Next 1 elections</h3>' in output.data)
self.assertTrue('<h3>Last 2 elections</h3>' in output.data)
+ votes = self.session.query(vote).filter(vote.voter == 'nerdsville')
+ sorted_votes = sorted(votes, key=lambda vote: vote.candidate_id)
+ self.assertEqual(sorted_votes[0].value, 2)
+ self.assertEqual(sorted_votes[1].value, 1)
+ self.assertEqual(sorted_votes[2].value, 1)
#If we haven't failed yet, HOORAY!
commit e7ccc3bb8b19a934d420da1478c01326cb755d47
Author: Joshua Santos <nerdsville(a)fed.dev>
Date: Fri Jul 17 10:17:22 2015 -0400
Need to filter candidates before even checking if it is revote
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index dffa9ec..55c1b8d 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -143,6 +143,8 @@ def vote_range(election, revote):
if form.validate_on_submit():
if form.action.data == 'submit':
for candidate in form:
+ if candidate.short_name in ['csrf_token', 'action']:
+ continue
if revote:
vote = update(models.Vote).\
where(models.Vote.candidate_id == candidate.short_name
@@ -152,8 +154,6 @@ def vote_range(election, revote):
SESSION.execute(vote)
#break out of candidate loop
continue
- if candidate.short_name in ['csrf_token', 'action']:
- continue
new_vote = models.Vote(
election_id=election.id,
commit 63f3ce19879b44be7d3998b63fde86e81a37dd38
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Jul 15 04:23:09 2015 -0700
Apparently the last voter is not a dictator :P
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 753e45b..dffa9ec 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -145,7 +145,9 @@ def vote_range(election, revote):
for candidate in form:
if revote:
vote = update(models.Vote).\
- where(models.Vote.candidate_id == candidate.short_name and models.Vote.voter == flask.g.fas_user.username).\
+ where(models.Vote.candidate_id == candidate.short_name
+ and models.Vote.voter == flask.g.fas_user.username
+ and models.Vote.election == election.id).\
values(value = candidate.data)
SESSION.execute(vote)
#break out of candidate loop
commit 32a2cea381423192ab9afe6a8cbb728baa159afa
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Jul 15 04:20:55 2015 -0700
Apparently the last voter is not a dictator :P
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index 07ddb02..753e45b 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -142,12 +142,10 @@ def vote_range(election, revote):
if form.validate_on_submit():
if form.action.data == 'submit':
- if revote:
- old_votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id)
for candidate in form:
if revote:
vote = update(models.Vote).\
- where(models.Vote.candidate_id == candidate.short_name).\
+ where(models.Vote.candidate_id == candidate.short_name and models.Vote.voter == flask.g.fas_user.username).\
values(value = candidate.data)
SESSION.execute(vote)
#break out of candidate loop
commit afdde721c525702ba0630511f26bf16c20843df1
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Jul 15 04:13:21 2015 -0700
Use update instead of nested for
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index a8efb2a..07ddb02 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -38,7 +38,7 @@ from fedora_elections import (
is_safe_url, safe_redirect_back,
)
from fedora_elections.utils import build_name_map
-
+from sqlalchemy import update
def login_required(f):
@wraps(f)
@@ -146,13 +146,10 @@ def vote_range(election, revote):
old_votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id)
for candidate in form:
if revote:
- #TODO: this could probably be done in a more efficient manner
- for vote in old_votes:
- if vote.candidate_id == candidate.short_name:
- vote.value = candidate.data
- SESSION.commit()
- #break out of this loop
- continue
+ vote = update(models.Vote).\
+ where(models.Vote.candidate_id == candidate.short_name).\
+ values(value = candidate.data)
+ SESSION.execute(vote)
#break out of candidate loop
continue
if candidate.short_name in ['csrf_token', 'action']:
commit a14f385221e59ad9c811bb3fa5541db3faa83e80
Author: Joshua Santos <nerdsville(a)nerdsville.net>
Date: Wed Jul 15 03:21:59 2015 -0700
Add range revoting
diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py
index d46db72..a8efb2a 100644
--- a/fedora_elections/elections.py
+++ b/fedora_elections/elections.py
@@ -108,12 +108,10 @@ def vote(election_alias):
votes = models.Vote.of_user_on_election(
SESSION, flask.g.fas_user.username, election.id, count=True)
- if votes > 0:
- flask.flash('You have already voted in the election!')
- return safe_redirect_back()
+ revote = True if votes > 0 else False
if election.voting_type.startswith('range'):
- return vote_range(election)
+ return vote_range(election, revote)
elif election.voting_type == 'simple':
return vote_simple(election)
elif election.voting_type == 'select':
@@ -125,8 +123,7 @@ def vote(election_alias):
'Unknown election voting type: %s' % election.voting_type)
return safe_redirect_back()
-
-def vote_range(election):
+def vote_range(election, revote):
votes = models.Vote.of_user_on_election(
SESSION, flask.g.fas_user.username, election.id, count=True)
@@ -144,9 +141,20 @@ def vote_range(election):
max_range=max_selection)
if form.validate_on_submit():
-
if form.action.data == 'submit':
+ if revote:
+ old_votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id)
for candidate in form:
+ if revote:
+ #TODO: this could probably be done in a more efficient manner
+ for vote in old_votes:
+ if vote.candidate_id == candidate.short_name:
+ vote.value = candidate.data
+ SESSION.commit()
+ #break out of this loop
+ continue
+ #break out of candidate loop
+ continue
if candidate.short_name in ['csrf_token', 'action']:
continue
diff --git a/requirements.txt b/requirements.txt
index 4810410..4352ff8 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,3 +11,4 @@ SQLAlchemy
wtforms
dogpile.cache
blinker # required by Flask for the unit-tests (for signaling)
+nose===1.3.0
diff --git a/tests/test_flask_range_voting.py b/tests/test_flask_range_voting.py
index d0be838..2c7f792 100644
--- a/tests/test_flask_range_voting.py
+++ b/tests/test_flask_range_voting.py
@@ -55,14 +55,6 @@ class FlaskRangeElectionstests(ModelFlasktests):
self.setup_db()
- user = FakeUser(['voters'], username='toshio')
- with user_set(fedora_elections.APP, user):
- output = self.app.get(
- '/vote/test_election3', follow_redirects=True)
- self.assertTrue(
- 'class="message">You have already voted in the election!</'
- in output.data)
-
user = FakeUser(['voters'], username='pingou')
with user_set(fedora_elections.APP, user):
output = self.app.get(
@@ -296,6 +288,44 @@ class FlaskRangeElectionstests(ModelFlasktests):
self.assertTrue('<h3>Next 1 elections</h3>' in output.data)
self.assertTrue('<h3>Last 2 elections</h3>' in output.data)
+ def test_vote_range_revote(self):
+ """ Test the vote_range function - the re-voting part. """
+ #First we need to vote
+ self.setup_db()
+
+ user = FakeUser(['voters'], username='pingou')
+ with user_set(fedora_elections.APP, user):
+ retrieve_csrf = self.app.post('/vote/test_election3')
+ csrf_token = retrieve_csrf.data.split(
+ 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
+ data = {
+ '4': 1,
+ '5': 0,
+ '6': 2,
+ 'action': 'submit',
+ 'csrf_token': csrf_token,
+ }
+ self.app.post('/vote/test_election3', data=data, follow_redirects=True)
+ #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
+ newdata = {
+ '4': 2,
+ '5': 1,
+ '6': 1,
+ 'action': 'submit',
+ 'csrf_token': csrf_token,
+ }
+ output = self.app.post('/vote/test_election3', 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!</'
+ in output.data)
+ self.assertTrue('<h3>Current elections</h3>' in output.data)
+ self.assertTrue('<h3>Next 1 elections</h3>' in output.data)
+ self.assertTrue('<h3>Last 2 elections</h3>' in output.data)
+ #If we haven't failed yet, HOORAY!
+
if __name__ == '__main__':
SUITE = unittest.TestLoader().loadTestsFromTestCase(
commit f778ec9c556419915952e5db6fa21aa3c047b1ce
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Tue Aug 4 15:30:48 2015 +0200
Adjust the faitout url to the new address
diff --git a/tests/__init__.py b/tests/__init__.py
index 08c6833..eecfc4a 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -49,7 +49,8 @@ from fedora_elections import models
DB_PATH = 'sqlite:///:memory:'
-FAITOUT_URL = 'http://209.132.184.152/faitout/'
+FAITOUT_URL = 'http://faitout.fedorainfracloud.org/'
+
if os.environ.get('BUILD_ID'):
try:
import requests
commit 7f01a48e58fc2bd9ed4d92217e016f0711e9df7e
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Thu May 28 11:27:16 2015 +0200
Release 2.5.4
diff --git a/fedora_elections/__init__.py b/fedora_elections/__init__.py
index 4e6273c..c08a61c 100644
--- a/fedora_elections/__init__.py
+++ b/fedora_elections/__init__.py
@@ -24,7 +24,7 @@
# Pierre-Yves Chibon <pingou(a)fedoraproject.org>
#
-__version__ = '2.5.3'
+__version__ = '2.5.4'
import logging
import os
diff --git a/files/fedora-elections.spec b/files/fedora-elections.spec
index 96660d9..1fa4d0f 100644
--- a/files/fedora-elections.spec
+++ b/files/fedora-elections.spec
@@ -1,7 +1,7 @@
%define modname fedora_elections
Name: fedora-elections
-Version: 2.5.3
+Version: 2.5.4
Release: 1%{?dist}
Summary: Fedora elections application
@@ -111,6 +111,11 @@ install -m 644 files/update_1_to_2.sql \
%changelog
+* Thu May 28 2015 Pierre-Yves Chibon <pingou(a)pingoured.fr> - 2.5.4-1
+- Update to 2.5.4
+- Fix displaying information about the simplified range voting method
+- Fix setting the max_vote value in the stats dict for the results page
+
* Tue May 05 2015 Pierre-Yves Chibon <pingou(a)pingoured.fr> - 2.5.3-1
- Update to 2.5.3
- Fix the simplified range voting
8 years, 8 months