def vote(): import hashlib response.menu = [] election_id = request.args(0, cast=int) voter_uuid = request.args(1) election = db.election(election_id) or redirect(URL("invalid_link")) voter = db(db.voter.election_id == election_id)(db.voter.voter_uuid == voter_uuid).select().first() or redirect( URL("invalid_link") ) if not DEBUG_MODE and voter.voted: redirect(URL("voted_already")) if election.deadline and request.now > election.deadline: session.flash = T("Election is closed") if voter.voted: session.flash += T("Your vote was recorded") else: session.flash += T("Your vote was NOT recorded") redirect(URL("results", args=election.id)) response.subtitle = election.title + " / Vote" form = ballot2form(election.ballot_model, readonly=False) if form.accepted: results = {} for_update = not db._uri.startswith("sqlite") # not suported by sqlite # if not for_update: db.executesql('begin immediate transaction;') ballot = db(db.ballot.election_id == election_id)(db.ballot.voted == False).select( orderby="<random>", limitby=(0, 1), for_update=for_update ).first() or redirect(URL("no_more_ballots")) ballot_content = form2ballot( election.ballot_model, token=ballot.ballot_uuid, vars=request.post_vars, results=results ) signature = "signature-" + sign(ballot_content, election.private_key) ballot.update_record( results=str(results), ballot_content=ballot_content, signature=signature, voted=True, assigned=True, voted_on=request.now, ) voter.update_record(voted=True) link = URL("ballot", args=(ballot.ballot_uuid, ballot.signature), scheme="http") message = message_replace( election.voted_email, link=link, election_id=election.id, owner_email=election.created_by.email, title=election.title, signature=signature, ) emailed = email_voter_and_managers(election, voter, ballot, message) session.flash = ( T("Your vote was recorded and we sent you an email") if emailed else T("Your vote was recorded but we failed to email you") ) redirect(link) return dict(form=form)
def vote(): import hashlib response.menu = [] election_id = request.args(0, cast=int) voter_uuid = request.args(1) election = db.election(election_id) or redirect(URL('invalid_link')) voter = db(db.voter.election_id==election_id)\ (db.voter.voter_uuid==voter_uuid).select().first() or \ redirect(URL('invalid_link')) if not DEBUG_MODE and voter.voted: redirect(URL('voted_already')) if election.deadline and request.now > election.deadline: session.flash = T('Election is closed') if voter.voted: session.flash += T('Your vote was recorded') else: session.flash += T('Your vote was NOT recorded') redirect(URL('results', args=election.id)) response.subtitle = election.title + ' / Vote' form = ballot2form(election.ballot_model, readonly=False) form.process() if form.accepted: results = form.vars for_update = not db._uri.startswith('sqlite') # not suported by sqlite #if not for_update: db.executesql('begin immediate transaction;') ballot = db(db.ballot.election_id==election_id)\ (db.ballot.voted==False).select( orderby='<random>',limitby=(0,1),for_update=for_update).first() \ or redirect(URL('no_more_ballots')) ballot_content = form2ballot(election.ballot_model, token=ballot.ballot_uuid, vars=request.post_vars, results=results) signature = 'signature-' + sign(ballot_content, election.private_key) ballot.update_record(results=results, ballot_content=ballot_content, signature=signature, voted=True, assigned=True, voted_on=request.now) voter.update_record(voted=True) link = URL('ballot', args=(ballot.ballot_uuid, ballot.signature), scheme='http') body = message_replace(election.voted_email, link=link, election_id=election.id, owner_email=election.created_by.email, title=election.title, signature=signature) emailed = email_voter_and_managers(election, voter, ballot, body) session.flash = \ T('Your vote was recorded and we sent you an email') \ if emailed else \ T('Your vote was recorded but we failed to email you') redirect(URL('recorded', vars=dict(link=link))) return dict(form=form)
def results(): id = request.args(0, cast=int) or redirect(URL("index")) election = db.election(id) or redirect(URL("index")) if auth.user_id != election.created_by and not (election.deadline and request.now > election.deadline): session.flash = T("Results not yet available") redirect(URL("index")) response.subtitle = election.title + T(" / Results") if DEBUG_MODE or not election.counters: compute_results(election) form = ballot2form(election.ballot_model, counters=unpack_counters(election.counters)) return dict(form=form, election=election)
def vote(): import hashlib response.menu = [] election_id = request.args(0,cast=int) voter_uuid = request.args(1) election = db.election(election_id) or redirect(URL('invalid_link')) voter = db(db.voter.election_id==election_id)\ (db.voter.voter_uuid==voter_uuid).select().first() or \ redirect(URL('invalid_link')) if not DEBUG_MODE and voter.voted: redirect(URL('voted_already')) if election.deadline and request.now>election.deadline: session.flash = T('Election is closed') if voter.voted: session.flash += T('Your vote was recorded') else: session.flash += T('Your vote was NOT recorded') redirect(URL('results',args=election.id)) response.subtitle = election.title + ' / Vote' form = ballot2form(election.ballot_model, readonly=False) form.process() if form.accepted: results = form.vars for_update = not db._uri.startswith('sqlite') # not suported by sqlite #if not for_update: db.executesql('begin immediate transaction;') ballot = db(db.ballot.election_id==election_id)\ (db.ballot.voted==False).select( orderby='<random>',limitby=(0,1),for_update=for_update).first() \ or redirect(URL('no_more_ballots')) ballot_content = form2ballot(election.ballot_model, token=ballot.ballot_uuid, vars=request.post_vars,results=results) signature = 'signature-'+sign(ballot_content,election.private_key) ballot.update_record(results=results, ballot_content=ballot_content, signature=signature, voted=True,assigned=True,voted_on=request.now) voter.update_record(voted=True) link = URL('ballot',args=(ballot.ballot_uuid,ballot.signature), scheme='http') body = message_replace(election.voted_email,link=link, election_id=election.id, owner_email = election.created_by.email, title=election.title,signature=signature) emailed = email_voter_and_managers(election,voter,ballot,body) session.flash = \ T('Your vote was recorded and we sent you an email') \ if emailed else \ T('Your vote was recorded but we failed to email you') redirect(URL('recorded',vars=dict(link=link))) return dict(form=form)
def results(): id = request.args(0,cast=int) or redirect(URL('index')) election = db.election(id) or redirect(URL('index')) if auth.user_id!=election.created_by and \ not(election.deadline and request.now>election.deadline): session.flash = T('Results not yet available') redirect(URL('index')) response.subtitle = election.title + T(' / Results') if (DEBUG_MODE or not election.counters or not election.deadline or request.now<=election.deadline): compute_results(election) form = ballot2form(election.ballot_model, counters=election.counters) return dict(form=form,election=election)
def results(): id = request.args(0,cast=int) or redirect(URL('index')) election = db.election(id) or redirect(URL('index')) if auth.user_id!=election.created_by and \ not(election.deadline and request.now>election.deadline): session.flash = T('Results not yet available') redirect(URL('index')) voted_ballots = db(db.ballot.election_id==election.id)\ (db.ballot.voted==True).select() response.subtitle = election.title + T(' / Results') counters = {} for ballot in voted_ballots: results = unpack_results(ballot.results) for key in results: counters[key] = counters.get(key,0) + results[key] form = ballot2form(election.ballot_model,counters=counters) return dict(form=form,election=election)
def start(): election = db.election(request.args(0, cast=int)) or redirect(URL("index")) response.subtitle = election.title + T(" / Start") demo = ballot2form(election.ballot_model) return dict(demo=demo, election=election)
def start(): election = db.election(request.args(0,cast=int)) or redirect(URL('index')) check_closed(election) response.subtitle = election.title+T(' / Start') demo = ballot2form(election.ballot_model) return dict(demo=demo,election=election)