SHARE
TWEET

survey.py

a guest Feb 14th, 2011 227 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from mod_python import apache
  2. from mod_python import util
  3.  
  4. conn = None
  5. csr = None
  6.  
  7. def _db_connect():
  8.    try:
  9.       import MySQLdb
  10.       conn = MySQLdb.connect(host = "localhost",
  11.                              user = "mysql",
  12.                              passwd = "password",
  13.                              db = "cs313",
  14.                              port= 3306)
  15.       return conn
  16.    except MySQLdb.Error, e:
  17.       return None
  18.  
  19. def index(req, results = None):
  20.    req.content_type = "text/html"
  21.    req.send_http_header()
  22.  
  23.  
  24.    if results is not None:
  25.       try:
  26.          # this code is for benchmarking, but if it wasn't
  27.          # session, cookie, and form handling would go here.
  28.  
  29.          req.write(open("/var/www-python/open.html").read())
  30.          results_html = ""
  31.  
  32.          global conn
  33.          if conn is None:
  34.             conn = _db_connect()
  35.          
  36.          global csr
  37.          if csr is None:
  38.             csr = conn.cursor();
  39.          
  40.          results_html += "<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/assign2.css\" />"
  41.          results_html += "<h1>Assignment 2</h1>"
  42.  
  43.          query = """SELECT name
  44.                         , COUNT(name) count
  45.                    FROM survey
  46.                    GROUP BY name"""
  47.          totals = {}
  48.          csr.execute(query)
  49.          results = csr.fetchall()
  50.          for next in results:
  51.             totals[next[0]] = next[1]
  52.  
  53.          results_html += "<h2>" + str(totals['cool']) + " people have voted.</h2>"
  54.  
  55.          query = """SELECT name
  56.                         , value
  57.                         , COUNT(value) count
  58.                    FROM survey
  59.                    GROUP BY name
  60.                         , value
  61.                      UNION
  62.                    SELECT name
  63.                         , value
  64.                         , 0
  65.                    FROM nada
  66.                    WHERE (SELECT COUNT(*)
  67.                           FROM survey
  68.                           WHERE name = nada.name
  69.                           AND value = nada.value) = 0
  70.                    ORDER BY name
  71.                           , count DESC
  72.                           , LENGTH(value)
  73.                           , value
  74.                    """
  75.  
  76.          csr.execute(query)
  77.          results = csr.fetchall()
  78.  
  79.          name = ""
  80.          results_html += "<div id=\"survey\">"
  81.  
  82.          for next in results:
  83.             if next[0] != name:
  84.                results_html += "<div class=\"question\">"
  85.                if next[0] == "cool":
  86.                   results_html += "Coolest Person in the class"
  87.                elif next[0] == "language":
  88.                   results_html += "Favorite Programming Language"
  89.                elif next[0] == "os":
  90.                   results_html += "Favorite OS"
  91.                elif next[0] == "pi":
  92.                   results_html += "Number of Digits of pi Memorized"
  93.                elif next[0] == "weapon":
  94.                   results_html += "Weapon of Chocie"
  95.                results_html += "</div>"
  96.             percent = int(100 * int(next[2]) / int(totals[next[0]]));
  97.             px = percent * 420 / 100;
  98.             results_html += "<div class=\"result\"><div class=\"info\"><div class=\"stats\">"
  99.             results_html += str(next[2]) + " votes - " + str(percent) + "%"
  100.             results_html += "</div>"
  101.             results_html += next[1]
  102.             results_html += "</div><div style=\"width:" + str(px) + "px\" class=\"graph\"></div></div>"
  103.             name = next[0];
  104.  
  105.          results_html += "</div>"
  106.          req.write(results_html)
  107.          req.write(open("/var/www-python/close.html").read())
  108.  
  109.       except MySQLdb.Error, e:
  110.          return "MySQL Error %d: %s" % (e.args[0], e.args[1])
  111.  
  112.    else:
  113.       html = open("/var/www-python/open.html").read()
  114.       html += "<h1>Assignment 2</h1>"
  115.       html += open("/var/www-python/survey.html").read()
  116.       html += "<a href=\"/assign2?results\">View Results</a>"
  117.       html += open("/var/www-python/close.html").read()
  118.       req.write(html)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top