User:Eleanorg/2.2/sloganVoting

From XPUB & Lens-Based wiki

SlogansScreenshot.png
Visualizing consensus, or lack thereof, through typography. See it here: http://pzwart3.wdka.hro.nl/~egreenhalgh/cgi-bin/2.2/sloganVoting/3votingForm.cgi

Code

Create db

Run once, to create a Mongo db and put a few slogans in it to start.

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
import pymongo
from pymongo import Connection
 
# create db and add some slogans to it. 
 
#======== create db with slogans
# 
connection = Connection()
myDB = connection['consentSlogans1']
collection = myDB.collection  
 
slogans = [ "YES MEANS YES", "consent is sexy", "no means no"]

for x in range(0,3):
	title = "slogan" + str(x)
	slogan = slogans[x]
 
	entry = {'title': title, 'slogan': slogan, 'tally': ' ' }
	collection.insert(entry)


#======= test it's working

for Entry in myDB.collection.find():
	print Entry

Display Voting Form

Display a random slogan from the database and invite user to vote on it.

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
 
import cgi
import cgitb; cgitb.enable()
import pymongo
from pymongo import Connection
import random
 
#========= get a slogan from the db
 
connection = Connection()
myDB = connection['consentSlogans1']
collection = myDB.collection 
 
slogans = []
for entry in myDB.collection.find():		# note: could later add search criteria here to filter which slogans show up - eg most popular ones?
    slogans.append(entry)			# add each entry to 'slogans' list
#print waiting
 
howMany = len(slogans)				# find out how many items in 'slogans' list
#print howMany
options = howMany-1				# subtract one - highest index no. of list will be one less than total number (as numbered from 0.)
#print options
random = random.randint(0,options)		# pick a random number in this range, subtract 1 
#print random		
mySlogan = slogans[random]			# ...and choose the slogan at this index
#print mySlogan
 
#========== show input form
 
print "Content-Type: text/html"
print 
print """
<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <style type="text/css">
    	.printedSlogan { font-size:40px; font-weight: bold;}
    	.textInput { height: 100px; width: 500px; margin: 0px 0px 30px 0px; }
    </style>
 
  </head>
 
<body>
 
<form action="4updateSloganTally.cgi" name="inputForm">	"""

print	"""<p class="printedSlogan">""" + mySlogan + """</p>"""

print """	<br />
	
	
	<input type="radio" name="vote" value="2">Yes<br />
	<input type="radio" name="vote" value="0">No<br />
	<input type="radio" name="vote" value="1">Maybe<br />
	
	<input type="submit" value="Submit" />
</form>
 
</body>
 
</html>"""

Update slogan's popularity rating

Adds user's vote to the current tally for the slogan in the db, and prints a thank you message to user.

#!/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"))		
title = form.getvalue("title")

 
#========= add user's vote to current tally
 
connection = Connection()
myDB = connection['consentSlogans1']
collection = myDB.collection  

for entry in myDB.collection.find({"title": title}):	# find out slogan's current tally
	tally = (entry['tally'])
	newTally = int(tally) + userVote		# add users' vote to it
 
collection.update( {'title': title}, {"$set":{'tally': newTally}} )	# put new tally in db
 
 
##========= print thank you
print "Content-Type: text/html"
print 
print """
<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
 
<body>"""

print """thank you for your vote."""
print "slogan was: " + title
print "its tally was: " + str(tally)
print "you voted:" + str(userVote)
print "new tally is " + str(newTally)

# 
print """
</body>
 
</html>
"""

Do the data-viz

Here is the simplest possible use of the voting figures - using them to set text size and opacity in css.

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
import cgi
import cgitb; cgitb.enable()
import pymongo
from pymongo import Connection
 
 
#========= get slogans & tallies
 
connection = Connection()
myDB = connection['consentSlogans1']
collection = myDB.collection  
	
#========== shameless data-viz!

print "Content-Type: text/html"
print
print """
<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <style type="text/css">
    h2 {position:absolute;left:100px;top:150px;margin:0px; line-height:0%;}
    </style>
 
  </head>

<body>"""

for entry in myDB.collection.find():
        slogan = entry['slogan']
        tally = entry['tally']
        print """<h2 style="opacity:0.""" + str(tally) + """; font-size:""" + str(tally) + """em;">""" + $
print """
</body>

</html>"""