Home | History | Annotate | Download | only in tko
      1 #!/usr/bin/python
      2 
      3 import os, cgi, cgitb, time, urllib
      4 import db, unique_cookie
      5 
      6 ## setting script globals
      7 form = cgi.FieldStorage()
      8 if 'label' in form.keys():
      9     comment = form['label'].value
     10 else:
     11     comment = ''
     12 dict_url = {}
     13 for key in form.keys():
     14     dict_url[key] = form[key].value
     15 
     16 tm = time.asctime()
     17 uid = unique_cookie.unique_id('tko_history')
     18 HTTP_REFERER = os.environ.get('HTTP_REFERER')
     19 if HTTP_REFERER is None:
     20     ## fall back strategy for proxy connection
     21     ## substitute relative url
     22     HTTP_REFERER = 'compose_query.cgi?' + urllib.urlencode(dict_url)    
     23 
     24 
     25 class QueryHistoryError(Exception):
     26     pass
     27 
     28 
     29 def log_query():
     30     db_obj = db.db()
     31     data_to_insert = {'uid':uid, 'time_created':tm,
     32               'user_comment':comment, 'url':HTTP_REFERER }
     33     try:
     34         db_obj.insert('tko_query_history', data_to_insert)
     35     except:
     36         raise QueryHistoryError("Could not save query")
     37 
     38 
     39 def delete_query(time_stamp):
     40     ## query is marked for delete by time stamp
     41     db_obj = db.db()
     42     data_to_delete = {'time_created':time_stamp}
     43     try:
     44         db_obj.delete('tko_query_history', data_to_delete)
     45     except Exception:
     46         raise QueryHistoryError("Could not delete query")
     47     
     48 
     49 def body():
     50     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"
     61 
     62     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>'
     68 
     69 
     70 def main():
     71     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>'
     78 
     79 
     80 main()
     81