1#!/usr/bin/python
2
3import sys, os
4import common
5import MySQLdb
6import urllib, db, unique_cookie
7
8uid = unique_cookie.unique_id('tko_history')
9
10
11def body():
12    db_obj = db.db()
13    condition = "uid='%s'" % uid
14    where = (condition,[])
15    try:
16        rows = db_obj.select("time_created,user_comment,url",
17                     "tko_query_history", where)
18    except MySQLdb.ProgrammingError, err:
19        print err
20        rows = ()
21    print '<table border="1">'
22    ##  Display history starting with the most recent queries
23    for row in reversed(rows):
24        (time_created, user_comment, tko_url) = row
25        print '<tr>'
26        print '<td>&nbsp;%s&nbsp;</td>' % time_created
27        print '<td>&nbsp;%s&nbsp;</td>' % user_comment
28        dict_url = {'delete':time_created}
29        link = 'save_query.cgi?' + urllib.urlencode(dict_url)
30        print '<td>&nbsp;<a href="%s">Delete</a>&nbsp;</td>' % link
31        print '<td><a href="%s">%s</a></td>' % (tko_url, tko_url)
32        print '</tr>'
33    print '</table>'
34
35    last_recorded_query = ''
36    if rows:
37        (time_created, user_comment, last_recorded_query) = rows[-1]
38    ## Link "Back to Autotest" on query history page
39    back_link = os.environ.get('HTTP_REFERER')
40    ## possible complications:
41    ## a) HTTP_REFERER = None
42    ## b) HTTP_REFERER is save_query page
43    ## In both cases we still want to get to tko results.
44    ## primary fall back: link to last_recorded_query
45    ## secondary fall back: link to opening tko page
46    if not "compose_query.cgi" in str(back_link):
47        back_link = last_recorded_query
48    if not back_link: ## e.g. history is empty and/or HTTP_REFERER unknown
49        back_link = "compose_query.cgi"
50    print '<br><a href="%s">Autotest Results</a><br>' % back_link
51
52
53def main():
54    print "Content-type: text/html\n"
55    print
56    # create the actual page
57    print '<html><head><title>'
58    print 'History of TKO usage'
59    print '</title></head><body>'
60    body()
61    print '</body></html>'
62
63
64main()
65