Author: humbedooh
Date: Thu Feb 25 13:49:36 2016
New Revision: 1732303
URL: http://svn.apache.org/viewvc?rev=1732303&view=rev
Log:
Allow for more than 25 candidates for an STV election by switching from a->z to
AA->ZZ.
Also remove the now non-needed manual box as this works on tablets now.
Candidate preferences (votes) are now entered as "AA AB AC AD" instead of
"abcd" as we need to split() them using the whitespace.
Modified:
steve/trunk/pysteve/lib/plugins/stv.py
steve/trunk/pysteve/www/htdocs/js/steve_stv.js
Modified: steve/trunk/pysteve/lib/plugins/stv.py
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/stv.py?rev=1732303&r1=1732302&r2=1732303&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/stv.py (original)
+++ steve/trunk/pysteve/lib/plugins/stv.py Thu Feb 25 13:49:36 2016
@@ -30,14 +30,24 @@ from lib import constants
debug = []
+def makeLetter(num):
+ x = ord('A')
+ while num > 25:
+ x += 1
+ num -= 25
+ letter = chr(x) + chr(ord('A') + num)
+ return letter
+
def validateSTV(vote, issue):
"Tries to validate a vote, returns why if not valid, None otherwise"
- letters = [chr(i) for i in range(ord('a'), ord('a') +
len(issue['candidates']))]
+ # aa, ab, ac, ad.... az, ba, bb, bc, bd...bz, ca, cb, cc, cd...
+ letters = [makeLetter(x) for x in range(1,100)]
letters.append('-')
+ seats = vote.split(" ")
for char in letters:
- if vote.count(char) > 1:
+ if seats.count(char) > 1:
return "Duplicate letters found"
- for char in vote:
+ for char in seats:
if char not in letters:
return "Invalid characters in vote. Accepted are: %s" % ",
".join(letters)
return None
@@ -50,6 +60,7 @@ def run_vote(names, votes, num_seats):
remap = dict((c.name, c) for c in candidates.l)
# Turn VOTES into a list of ordered-lists of Candidate objects
+ letter = []
votes = [[remap[n] for n in choices] for choices in votes.values()]
if candidates.count(ELECTED + HOPEFUL) <= num_seats:
@@ -331,7 +342,7 @@ def tallySTV(votes, issue):
# Cut out abstained votes.
for vote in votes:
if votes[vote].find("-") == -1:
- rvotes[vote] = votes[vote]
+ rvotes[vote] = votes[vote].split(" ")
votes = rvotes
m = re.match(r"stv(\d+)", issue['type'])
if not m:
@@ -341,7 +352,7 @@ def tallySTV(votes, issue):
candidates = {}
z = 0
for c in issue['candidates']:
- candidates[chr(ord('a') + z)] = c['name']
+ candidates[makeLetter(z)] = c['name']
z += 1
# run the stv calc
@@ -353,7 +364,7 @@ def tallySTV(votes, issue):
# Return the data
return {
- 'votes': len(votes),
+ 'votes': len(rvotes),
'winners': winners,
'winnernames': winnernames,
'debug': debug
Modified: steve/trunk/pysteve/www/htdocs/js/steve_stv.js
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/www/htdocs/js/steve_stv.js?rev=1732303&r1=1732302&r2=1732303&view=diff
==============================================================================
--- steve/trunk/pysteve/www/htdocs/js/steve_stv.js (original)
+++ steve/trunk/pysteve/www/htdocs/js/steve_stv.js Thu Feb 25 13:49:36 2016
@@ -540,9 +540,22 @@ function loadIssue(election, issue, uid,
}
}
+function makeLetter(num) {
+ var x = String.charCodeAt('A')
+ while (num > 25) {
+ x += 1
+ num -= 25
+ }
+ var letter = String.fromCharCode(x) +
String.fromCharCode(String.charCodeAt('A') + num)
+ return letter
+}
+
function displayIssueSTV(code, response, state) {
initTouch()
- chars =
['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
// Corresponding STV letters, in same order as nominees
+ chars = [] // Corresponding STV letters, in same order as nominees
+ for (var z = 0; z <= 100; z++) {
+ chars.push(makeLetter(z))
+ }
election_data = response
if (code != 200) {
document.getElementById('preloaderWrapper').innerHTML = "<h1>Could not
load issue:</h1><h2>" + response.message + "</h2>";
@@ -623,20 +636,13 @@ function displayIssueSTV(code, response,
stvdiv.appendChild(document.createElement('br'))
stvdiv.appendChild(document.createElement('br'))
- var mbox = document.createElement('input')
- mbox.setAttribute("type", "text")
- mbox.setAttribute("name", "mbox")
- mbox.setAttribute("id", "mbox")
- mbox.style.width = "350px"
- mbox.setAttribute("placeholder", "Tablet folks: enter the order here
instead")
- stvdiv.appendChild(mbox)
shuffleCandidates();
drawCandidates();
document.getElementById('title').innerHTML = response.issue.title
document.title = response.issue.title + " - Apache STeVe"
-
+
}
}
@@ -646,7 +652,7 @@ function castVotes(args) {
election = l[0];
issue = l.length > 1 ? l[l.length-2] : "";
uid = l.length > 2 ? l[l.length-1] : "";
- var v = ballotChars.join("")
+ var v = ballotChars.join(" ")
if (v.length == 0 && document.getElementById('mbox').value.length > 0) {
v = document.getElementById('mbox').value
}