Author: humbedooh
Date: Wed Mar 25 01:28:53 2015
New Revision: 1669038

URL: http://svn.apache.org/r1669038
Log:
Add First Past the Post voting as well, why not...

Added:
    steve/trunk/pysteve/lib/plugins/fpp.py
    steve/trunk/pysteve/www/htdocs/ballot_fpp.html
Modified:
    steve/trunk/pysteve/lib/plugins/__init__.py
    steve/trunk/pysteve/www/htdocs/js/steve_dh.js
    steve/trunk/pysteve/www/htdocs/js/steve_rest.js

Modified: steve/trunk/pysteve/lib/plugins/__init__.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/__init__.py?rev=1669038&r1=1669037&r2=1669038&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/plugins/__init__.py (original)
+++ steve/trunk/pysteve/lib/plugins/__init__.py Wed Mar 25 01:28:53 2015
@@ -18,6 +18,7 @@
 yna
 stv
 dh
+fpp
 """
 
-__all__ = ['yna','stv','dh']
\ No newline at end of file
+__all__ = ['yna','stv','dh','fpp']
\ No newline at end of file

Added: steve/trunk/pysteve/lib/plugins/fpp.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/plugins/fpp.py?rev=1669038&view=auto
==============================================================================
--- steve/trunk/pysteve/lib/plugins/fpp.py (added)
+++ steve/trunk/pysteve/lib/plugins/fpp.py Wed Mar 25 01:28:53 2015
@@ -0,0 +1,83 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+""" FPP (First Past the Post) Based Voting Plugin """
+import re, json
+
+from lib import constants
+
+def validateFPP(vote, issue):
+    "Tries to invalidate a vote, returns why if succeeded, None otherwise"
+    letters = [chr(i) for i in range(ord('a'), ord('a') + 
len(issue['candidates']))]
+    if len(vote) > 1:
+        return "Vote may only contain one letter!"
+    for char in vote:
+        if char not in letters:
+            return "Invalid characters in vote. Accepted are: %s" % ", 
".join(letters)
+    return None
+
+
+def tallyFPP(votes, issue):
+    candidates = []
+    for c in issue['candidates']:
+        candidates.append(c['name'])
+    
+
+    debug = []
+    matrix = {}
+    
+    # Set up counting matrix
+    for key in votes:
+        vote = votes[key]
+        matrix[vote] = (matrix[vote] if vote in matrix else 0) + 1
+    
+    l = []
+    for x in matrix:
+        l.append(matrix[x])
+        
+    cc = []
+    for x in matrix:
+        if matrix[x] == max(l):
+            cc.append(x)
+    winners = []
+    winnernames = []
+    
+    for c in cc:
+        i = ord(c) - ord('a')
+        winners.append(c)
+        winnernames.append(candidates[i])
+
+
+    # Return the data
+    return {
+        'votes': len(votes),
+        'winners': winners,
+        'winnernames': winnernames,
+        'winnerpct': ((1.00*max(l)/len(votes))*100) if len(votes) > 0 else 
0.00,
+        'tie': True if len(winners) > 1 else False
+    }
+
+
+constants.VOTE_TYPES += (
+    {
+        'key': "fpp",
+        'description': "First Past the Post (FPP) Election",
+        'category': 'fpp',
+        'validate_func': validateFPP,
+        'vote_func': None,
+        'tally_func': tallyFPP
+    },
+)

Added: steve/trunk/pysteve/www/htdocs/ballot_fpp.html
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/www/htdocs/ballot_fpp.html?rev=1669038&view=auto
==============================================================================
--- steve/trunk/pysteve/www/htdocs/ballot_fpp.html (added)
+++ steve/trunk/pysteve/www/htdocs/ballot_fpp.html Wed Mar 25 01:28:53 2015
@@ -0,0 +1,39 @@
+ <!DOCTYPE HTML>
+<html>
+<head>
+<meta charset="utf-8">
+<link rel="stylesheet" href="css/steve_interactive.css">
+<link rel="stylesheet" href="css/jquery-ui.css">
+<script src="js/steve_rest.js" type="text/javascript"></script>
+<script src="js/steve_dh.js" type="text/javascript"></script>
+<script src="js/jquery.js" type="text/javascript"></script>
+<script src="js/jquery-ui.js" type="text/javascript"></script>
+<title>Apache STeVe: D'Hondt vote</title>
+
+</head>
+<body onload="window.setTimeout(loadIssue, 500, null, null, null, 
displayIssueDH);">
+    <div id="popups"></div>
+    <p style="text-align: center;">
+        <img src="/images/steve_logo.png"/>
+    </p>
+    <a href="javascript:void(location.href='election.html' + 
document.location.search);">Back to election front page</a>
+<div class="formbox">
+    <p>
+        This is a standard First Past the Post election. The person/party with 
the most votes wins the election.
+        To vote, click on the candidate of your choice and then click on 
<kbd>Cast vote</kbd>.
+        Should you reconsider later, you can always recast your vote for as 
long as the election remains open.
+    </p>
+<div id="preloaderWrapper">
+    <img src="/images/steve_spinner.gif"/>
+    <div id="preloader">
+        Loading issue, please wait...
+    </div>
+</div>
+</div>
+<p style="font-size: 12px; font-style: italic; text-align: center;">
+    Powered by <a href="https://steve.apache.org/";>Apache STeVe</a>.
+    Copyright 2015, the Apache Software Foundation.
+    Licensed under the <a 
href="http://www.apache.org/licenses/LICENSE-2.0";>Apache License 2.0</a>
+</p>
+</body>
+</html>
\ No newline at end of file

Modified: steve/trunk/pysteve/www/htdocs/js/steve_dh.js
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/www/htdocs/js/steve_dh.js?rev=1669038&r1=1669037&r2=1669038&view=diff
==============================================================================
--- steve/trunk/pysteve/www/htdocs/js/steve_dh.js (original)
+++ steve/trunk/pysteve/www/htdocs/js/steve_dh.js Wed Mar 25 01:28:53 2015
@@ -140,8 +140,8 @@ function displayIssueDH(code, response,
             candidates.push(candidate.name);
             statements[chars[c]] = candidate.statement;
         }
-        document.getElementById('cnum').innerHTML = candidates.length
-        document.getElementById('snum').innerHTML = seats        
+        if (document.getElementById('cnum')) 
document.getElementById('cnum').innerHTML = candidates.length
+        if (document.getElementById('snum')) 
document.getElementById('snum').innerHTML = seats        
         while (chars.length > candidates.length) chars.splice(-1,1)
         
         

Modified: steve/trunk/pysteve/www/htdocs/js/steve_rest.js
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/www/htdocs/js/steve_rest.js?rev=1669038&r1=1669037&r2=1669038&view=diff
==============================================================================
--- steve/trunk/pysteve/www/htdocs/js/steve_rest.js (original)
+++ steve/trunk/pysteve/www/htdocs/js/steve_rest.js Wed Mar 25 01:28:53 2015
@@ -149,7 +149,8 @@ function displayTally(code, response, is
                        for (i in response.winners) {
                                var winner = response.winners[i]
                                var winnerName = response.winnernames[i]
-                               obj.innerHTML += "<li>" + winner + ": " + 
winnerName + "</li>"
+                               var pct = response.winnerpct ? " (" + 
response.winnerpct + "%)" : ""
+                               obj.innerHTML += "<li>" + winner + ": " + 
winnerName + pct + "</li>"
                        }
                        obj.innerHTML += "</ol>"
                } else if (response.yes && response.yes != undefined) {


Reply via email to