Skip to content

Commit

Permalink
added election creator client and small tweaks to make it possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Adida authored and Ben Adida committed Jun 1, 2009
1 parent b606de5 commit c2e724b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 15 deletions.
2 changes: 2 additions & 0 deletions base/__init__.py
Expand Up @@ -21,6 +21,8 @@ def web(func):
but also that enables filters
"""
def apply_before_filter(self, *args, **kwargs):
import logging
logging.info("in apply_before_filter on func " + str(func))
self.before_filter()
return func(self, *args, **kwargs)

Expand Down
4 changes: 3 additions & 1 deletion base/session.py
Expand Up @@ -68,7 +68,9 @@ def admin_protect(func, redirect_to = None):
def ensure_admin(self, *args, **kwargs):
if not get_session().is_admin():
raise cherrypy.HTTPRedirect(redirect_to or users.create_login_url("/"))


import logging
logging.info("func is " + str(func))
return func(self, *args, **kwargs)

return ensure_admin
Expand Down
71 changes: 71 additions & 0 deletions client/electioncreator.py
@@ -0,0 +1,71 @@
"""
Create an Election
usage:
python client/electioncreator.py ELECTION_NAME election_questions.json voters.csv HELIOS_SERVER API_KEY_FILENAME.json SECRET_KEY_FILENAME.txt
"""

from base import utils
from crypto import algs, electionalgs
from client import heliosclient

import sys

ELECTION_NAME = sys.argv[1]
QUESTIONS_FILE = sys.argv[2]
VOTERS_FILE = sys.argv[3]
HELIOS_SERVER = sys.argv[4]
API_KEY_FILE = sys.argv[5]
SECRET_KEY_FILE = sys.argv[6]

def open_and_read_file(file_path):
the_file = open(file_path, "r")
the_content = the_file.read()
the_file.close()
return the_content

# parse the json of questions
print "doing questions"
questions = utils.from_json(open_and_read_file(QUESTIONS_FILE))

print "doing api_key"
# parse the json for api keys
api_key = utils.from_json(open_and_read_file(API_KEY_FILE))

# instantiate the client
# modify variables here
# api_key_file should contain {consumer_key: 'test', consumer_secret: 'test', access_token: '123', access_token_secret : '123'}
helios = heliosclient.HeliosClient(api_key,
host = HELIOS_SERVER,
port = 8080)

# get the El Gamal Parameters
params = helios.params()

# generate a keypair
kp = params.generate_keypair()

# create the election remotely
election_id = helios.election_new(ELECTION_NAME, kp.pk)

print "election ID is " + election_id

# set questions
helios.election_questions_save(election_id, questions)

# upload the voters
import csv
voter_reader = csv.reader(open(VOTERS_FILE))
for voter_row in voter_reader:
helios.election_voter_add(election_id, name = voter_row[0], email = voter_row[1])
print "added " + voter_row[1]

# freeze it
helios.election_freeze(election_id)

print "election questions set and frozen"

# secret key
sk_file = open(SECRET_KEY_FILE, "w")
sk_file.write(utils.to_json(kp.sk.toJSONDict()))
sk_file.close()
4 changes: 4 additions & 0 deletions client/heliosclient.py
Expand Up @@ -42,6 +42,10 @@ def election_set_reg(self, election_id, open_reg=False):
def election_questions_save(self, election_id, questions):
result = self.post("/elections/%s/save_questions" % election_id, {'questions_json' : utils.to_json(questions)})
return result == "SUCCESS"

def election_voter_add(self, election_id, name, email):
result = self.post("/elections/%s/voters/" % election_id, {'name' : name, 'email' : email})
return result == "SUCCESS"

def election_freeze(self, election_id):
result = self.post("/elections/%s/freeze_2" % election_id, {})
Expand Down
25 changes: 16 additions & 9 deletions controllers/admin.py
Expand Up @@ -31,16 +31,13 @@ def index(self):
Display admin homepage.
"""
return self.render('index')

@web
@session.admin_protect
def clients(self):
"""
Display API clients
"""
clients = do.APIClient.selectAll()
return self.render('clients')

def client_delete(self,consumer_key):
do.APIClient.get_by_consumer_key(consumer_key).delete()
self.redirect("./")

@web
@session.admin_protect
def client_new(self, consumer_key, consumer_secret, access_token, access_token_secret):
Expand All @@ -51,4 +48,14 @@ def client_new(self, consumer_key, consumer_secret, access_token, access_token_s
new_client.access_token_secret = access_token_secret
new_client.save()
self.redirect("./")


@web
@session.admin_protect
def clients(self):
"""
Display API clients
"""
clients = do.APIClient.selectAll()
return self.render('clients')


5 changes: 4 additions & 1 deletion controllers/election.py
Expand Up @@ -100,7 +100,10 @@ def add(self, email, name, category=None):
v.generate_password()
v.insert()

raise cherrypy.HTTPRedirect("../voters_manage")
if user:
raise cherrypy.HTTPRedirect("../voters_manage")
else:
return SUCCESS

@web
def submit(self, voter, email, password, encrypted_vote):
Expand Down
6 changes: 3 additions & 3 deletions index.yaml
Expand Up @@ -10,14 +10,14 @@ indexes:
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.

# Used once in query history.
# Used 23 times in query history.
- kind: ElectionExponent
properties:
- name: election
- name: exponent
direction: desc

# Used 5 times in query history.
# Unused in query history -- copied from input.
- kind: KeyShare
properties:
- name: election
Expand All @@ -43,7 +43,7 @@ indexes:
- name: tallied_at
- name: cast_id

# Used 8 times in query history.
# Used 3 times in query history.
- kind: Voter
properties:
- name: election
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/clients.tmpl
Expand Up @@ -8,7 +8,7 @@
#for $client in $clients
<tr><td>${client.consumer_key}</td><td>${client.consumer_secret}</td><td>${client.access_token}</td><td>${client.access_token_secret}</td>
<td>
<form method="post" action="delete" onsubmit="return confirm('You\'re about to delete $client.consumer_key...');">
<form method="post" action="client_delete" onsubmit="return confirm('You\'re about to delete $client.consumer_key...');">
<input type="hidden" name="consumer_key" value="${client.consumer_key}" />
<input type="submit" value="delete" class="prettysmall" />
</form>
Expand Down

0 comments on commit c2e724b

Please sign in to comment.