#!/usr/bin/env python
import cgitb; cgitb.enable()
import cgi
import MySQLdb

db = "wanderin_suzanne"   # your username (= login name  = account name )
host = "localhost"     # = "localhost", the server your are on.
user = "wanderin_stan" # your Database name is the same as your account name.
passwd = "fortune"     # Your account password
db = MySQLdb.connect(db=db, host=host, user=user, passwd=passwd)

def safeLookup(dict,key):
   try:
      x = dict[key].value
   except KeyError:
      return ""
   else:
      return x
      
def xml_out(out):
	print 'Content-type: application/xml\n'
	print '<?xml version="1.0" ?><?xml-stylesheet type="text/xsl" href="suzanne.xsl"?>'	
	print '<doc>'
	print out
	print '</doc>'
	
o=""
form = cgi.FieldStorage()
if not (form.has_key("action")):
	o += "<eval_form>"
	o += "  <object_uri>"+safeLookup(form,"fObjectURI")+"</object_uri>"
	o += "  <user_uri>"+safeLookup(form,"fUserURI")+"</user_uri>"
	o += "  <user_password>"+safeLookup(form,"fUserPassword")+"</user_password>"
	o += "</eval_form>"
	
else:

	# damn, don't have MySQL 5, so can't user stored procs
	q = "INSERT INTO `tbl_trusts` "
	q+= "(  `user_uri` , `object_uri` , `degree` , `last_update` , `comment` , `comment_uri` )"
	q+= "VALUES ("
	q+= "'"+safeLookup(form,"fUserURI") + "'," 
	q+= "'"+safeLookup(form,"fObjectURI") + "'," 
	q+= "'"+safeLookup(form,"fDegree") + "'," 
	q+= "NOW( ),"
	q+= "'" + safeLookup(form,"fComment") + "',"
	q+= "'" + safeLookup(form,"fCommentURI") + "'"
	q+=")"

	c = db.cursor()
	c.execute(q)
	results = c.fetchall()

	o += "<p>"
	o += "Results (probably) added to your database."
	o += repr(results)
	o += "</p>"

xml_out(o)

