/
ballot.py
79 lines (70 loc) · 2.73 KB
/
ballot.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
from gluon import *
import re, hashlib, base64
import rsa
import json
import random
import cPickle as pickle
from uuid import uuid4
try:
import ast
have_ast=True
except:
have_ast=False
regex_field = re.compile('{{(\w+)(\:\w+)?\!?}}')
regex_email = re.compile('[^\s<>"\',;]+\@[^\s<>"\',;]+',re.IGNORECASE)
def uuid():
return str(uuid4()).replace('-','').upper()
def rsakeys():
(pubkey,privkey) = rsa.newkeys(1024)
return (pubkey.save_pkcs1(), privkey.save_pkcs1())
def sign(text,privkey_pem):
privkey = rsa.PrivateKey.load_pkcs1(privkey_pem)
signature = base64.b16encode(rsa.sign(text,privkey,'SHA-1'))
return signature
def ballot2form(ballot_model,readonly=False, vars=None, counters=None):
"""If counters is passed this counts the results in the ballot.
If readonly is False, then the voter has not yet voted; if readonly
is True, then they have just voted."""
ballot_structure = json.loads(ballot_model)
ballot = FORM()
for question in ballot_structure:
div =DIV(_class="question")
ballot.append(div)
div.append(MARKMIN(question['preamble']))
table = TABLE()
div.append(table)
name = question['name']
if counters:
options = []
for answer in question['answers']:
key = name+'/simple-majority/'+answer
options.append((counters.get(key,0), answer))
options.sort(reverse=True)
options = map(lambda a: a[1], options)
else:
options = question['answers']
if question['randomize']:
random.shuffle(options)
for answer in options:
key = name + '/simple-majority/' + answer
if not counters:
if question['algorithm'] == 'simple-majority':
inp = INPUT(_name=question['name'], _type="radio", _value=answer)
if vars and vars.get(name) == answer:
inp['_checked'] = True
if readonly:
inp['_readonly'] = True
else:
inp = STRONG(counters.get(key, 0))
table.append(TR(TD(inp),TD(answer)))
if not readonly and not counters:
ballot.append(INPUT(_type='submit', _value="Submit Your Ballot!"))
return ballot
def form2ballot(ballot_model, token, vars, results):
ballot_content = ballot2form(ballot_model, readonly=True, vars=vars).xml()
if token: ballot_content += '<pre>\n%s\n</pre>' % token
return '<div class="ballot">%s</div>' % ballot_content.strip()
def blank_ballot(token):
ballot_content = '<h2>Blank</h2>'
if token: ballot_content += '<pre>\n%s\n</pre>' % token
return '<div class="ballot">%s</div>' % ballot_content