Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mod_python import apache
- from mod_python import util
- conn = None
- csr = None
- def _db_connect():
- try:
- import MySQLdb
- conn = MySQLdb.connect(host = "localhost",
- user = "mysql",
- passwd = "password",
- db = "cs313",
- port= 3306)
- return conn
- except MySQLdb.Error, e:
- return None
- def index(req, results = None):
- req.content_type = "text/html"
- req.send_http_header()
- if results is not None:
- try:
- # this code is for benchmarking, but if it wasn't
- # session, cookie, and form handling would go here.
- req.write(open("/var/www-python/open.html").read())
- results_html = ""
- global conn
- if conn is None:
- conn = _db_connect()
- global csr
- if csr is None:
- csr = conn.cursor();
- results_html += "<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/assign2.css\" />"
- results_html += "<h1>Assignment 2</h1>"
- query = """SELECT name
- , COUNT(name) count
- FROM survey
- GROUP BY name"""
- totals = {}
- csr.execute(query)
- results = csr.fetchall()
- for next in results:
- totals[next[0]] = next[1]
- results_html += "<h2>" + str(totals['cool']) + " people have voted.</h2>"
- query = """SELECT name
- , value
- , COUNT(value) count
- FROM survey
- GROUP BY name
- , value
- UNION
- SELECT name
- , value
- , 0
- FROM nada
- WHERE (SELECT COUNT(*)
- FROM survey
- WHERE name = nada.name
- AND value = nada.value) = 0
- ORDER BY name
- , count DESC
- , LENGTH(value)
- , value
- """
- csr.execute(query)
- results = csr.fetchall()
- name = ""
- results_html += "<div id=\"survey\">"
- for next in results:
- if next[0] != name:
- results_html += "<div class=\"question\">"
- if next[0] == "cool":
- results_html += "Coolest Person in the class"
- elif next[0] == "language":
- results_html += "Favorite Programming Language"
- elif next[0] == "os":
- results_html += "Favorite OS"
- elif next[0] == "pi":
- results_html += "Number of Digits of pi Memorized"
- elif next[0] == "weapon":
- results_html += "Weapon of Chocie"
- results_html += "</div>"
- percent = int(100 * int(next[2]) / int(totals[next[0]]));
- px = percent * 420 / 100;
- results_html += "<div class=\"result\"><div class=\"info\"><div class=\"stats\">"
- results_html += str(next[2]) + " votes - " + str(percent) + "%"
- results_html += "</div>"
- results_html += next[1]
- results_html += "</div><div style=\"width:" + str(px) + "px\" class=\"graph\"></div></div>"
- name = next[0];
- results_html += "</div>"
- req.write(results_html)
- req.write(open("/var/www-python/close.html").read())
- except MySQLdb.Error, e:
- return "MySQL Error %d: %s" % (e.args[0], e.args[1])
- else:
- html = open("/var/www-python/open.html").read()
- html += "<h1>Assignment 2</h1>"
- html += open("/var/www-python/survey.html").read()
- html += "<a href=\"/assign2?results\">View Results</a>"
- html += open("/var/www-python/close.html").read()
- req.write(html)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement