User:Eleanorg/2.2/yes: Difference between revisions
No edit summary |
|||
Line 90: | Line 90: | ||
</html>""" | </html>""" | ||
</source> | |||
===update vote tally=== | |||
And display new sized text to user. (Todo: code for calculating the css is duplicated from prev script.) | |||
<source lang="python"> | |||
#!/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 | |||
userVote = int(form.getvalue("vote")) | |||
#========= add user's vote to current tally | |||
connection = Connection() | |||
myDB = connection['yes1'] | |||
collection = myDB.collection | |||
entry = myDB.collection.find({'title': 'yesVotes'}) | |||
tally = entry[0]['tally'] | |||
newTally = int(tally) + userVote # add users' vote to it | |||
cssOpacity = float(newTally) * 0.001 | |||
cssFontsize = float(newTally) * 0.0001 | |||
collection.update( {'title': 'yesVotes'}, {"$set":{'tally': newTally}} ) # put new tally in db | |||
#========= display the word styled according to no. of votes | |||
print "Content-Type: text/html" | |||
print | |||
print """ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title></title> | |||
<style type="text/css"> | |||
.input {margin:auto; text-align:center; padding:1%; border-bottom:1px solid #ddd;} | |||
input span{float:left;margin:5%;} | |||
.yesDiv {min-width:80%;margin:auto;padding:5%; text-align:center} | |||
.yes {font-size:""" + str(cssFontsize) + """; opacity:""" + str(cssOpacity) + """;} | |||
</style> | |||
</head> | |||
<body> | |||
<div class="input"> | |||
<form action="3updateYes.cgi" name="inputForm"> | |||
<span><input type="radio" name="vote" value="2">Yes</span> | |||
<span><input type="radio" name="vote" value="0">No</span> | |||
<span><input type="radio" name="vote" value="1">Maybe</span><br /><br /> | |||
<input type="submit" value="Submit" /> | |||
</form> | |||
</div> | |||
<div class="yesDiv"> | |||
<p class="yes">YES</p> | |||
</div> | |||
</body> | |||
</html>""" | |||
</source> | </source> |
Revision as of 22:34, 25 January 2013
The word yes is printed to screen. User votes 'yes', 'no', or 'maybe'. For each 'maybe' the word gets x amount bigger. For each 'yes' it gets twice that amount bigger. Each no is counted as zero and does not affect the size.
Code
Make db
Make a db to count the votes. Run once.
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pymongo
from pymongo import Connection
connection = Connection()
myDB = connection['yes1']
collection = myDB.collection
# create db entry to track votes, starting at zero.
entry = {'title': 'yesVotes', 'tally': 0 }
collection.insert(entry)
Voting interface
Display the word 'YES', with css properties determined by tally in db.
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi
import cgitb; cgitb.enable()
import pymongo
from pymongo import Connection
import random
#========= get votes tally from db
connection = Connection()
myDB = connection['yes1']
collection = myDB.collection
entry = myDB.collection.find({'title': 'yesVotes'})
tally = entry[0]['tally'] # entry[0] is the hash in our db. Grab value of 'tally' from the hash.
cssOpacity = float(tally) * 0.001
cssFontsize = float(tally) * 0.0001
#========= display the word styled according to no. of votes
print "Content-Type: text/html"
print
print """
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.input {margin:auto; text-align:center; padding:1%; border-bottom:1px solid #ddd;}
input span{float:left;margin:5%;}
.yesDiv {min-width:80%;margin:auto;padding:5%; text-align:center}
.yes {font-size:""" + str(cssFontsize) + """; opacity:""" + str(cssOpacity) + """;}
</style>
</head>
<body>
<div class="input">
<form action="3updateTally.cgi" name="inputForm">
<span><input type="radio" name="vote" value="2">Yes</span>
<span><input type="radio" name="vote" value="0">No</span>
<span><input type="radio" name="vote" value="1">Maybe</span><br /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="yesDiv">
<p class="yes">YES</p>
</div>
</body>
</html>"""
update vote tally
And display new sized text to user. (Todo: code for calculating the css is duplicated from prev script.)
#!/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
userVote = int(form.getvalue("vote"))
#========= add user's vote to current tally
connection = Connection()
myDB = connection['yes1']
collection = myDB.collection
entry = myDB.collection.find({'title': 'yesVotes'})
tally = entry[0]['tally']
newTally = int(tally) + userVote # add users' vote to it
cssOpacity = float(newTally) * 0.001
cssFontsize = float(newTally) * 0.0001
collection.update( {'title': 'yesVotes'}, {"$set":{'tally': newTally}} ) # put new tally in db
#========= display the word styled according to no. of votes
print "Content-Type: text/html"
print
print """
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.input {margin:auto; text-align:center; padding:1%; border-bottom:1px solid #ddd;}
input span{float:left;margin:5%;}
.yesDiv {min-width:80%;margin:auto;padding:5%; text-align:center}
.yes {font-size:""" + str(cssFontsize) + """; opacity:""" + str(cssOpacity) + """;}
</style>
</head>
<body>
<div class="input">
<form action="3updateYes.cgi" name="inputForm">
<span><input type="radio" name="vote" value="2">Yes</span>
<span><input type="radio" name="vote" value="0">No</span>
<span><input type="radio" name="vote" value="1">Maybe</span><br /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="yesDiv">
<p class="yes">YES</p>
</div>
</body>
</html>"""