User:Eleanorg/2.2/sloganVoting: Difference between revisions
No edit summary |
|||
Line 62: | Line 62: | ||
howMany = len(slogans) # find out how many items in 'slogans' list | howMany = len(slogans) # find out how many items in 'slogans' list | ||
#print howMany | #print howMany | ||
random = random.randint(0, | options = howMany-1 # subtract one - highest index no. of list will be one less than total number (as numbered from 0.) | ||
#print random | #print options | ||
mySlogan = slogans[random | 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 | #print mySlogan | ||
Revision as of 16:39, 25 January 2013
Visualizing consensus, or lack thereof, through typography.
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>"""