elections2/controllers/root.py | 27 +++++++++++++++++++++------
elections2/lib/vote.py | 11 ++++++-----
elections2/model/__init__.py | 38 ++------------------------------------
elections2/templates/results.html | 13 ++++++++++++-
elections2/templates/verify.html | 12 ++++++++++--
5 files changed, 51 insertions(+), 50 deletions(-)
New commits:
commit 7b6678f82d54a083ef6e8c03c79785cb22c5db5d
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Wed Dec 14 13:47:33 2011 +0100
Code layout in the template
diff --git a/elections2/templates/verify.html b/elections2/templates/verify.html
index 0dbea59..c5b0d59 100644
--- a/elections2/templates/verify.html
+++ b/elections2/templates/verify.html
@@ -11,11 +11,19 @@
<p>Please check the below content to ensure that all of your cast votes are
marked as 'valid', if you notice any elections showing that you have an invalid
vote cast please contact the election administration team by creating a ticket at the
<a
href="https://fedorahosted.org/fedora-infrastructure/">Fedora
Infrastructure ticket system</a> and select the 'Elections' component with
your FAS username and the election that is marked as invalid.</p>
<h2 py:if="validvotes">Valid Votes</h2>
<ul py:if="validvotes">
- <li py:for="vote in validvotes"><a
href="${tg.url('/about/' +
validvotes[vote].election.id)}">${validvotes[vote].election.shortdesc}</a></li>
+ <li py:for="vote in validvotes">
+ <a href="${tg.url('/about/' +
validvotes[vote].election.id)}">
+ ${validvotes[vote].election.shortdesc}
+ </a>
+ </li>
</ul>
<h2 py:if="invalidvotes">Invalid Votes</h2>
<ul py:if="invalidvotes">
- <li py:for="vote in invalidvotes"><a
href="${tg.url('/about/' +
invalidvotes[vote].election.id)}">${invalidvotes[vote].election.shortdesc}</a></li>
+ <li py:for="vote in invalidvotes">
+ <a href="${tg.url('/about/' +
invalidvotes[vote].election.id)}">
+ ${invalidvotes[vote].election.shortdesc}
+ </a>
+ </li>
</ul>
</body>
commit dabbcf6f0adae3bf4bd4cf5c1cf606c2a88d1a4e
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Wed Dec 14 13:47:26 2011 +0100
Start removing View from the application.
Remove View fvotecount obj: VoteTally
Remove View uservotes obj: UserVoteCount
Adjust queries
Adjust results template to the new query
diff --git a/elections2/controllers/root.py b/elections2/controllers/root.py
index 430d6b7..ca672a3 100644
--- a/elections2/controllers/root.py
+++ b/elections2/controllers/root.py
@@ -23,6 +23,7 @@ from elections2.lib.vote import Vote
from datetime import datetime
+from sqlalchemy import func
__all__ = ['RootController']
@@ -138,8 +139,11 @@ class RootController(BaseController):
validvotes = {}
invalidvotes = {}
c = 0
- allvotes = UserVoteCount.query.filter_by(voter=
- turbogears.identity.current.user_name).all()
+ print request.identity['user']
+ allvotes = model.DBSession.query(Votes.election_id,
+ Votes.voter, func.count(Votes.voter)
+ ).filter_by(voter=request.identity['user']
+ ).group_by(Votes.voter).all()
for v in allvotes:
if len(v.election.candidates) == v.novotes:
validvotes[c] = v
@@ -188,10 +192,12 @@ class RootController(BaseController):
def results(self, eid=None):
try:
eid = int(eid)
- election = model.DBSession.query(Elections).filter_by(id=eid).all()[0]
+ election = model.DBSession.query(Elections).filter_by(
+ id=eid).all()[0]
except ValueError:
try:
- election =
model.DBSession.query(Elections).filter_by(alias=eid).all()[0]
+ election = model.DBSession.query(Elections).filter_by(
+ alias=eid).all()[0]
eid = election.id
except IndexError:
flash("This election does not exist, check"\
@@ -238,8 +244,17 @@ class RootController(BaseController):
" are currently embargoed pending formal"
" announcement.")
raise turbogears.redirect("/")
- votecount = model.DBSession.query(VoteTally).filter_by(election_id=eid
- ).order_by(VoteTally.novotes.desc()).all()
+ votecount = model.DBSession.query(Candidates,
+ func.sum(Votes.weight).label('novotes'
+ )).filter(
+ Candidates.id == Votes.candidate_id
+ ).filter_by(
+ election_id=eid,
+ ).order_by(
+ func.sum(Votes.weight).desc()
+ ).all()
+
+
return dict(votecount=votecount, usernamemap=usernamemap,
election=election, appTitle=self.appTitle)
diff --git a/elections2/lib/vote.py b/elections2/lib/vote.py
index e5c3547..81e8ccf 100644
--- a/elections2/lib/vote.py
+++ b/elections2/lib/vote.py
@@ -34,9 +34,10 @@ from elections2.lib.base import BaseController
from elections2 import model
from elections2.model import Elections, LegalVoters, Candidates, \
- Votes, UserVoteCount
+ Votes
import sqlalchemy
+from sqlalchemy import func
#from turbogears.database import session
@@ -87,7 +88,7 @@ class Vote(BaseController):
usergroups if re.match("cla_.*",g)])
):
match = 1
- elif request.identity.in_group(group.group_name) or \
+ elif predicates.in_group(group.group_name) or \
group.group_name == "anyany":
match = 1
if match == 0:
@@ -99,9 +100,9 @@ class Vote(BaseController):
candidates = model.DBSession.query(Candidates
).filter_by(election_id=eid
).order_by(Candidates.name).all()
- uservote = model.DBSession.query(UserVoteCount
- ).filter_by(election_id=eid,
- voter=request.identity['username']).all()
+ uservote = model.DBSession.query(Votes.election_id,
+ Votes.voter, func.count(Votes.voter)
+ ).group_by(Votes.voter).all()
usernamemap = {}
diff --git a/elections2/model/__init__.py b/elections2/model/__init__.py
index c9dce69..8c7e2db 100644
--- a/elections2/model/__init__.py
+++ b/elections2/model/__init__.py
@@ -101,40 +101,6 @@ class ElectionAdmins(DeclarativeBase):
return self.group_name
#}
-class VoteTally(DeclarativeBase):
- __tablename__ = 'fvotecount'
-
- #{ Columns
- id = Column(Integer, ForeignKey('candidates.id'), primary_key=True)
- name = Column(String, nullable=False)
- novotes = Column(Integer, nullable=False)
-
- #{ Special methods
- def __repr__(self):
- return '<VoteTally: id=%r election=%r group=%r>' % ( self.id,
- self.election_id, self.group_name)
-
- def __unicode__(self):
- return self.group_name
- #}
-
-class UserVoteCount(DeclarativeBase):
-
- __tablename__ = 'uservotes'
-
- #{ Columns
- election_id = Column(Integer, ForeignKey('elections.id'), primary_key=True)
- voter = Column(String, nullable=False, primary_key=True)
- novotes = Column(Integer, nullable=False)
-
- #{ Special methods
- def __repr__(self):
- return '<UserVoteCount: id=%r voter=%r novotes=%r>' % (
- self.election_id, self.voter, self.novotes)
-
- def __unicode__(self):
- return self.group_name
- #}
class Elections(DeclarativeBase):
"""
@@ -160,7 +126,7 @@ class Elections(DeclarativeBase):
#{ Relations
legalVoters = relation('LegalVoters')
candidates = relation('Candidates')
- uservotes = relation('UserVoteCount')
+ #uservotes = relation('UserVoteCount')
#{ Special methods
def __repr__(self):
@@ -211,7 +177,7 @@ class Candidates(DeclarativeBase):
status = Column(Integer)
#{ Relations
- #votes = relation('Votes', backref='candidate'),
+ votes = relation('Votes', backref='candidate'),
#tally = relation('VoteTally', backref='candidate')
#{ Special methods
diff --git a/elections2/templates/results.html b/elections2/templates/results.html
index d2d142d..7570a55 100644
--- a/elections2/templates/results.html
+++ b/elections2/templates/results.html
@@ -12,7 +12,18 @@
<p py:if="election.url"><a
href="${election.url}">[More Information]</a></p>
<table border="1" cellpadding="1">
<tr py:for="candcount in votecount">
- <td><py:choose test="election.usefas"><py:when
test="0">${candcount.candidate.name}</py:when><py:when
test="1">${usernamemap[candcount.candidate.id]}</py:when></py:choose>
<small py:if="candcount.candidate.url"><a
href="${candcount.candidate.url}">[info]</a></small></td>
+ <td><py:choose test="election.usefas">
+ <py:when test="0">
+ ${candcount.Candidates.name}
+ </py:when>
+ <py:when test="1">
+ ${usernamemap[candcount.Candidates.id]}
+ </py:when>
+ </py:choose>
+ <small py:if="candcount.Candidates.url">
+ <a href="${candcount.Candidates.url}">[info]</a>
+ </small>
+ </td>
<td>${candcount.novotes}</td>
</tr>
</table>