Implement BLT/JSON export

This commit is contained in:
RunasSudo 2017-12-15 22:35:49 +10:30
parent d4eb9ae5cf
commit 25a9a27434
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 61 additions and 0 deletions

47
eos/base/util/blt.py Normal file
View File

@ -0,0 +1,47 @@
# Eos - Verifiable elections
# pyRCV - Preferential voting counting
# Copyright © 2016–2017 RunasSudo (Yingtong Li)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def writeBLT(election, q_num, seats, withdrawn=[]):
question = election.questions[q_num]
flat_choices = question.flatten_choices()
electionLines = []
electionLines.append('{} {}\n'.format(len(flat_choices), seats))
if len(withdrawn) > 0:
electionLines.append(' '.join(['-{}'.format(flat_choices.index(candidate) + 1) for candidate in withdrawn]) + '\n')
result = election.results[q_num].count()
for answer, count in result:
if answer.choices:
electionLines.append('{} {} 0\n'.format(count, ' '.join(str(x + 1) for x in answer.choices)))
else:
electionLines.append('{} 0\n'.format(count))
electionLines.append('0\n')
for candidate in flat_choices:
if candidate.party:
electionLines.append("'{}{}'\n".format(candidate.name, candidate.party))
else:
electionLines.append("'{}'\n".format(candidate.name))
electionLines.append("'{}{}'\n".format(election.name, question.prompt))
return electionLines

View File

@ -34,6 +34,7 @@ from datetime import datetime
import functools
import importlib
import io
import json
import os
import pytz
@ -308,6 +309,15 @@ def election_api_cast_vote(election):
'vote': EosObject.serialise_and_wrap(vote, should_protect=True)
}), mimetype='application/json')
@app.route('/election/<election_id>/export/question/<int:q_num>/<format>')
@using_election
def election_api_export_question(election, q_num, format):
import eos.base.util.blt
#return flask.Response(''.join(eos.base.util.blt.writeBLT(election, q_num, 2)), mimetype='text/plain')
resp = flask.send_file(io.BytesIO(''.join(eos.base.util.blt.writeBLT(election, q_num, 2)).encode('utf-8')), mimetype='text/plain; charset=utf-8', attachment_filename='{}.blt'.format(q_num), as_attachment=True)
resp.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
return resp
@app.route('/auditor')
def auditor():
return flask.render_template('election/auditor.html')

View File

@ -81,6 +81,8 @@
{% if (session.user and session.user.is_admin() and election.workflow.get_task('eos.base.workflow.TaskReleaseResults').status == Status.READY) or election.workflow.get_task('eos.base.workflow.TaskReleaseResults').status == Status.EXITED %}
<h2>Results</h2>
<p><a href="{{ url_for('election_api_json', election_id=election._id) }}?full" class="mini ui labeled icon button"><i class="download icon"></i> Export as Eos JSON</a></p>
{% if election.workflow.get_task('eos.base.workflow.TaskReleaseResults').status == Status.EXITED %}
<p>Results were released at {{ election.workflow.get_task('eos.base.workflow.TaskReleaseResults').exited_at|pretty_date }}.</p>
{% else %}

View File

@ -16,6 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
<p><a href="{{ url_for('election_api_export_question', election_id=election._id, q_num=loop.index0, format='blt') }}" class="mini ui labeled icon button"><i class="download icon"></i> Export as OpenSTV BLT</a></p>
<table class="ui celled table">
{% for answer, num in election.results[loop.index0].count() %}
<tr>