#!/usr/bin/python2
import os, cgi, cgitb, time, urllib
import db, unique_cookie
## setting script globals
form = cgi.FieldStorage()
if 'label' in form.keys():
    comment = form['label'].value
else:
    comment = ''
dict_url = {}
for key in form.keys():
    dict_url[key] = form[key].value
tm = time.asctime()
uid = unique_cookie.unique_id('tko_history')
HTTP_REFERER = os.environ.get('HTTP_REFERER')
if HTTP_REFERER is None:
    ## fall back strategy for proxy connection
    ## substitute relative url
    HTTP_REFERER = 'compose_query.cgi?' + urllib.urlencode(dict_url)    
class QueryHistoryError(Exception):
    pass
def log_query():
    db_obj = db.db()
    data_to_insert = {'uid':uid, 'time_created':tm,
              'user_comment':comment, 'url':HTTP_REFERER }
    try:
        db_obj.insert('tko_query_history', data_to_insert)
    except:
        raise QueryHistoryError("Could not save query")
def delete_query(time_stamp):
    ## query is marked for delete by time stamp
    db_obj = db.db()
    data_to_delete = {'time_created':time_stamp}
    try:
        db_obj.delete('tko_query_history', data_to_delete)
    except Exception:
        raise QueryHistoryError("Could not delete query")
    
def body():
    if not 'delete' in dict_url.keys():
        log_query()
        print '%s
' % "Your query has been saved"
        print 'time: %s
' % tm
        print 'comments: %s
' % comment
    else:
        ## key 'delete' has arg value of time_stamp
        ## which identifies the query to be deleted
        time_stamp = dict_url['delete']
        delete_query(time_stamp)
        print '%s
' % "Your query has been deleted"
    print 'View saved queries  '
    print '
'
    if not 'delete' in dict_url.keys():
        print 'Back to Autotest
' % HTTP_REFERER
    else:
        print 'Autotest Results
'
def main():
    print "Content-type: text/html\n"
    print '