User:Eleanorg/2.2/yesNo: Difference between revisions
(Created page with "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, producin...") |
|||
Line 91: | Line 91: | ||
connection = Connection() | connection = Connection() | ||
myDB = connection['yes2'] | myDB = connection['yes2'] | ||
collection = myDB.collection | collection = myDB.collection | ||
for entry in myDB.collection.find({"title": "votes"}): | for entry in myDB.collection.find({"title": "votes"}): | ||
yesTotal = entry['yes'] | |||
noTotal = entry['no'] | |||
if vote == "yes": | if vote == "yes": | ||
yesTotal = yesTotal + 1 | |||
collection.update( {'title': 'votes'}, {"$set":{'yes': | collection.update( {'title': 'votes'}, {"$set":{'yes': yesTotal}} ) | ||
elif vote == "no": | |||
noTotal = noTotal + 1 | |||
collection.update( {'title': 'votes'}, {"$set":{'no': | collection.update( {'title': 'votes'}, {"$set":{'no': noTotal}} ) | ||
#============ print thank you to user | #============ print thank you to user | ||
Line 113: | Line 116: | ||
<body>""" | <body>""" | ||
Thank you, your vote was added. There are now | print "Thank you, your vote was added. There are now " + str(yesTotal) + """ 'yes' votes, and """ + str(noTotal) + " 'no' votes." | ||
print """ | print """ | ||
</body> | </body> | ||
</html> | </html> | ||
""" | |||
</source> | </source> |
Latest revision as of 15:03, 14 March 2013
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"}):
yesTotal = entry['yes']
noTotal = entry['no']
if vote == "yes":
yesTotal = yesTotal + 1
collection.update( {'title': 'votes'}, {"$set":{'yes': yesTotal}} )
elif vote == "no":
noTotal = noTotal + 1
collection.update( {'title': 'votes'}, {"$set":{'no': noTotal}} )
#============ print thank you to user
print "Content-Type: text/html"
print
print """
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>"""
print "Thank you, your vote was added. There are now " + str(yesTotal) + """ 'yes' votes, and """ + str(noTotal) + " 'no' votes."
print """
</body>
</html>
"""