summaryrefslogtreecommitdiff
path: root/tko/query_history.cgi
blob: a1e2f3a9742cd0d733ed38f75010273c193031c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python

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",
                     "query_history", where)
    except MySQLdb.ProgrammingError, err:
        print err
        rows = ()
    print '<table border="1">'
    ##  Display history starting with the most recent queries
    for row in reversed(rows):
        (time_created, user_comment, tko_url) = row
        print '<tr>'
        print '<td>&nbsp;%s&nbsp;</td>' % time_created
        print '<td>&nbsp;%s&nbsp;</td>' % user_comment
        dict_url = {'delete':time_created}
        link = 'save_query.cgi?' + urllib.urlencode(dict_url)
        print '<td>&nbsp;<a href="%s">Delete</a>&nbsp;</td>' % link
        print '<td><a href="%s">%s</a></td>' % (tko_url, tko_url)
        print '</tr>'
    print '</table>'

    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 '<br><a href="%s">Autotest Results</a><br>' % back_link


def main():
    print "Content-type: text/html\n"
    print
    # create the actual page
    print '<html><head><title>'
    print 'History of TKO usage'
    print '</title></head><body>'
    body()
    print '</body></html>'


main()