-
Notifications
You must be signed in to change notification settings - Fork 1
/
irv.py
223 lines (182 loc) · 6.8 KB
/
irv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from functools import wraps
from flask import Flask, request, render_template, Response, jsonify, url_for
from flask_mail import Mail, Message
from sqlalchemy import create_engine, Table, Column, Integer, String, ForeignKey, UniqueConstraint
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.orderinglist import ordering_list
from uuid import uuid4
app = Flask(__name__)
app.config.from_pyfile('irv.cfg')
mail = Mail(app)
class InvalidUsage(Exception):
status_code = 400
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
engine = create_engine(app.config['DATABASE'], echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Election(Base):
__tablename__ = 'elections'
id = Column(Integer, primary_key=True)
name = Column(String)
key = Column(String)
positions = relationship('Position', order_by='Position.rank',
collection_class=ordering_list('rank'),
back_populates='election')
voters = relationship('Voter', back_populates='election')
def __str__(self):
return self.name
class Position(Base):
__tablename__ = 'positions'
id = Column(Integer, primary_key=True)
name = Column(String)
rank = Column(Integer)
election_id = Column(Integer, ForeignKey('elections.id'))
election = relationship('Election', back_populates='positions')
candidates = relationship('Candidate', back_populates='position')
def __str__(self):
return self.name
class Candidate(Base):
__tablename__ = 'candidates'
id = Column(Integer, primary_key=True)
name = Column(String)
position_id = Column(Integer, ForeignKey('positions.id'))
position = relationship('Position', back_populates='candidates')
votes = relationship('Vote', back_populates='candidate')
def __str__(self):
return self.name
class Vote(Base):
__tablename__ = 'votes'
__table_args__ = (
UniqueConstraint('voter_id', 'candidate_id', name='unique_rank_candidate'),
UniqueConstraint('rank', 'candidate_id', name='unique_rank_candidate'),
)
id = Column(Integer, primary_key=True)
rank = Column(Integer)
candidate_id = Column(Integer, ForeignKey('candidates.id'))
voter_id = Column(Integer, ForeignKey('voters.id'))
candidate = relationship('Candidate', back_populates='votes')
voter = relationship('Voter', back_populates='votes')
class Voter(Base):
__tablename__ = 'voters'
id = Column(Integer, primary_key=True)
key = Column(String)
election_id = Column(Integer, ForeignKey('elections.id'))
election = relationship('Election', back_populates='voters')
votes = relationship('Vote', back_populates='voter')
Base.metadata.create_all(engine)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/election', methods = ['GET', 'POST'])
def create_election():
"""Create an Election given a JSON description.
Example JSON request:
{
"name": "test election",
"admin_email": "[email protected]",
"voter_emails": [
],
"positions": [
{
"name": "test ballot",
"candidates": [
"test candidate"
]
}
]
}
"""
if request.method == 'GET':
return render_template('create.html')
position_rank = 0
election = Election(name=request.json['name'])
election.key = str(uuid4())
for position_json in request.json['positions']:
position = Position(name=position_json['name'])
position.election = election
position.rank = position_rank
position_rank += 1
for candidate_name in position_json['candidates']:
candidate = Candidate(name=candidate_name)
candidate.position = position
session.add(election)
session.commit()
manage_url = url_for("manage_election", election_id=election.id,
_external=True) + "#" + election.key
mail.send(Message("Election Created",
recipients=[request.json['admin_email']],
body=manage_url))
for email in request.json['voter_emails']:
voter = Voter()
voter.election = election
voter.key = str(uuid4())
voter_url = url_for("vote_election", election_id=election.id,
_external=True) + "#" + voter.key
mail.send(Message("Vote in New Election",
recipients=[email],
body=voter_url))
session.add(election)
session.commit()
return jsonify({
"redirect": manage_url
})
@app.route('/election/<int:election_id>/manage', methods = ['GET', 'POST'])
def manage_election(election_id):
if request.method == 'GET':
return render_template('manage.html')
return ''
@app.route('/election/<int:election_id>/vote', methods = ['GET', 'POST'])
def vote_election(election_id):
if request.method == 'GET':
positions = session.query(Position).\
filter(Position.election_id == election_id).\
all()
return render_template('vote.html', positions=positions)
voter = session.query(Voter).\
filter(Voter.key == request.json['key']).\
filter(Voter.election_id == election_id).\
one()
old_votes = session.query(Vote).\
filter(Vote.voter==voter).\
join(Vote.candidate).\
join(Candidate.position).\
filter(Position.election_id==election_id).\
all()
for old_vote in old_votes:
session.delete(old_vote)
session.commit()
for ballot in request.json['votes']:
position = session.query(Position).\
filter(Position.id == ballot['position_id']).\
one()
rank = 1
for candidate_id in ballot['candidate_ids']:
vote = Vote(rank=rank, candidate_id=candidate_id, voter=voter)
rank += 1
session.add(voter)
session.commit()
return jsonify({
"redirect": url_for("results_election", election_id=election_id, _external=True)
})
@app.route('/election/<int:election_id>/results', methods = ['GET'])
def results_election(election_id):
return render_template('results.html')
if __name__ == '__main__':
app.run()