User:Eleanorg/2.2/yesNo
An extremely minimal voting interface displays a phrase with the option to vote yes or no.
Various different scripts process the yes and no totals in different ways, producing a range of contrasting visual outputs.
Code
Make db
Run once.
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
from pymongo import Connection
connection = Connection()
myDB = connection['yes2']
collection = myDB.collection
# create a db to count 'no' votes and 'yes' votes, starting at 0
entry = {'title': 'votes', 'yes': 0, 'no':0}
collection.insert(entry)
Voting interface
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi
import cgitb; cgitb.enable()
import pymongo
from pymongo import Connection
#========= display the phrase and voting form
print "Content-Type: text/html"
print
print """
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="yes.css">
</head>
<body>
<div class="yesDiv">
<p class="yes">YES.</p>
</div>
<div class="input">
<form action="updateYesNoTally.cgi" name="inputForm">
<input type="radio" name="vote" value="yes">Yes
<input type="radio" name="vote" value="no">No
<br >
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>"""
Update vote totals in DB
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi
import cgitb; cgitb.enable()
import pymongo
from pymongo import Connection
#
#======== get vote
form = cgi.FieldStorage() # Grabs whatever input comes from form
vote = form.getvalue("vote")
#========= add user's vote to current tally
connection = Connection()
myDB = connection['yes2']
collection = myDB.collection
for entry in myDB.collection.find({"title": "votes"}):
if vote == "yes":
newYesTotal = entry['yes'] + 1
collection.update( {'title': 'votes'}, {"$set":{'yes': newYesTotal}} )
else if vote == "no":
newNoTotal = entry['no'] + 1
collection.update( {'title': 'votes'}, {"$set":{'no': newNoTotal}} )
#============ print thank you to user
print "Content-Type: text/html"
print
print """
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>"""
Thank you, your vote was added. There are now """ + str(newYesTotal) + """ 'yes' votes, and """ + str(newNoTotal) + """ 'no' votes.
#print vote
print "hello"
print """
</body>
</html>
""""