#!/usr/bin/python # castvote.cgi # Authors: Tom "spot" Callaway # Toshio Kuratomi # Ricky Zhou # Date: 20 April 2008 # Copyright: Tom Callaway # License: GPLv2 # License granted to Red Hat, Inc under the terms # specified in the Fedora Project Individual Contributor License # Agreement. # Description: The cgi script to enter the voter's ballot into the # database. # Id: $Id: castvote.cgi,v 1.16 2008/04/21 04:30:03 toshio Exp $ import cgi import os import sys sys.path.append('/srv/web/accounts') import pgdb import pg import website import voting import votingadmin from fedora.accounts.fas2 import AccountSystem def make_thank_you(electionName, receipt): content = '''
Thank you for voting in the %s Election.
%s ''' % (electionName, receipt) return content def make_body(electionName, fas_url, fas_username, fas_password): form = cgi.FieldStorage() username = os.environ['REMOTE_USER'] fas = AccountSystem(fas_url, fas_username, fas_password) content = '' pageTitle = 'Vote for %s Submitted' % electionName try: election = votingadmin.ElectionAdmin(fas=fas, commonName=electionName) except (voting.VotingError, pgdb.Error), e: website.send_email(voting.sendAs, voting.sendErrorsTo, 'Voting Error', '''The Fedora Voting Application failed to retrieve information from the database necessary to save a vote from %s for The %s Election, The error occurred from castvote.cgi. It's message was: %s''' % (username, electionName, str(e))) content = 'Failed to get information on the election' \ ' from the database. Your vote has not been processed. An' \ ' email has been sent to the voting administrators to look' \ ' at what might be broken in the software. Please try to' \ ' recast your ballot later.
' else: # Extract the candidates from the web form ballotCandidates = {} for entry in form.keys(): if entry.startswith('cand'): ballotCandidates[entry[4:]] = form.getvalue(entry) try: receipt = election.process_ballot(username, ballotCandidates) except voting.VotingError, e: # Authorization or validation failed if e.args[0] == 'Already voted in this election': content = 'You have already submitted a' \ ' ballot in this election. You can only vote once.
' elif e.args[0] == 'Not authorized': content = 'You are not a member of one of' \ ' the groups eligible to vote in this election. If' \ ' you think you should be, please contact someone' \ ' ASAP.
' elif e.args[0] == 'Election is not in progress': content = 'The ' + electionName + ' Election' \ ' is not currently taking place. You cannot vote in' \ ' it at this time.
' else: website.send_email(voting.sendAs, voting.sendErrorsTo, 'Voting Error', '''The Fedora Voting Application failed to save a ballot cast by %s in the %s election. It received a VotingError with message %s in castvote.cgi.''' % (username, electionName, str(e))) content = 'The ballot you have attempted' \ ' to cast is not valid. The admins have been emailed' \ ' to see why the website is trying to send malformed' \ ' ballots.
' except (pgdb.Error, pg.Error), e: website.send_email(voting.sendAs, voting.sendErrorsTo, 'Database Error', '''The Fedora Voting Application was unable to save a ballot cast by %s in the %s election. It received a DatabaseError with the message %s in castvote.cgi.''' %(username, electionName, str(e))) content = 'There was a database error while' \ ' processing your ballot. The ballot was not saved.' \ ' The voting admins have been emailed to look into' \ ' the problem. Please try to vote again later.
' else: content = make_thank_you(electionName, receipt) return pageTitle, content if __name__ == '__main__': content = '' try: config = voting.get_config() except: content = 'Voting app is not configured.' electionName = config.get('global', 'election_name', None) fas_username = config.get('global', 'fas_username', None) fas_password = config.get('global', 'fas_password', None) fas_url = config.get('global', 'fas_url', None) if not (electionName and fas_username and fas_password and fas_url): content = 'One of the mandatory election variables was not configured' if not content: # Load content from the database pageTitle, content = make_body(electionName, fas_url, fas_username, fas_password) else: pageTitle = 'Fedora Voting App -- Error Page' website.print_header(pageTitle) print content website.print_footer(pageTitle, 'http://www.fedoraproject.org')