mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys, os |
mbligh | 8bcd23a | 2009-02-03 19:14:06 +0000 | [diff] [blame] | 4 | import common |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 5 | import MySQLdb |
| 6 | import urllib, db, unique_cookie |
| 7 | |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame] | 8 | uid = unique_cookie.unique_id('tko_history') |
| 9 | |
| 10 | |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 11 | def body(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 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", |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 17 | "tko_query_history", where) |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 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> %s </td>' % time_created |
| 27 | print '<td> %s </td>' % user_comment |
| 28 | dict_url = {'delete':time_created} |
| 29 | link = 'save_query.cgi?' + urllib.urlencode(dict_url) |
| 30 | print '<td> <a href="%s">Delete</a> </td>' % link |
| 31 | print '<td><a href="%s">%s</a></td>' % (tko_url, tko_url) |
| 32 | print '</tr>' |
| 33 | print '</table>' |
mbligh | 099c142 | 2008-06-06 17:42:09 +0000 | [diff] [blame] | 34 | |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 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 |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def main(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 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>' |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | main() |