Improvements of query history:
a) User shall be able to delete query from the history
b) Link "Back to Autotest" on query history page
c) Display history starting with the most recent queries

Signed-off-by: Vladimir Samarskiy <vsamarsk@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@1650 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/query_history.cgi b/tko/query_history.cgi
index 0384a84..b947140 100644
--- a/tko/query_history.cgi
+++ b/tko/query_history.cgi
@@ -17,14 +17,36 @@
 	except MySQLdb.ProgrammingError, err:
 		print err
 		rows = ()
-
-	for row in 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 "<hr>"
-		print time_created + "&nbsp;"*3
-		print user_comment + "<br>"
-		print '<a href="%s">%s</a>' % (tko_url, tko_url)
-	print '<hr>'
+		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():