blob: d651cf3b0278828c2836e676ab5ad139ecb87f55 [file] [log] [blame]
mbligh94f07a22008-05-21 17:10:57 +00001#!/usr/bin/python
2
mblighd34763f2008-06-06 14:28:10 +00003import os, cgi, cgitb, time, urllib
mbligh94f07a22008-05-21 17:10:57 +00004import db, unique_cookie
5
6## setting script globals
7form = cgi.FieldStorage()
8if 'label' in form.keys():
mbligh15110b72009-01-13 23:33:33 +00009 comment = form['label'].value
mbligh94f07a22008-05-21 17:10:57 +000010else:
mbligh15110b72009-01-13 23:33:33 +000011 comment = ''
mblighd34763f2008-06-06 14:28:10 +000012dict_url = {}
13for key in form.keys():
mbligh15110b72009-01-13 23:33:33 +000014 dict_url[key] = form[key].value
mblighd34763f2008-06-06 14:28:10 +000015
mbligh94f07a22008-05-21 17:10:57 +000016tm = time.asctime()
mblighd34763f2008-06-06 14:28:10 +000017uid = unique_cookie.unique_id('tko_history')
mbligh94f07a22008-05-21 17:10:57 +000018HTTP_REFERER = os.environ.get('HTTP_REFERER')
mblighd876f452008-12-03 15:09:17 +000019if HTTP_REFERER is None:
mbligh15110b72009-01-13 23:33:33 +000020 ## fall back strategy for proxy connection
21 ## substitute relative url
22 HTTP_REFERER = 'compose_query.cgi?' + urllib.urlencode(dict_url)
mbligh94f07a22008-05-21 17:10:57 +000023
24
25class QueryHistoryError(Exception):
mbligh15110b72009-01-13 23:33:33 +000026 pass
mbligh94f07a22008-05-21 17:10:57 +000027
28
29def log_query():
mbligh15110b72009-01-13 23:33:33 +000030 db_obj = db.db()
31 data_to_insert = {'uid':uid, 'time_created':tm,
32 'user_comment':comment, 'url':HTTP_REFERER }
33 try:
showardeab66ce2009-12-23 00:03:56 +000034 db_obj.insert('tko_query_history', data_to_insert)
mbligh15110b72009-01-13 23:33:33 +000035 except:
36 raise QueryHistoryError("Could not save query")
mbligh94f07a22008-05-21 17:10:57 +000037
38
mbligh099c1422008-06-06 17:42:09 +000039def delete_query(time_stamp):
mbligh15110b72009-01-13 23:33:33 +000040 ## query is marked for delete by time stamp
41 db_obj = db.db()
42 data_to_delete = {'time_created':time_stamp}
43 try:
showardeab66ce2009-12-23 00:03:56 +000044 db_obj.delete('tko_query_history', data_to_delete)
mbligh15110b72009-01-13 23:33:33 +000045 except Exception:
46 raise QueryHistoryError("Could not delete query")
47
mbligh099c1422008-06-06 17:42:09 +000048
mbligh94f07a22008-05-21 17:10:57 +000049def body():
mbligh15110b72009-01-13 23:33:33 +000050 if not 'delete' in dict_url.keys():
51 log_query()
52 print '<b>%s</b><br><br>' % "Your query has been saved"
53 print 'time: %s<br>' % tm
54 print 'comments: %s<br><br>' % comment
55 else:
56 ## key 'delete' has arg value of time_stamp
57 ## which identifies the query to be deleted
58 time_stamp = dict_url['delete']
59 delete_query(time_stamp)
60 print '<b>%s</b><br><br>' % "Your query has been deleted"
mbligh099c1422008-06-06 17:42:09 +000061
mbligh15110b72009-01-13 23:33:33 +000062 print '<a href="query_history.cgi">View saved queries</a>&nbsp;&nbsp;'
63 print '<br><br>'
64 if not 'delete' in dict_url.keys():
65 print '<a href="%s">Back to Autotest</a><br>' % HTTP_REFERER
66 else:
67 print '<a href="compose_query.cgi">Autotest Results</a><br>'
mbligh94f07a22008-05-21 17:10:57 +000068
69
70def main():
mbligh15110b72009-01-13 23:33:33 +000071 print "Content-type: text/html\n"
72 print '<html><head><title>'
73 print '</title></head>'
74 print '<body>'
75 body()
76 print '</body>'
77 print '</html>'
mbligh94f07a22008-05-21 17:10:57 +000078
79
80main()