Poll script in Python

Dan Bishop danb_83 at yahoo.com
Mon Oct 18 20:27:07 EDT 2004


fuzzyman at gmail.com (Michael Foord) wrote in message news:<6f402501.0410171605.70b0c279 at posting.google.com>...
> export at hope.cz (Lad) wrote in message news:<81a41dd.0410160259.4767918f at posting.google.com>...
> > Is there a poll script available in Python?
> 
> I'm very interested in increasing the range of 'standard' CGI scripts
> available in python. If you (or anyone else ?) wants help in writing a
> simple one that could be used as a plugin to other webpages then
> contact me and we'll work on it.

Here's some code (with organization-specific stuff replaced by
asterisks) that I wrote for polls that use ranked ballots:

# vote.cgi

#!/usr/bin/python

import os

def printError(message):
   print '      <p>%s</p>\n   </body>\n</html>' % message

def ordinal(n):
   if n == 1:
      return str(n) + 'st'
   if n == 2:
      return str(n) + 'nd'
   if n == 3:
      return str(n) + 'rd'
   if n < 20:
      return str(n) + 'th'
   return ordinal(n % 10)

def printBallot():
   candidates = file('CANDIDATES').read().splitlines()
   n = len(candidates)
   print '      <form method="post" action="castvote.cgi" />'
   print '         <table>'
   print '            <tr>'
   print '               <th>Candidate</th>'
   for i in xrange(1, n + 1):
      print '               <th>%s choice</th>' % ordinal(i)
   print '            </tr>'
   for (candidateNumber, candidateName) in enumerate(candidates):
      print '            <tr>'
      print '               <td class="candidate">%s</td>' %
candidateName
      for i in xrange(1, n + 1):
         print '               <td>'
         print '                  <input type="radio" name="rank-%d" '
\
                                   'value="%d"' % (candidateNumber,
i),
         if i == n:
            print 'checked',
         print '/>'
         print '               </td>'
      print '            </tr>'
   print '         </table>'
   print '         <p class="votesubmission">'
   print '            <input type="submit" value="Submit Ballot" />'
   print '         </p>'
   print '      </form>'
   print '   </body>'
   print '</html>'

print '''Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
   <head>
      <meta http-equiv="Content-Type" content="text/html;
charset=us-ascii" />
      <link rel="stylesheet" type="text/css" href="../****/style.css"
/>
      <title>Vote!</title>
   </head>
   <body>'''
address = os.environ['REMOTE_ADDR']
if address.startswith(****):
   try:
      ballots = file('BALLOTS').read()
   except IOError:
      ballots = ''
   if (address + '\n') in ballots:
      printError('This isn\'t Chicago.  You\'re only allowed to vote
once.')
   else:
      printBallot()
else:
   printError('You can only vote from within ****')

# castvote.cgi

#!/usr/bin/python

import cgi
import os

address = os.environ['REMOTE_ADDR']
form = cgi.FieldStorage()
ballot = []
i = 0
while True:
   try:
      ballot.append(int(form['rank-%d' % i].value))
   except KeyError:
      break
   i += 1

f = file('BALLOTS', 'a')
f.write('%r # %s\n' % (ballot, address))
f.close()

print '''Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
   <head>
      <meta http-equiv="Content-Type" content="text/html;
charset=us-ascii" />
      <link rel="stylesheet" type="text/css" href="../****/style.css"
/>
      <title>Vote Submitted</title>
   </head>
   <body>
      <p>Thank you for voting in ****'s ****.  Results will be
announced at
         ****.</p>
      <p>Click <a href="../****">here</a> to return to the
website.</p>
   </body>
</html>'''



More information about the Python-list mailing list