#!/usr/bin/python2
import sys, os
import common
import MySQLdb
import urllib, db, unique_cookie
uid = unique_cookie.unique_id('tko_history')
def body():
db_obj = db.db()
condition = "uid='%s'" % uid
where = (condition,[])
try:
rows = db_obj.select("time_created,user_comment,url",
"tko_query_history", where)
except MySQLdb.ProgrammingError, err:
print err
rows = ()
print '
'
## Display history starting with the most recent queries
for row in reversed(rows):
(time_created, user_comment, tko_url) = row
print ''
print ' %s | ' % time_created
print ' %s | ' % user_comment
dict_url = {'delete':time_created}
link = 'save_query.cgi?' + urllib.urlencode(dict_url)
print ' Delete | ' % link
print '%s | ' % (tko_url, tko_url)
print '
'
print '
'
last_recorded_query = ''
if rows:
(time_created, user_comment, last_recorded_query) = rows[-1]
## Link "Back to Autotest" on query history page
back_link = os.environ.get('HTTP_REFERER')
## possible complications:
## a) HTTP_REFERER = None
## b) HTTP_REFERER is save_query page
## In both cases we still want to get to tko results.
## primary fall back: link to last_recorded_query
## secondary fall back: link to opening tko page
if not "compose_query.cgi" in str(back_link):
back_link = last_recorded_query
if not back_link: ## e.g. history is empty and/or HTTP_REFERER unknown
back_link = "compose_query.cgi"
print '
Autotest Results
' % back_link
def main():
print "Content-type: text/html\n"
print
# create the actual page
print ''
print 'History of TKO usage'
print ''
body()
print ''
main()