diff options
Diffstat (limited to 'tko')
-rw-r--r-- | tko/migrations/006_add_table_query_history.py | 18 | ||||
-rw-r--r-- | tko/query_history.cgi | 40 | ||||
-rw-r--r-- | tko/save_query.cgi | 59 | ||||
-rw-r--r-- | tko/unique_cookie.py | 34 |
4 files changed, 151 insertions, 0 deletions
diff --git a/tko/migrations/006_add_table_query_history.py b/tko/migrations/006_add_table_query_history.py new file mode 100644 index 00000000..54b514da --- /dev/null +++ b/tko/migrations/006_add_table_query_history.py @@ -0,0 +1,18 @@ +def migrate_up(manager): + manager.execute_script(ADD_TABLE_QUERY_HISTORY) + + +def migrate_down(manager): + manager.execute_script(DROP_TABLE_QUERY_HISTORY) + + +ADD_TABLE_QUERY_HISTORY = """ +CREATE TABLE IF NOT EXISTS query_history +(uid VARCHAR(32), time_created VARCHAR(32), user_comment VARCHAR(256), +url VARCHAR(65534)); +""" + +DROP_TABLE_QUERY_HISTORY = """ +DROP TABLE query_history; +""" + diff --git a/tko/query_history.cgi b/tko/query_history.cgi new file mode 100644 index 00000000..a3991242 --- /dev/null +++ b/tko/query_history.cgi @@ -0,0 +1,40 @@ +#!/usr/bin/python + +import sys, os +import MySQLdb +import urllib, db, unique_cookie + +def body(): + db_obj = db.db() + uid = unique_cookie.unique_id('tko_history') + condition = "uid='%s'" % uid + where = (condition,[]) + try: + rows = db_obj.select("time_created,user_comment,url", + "query_history", where) + except MySQLdb.ProgrammingError, err: + print err + rows = () + + for row in rows: + (time_created, user_comment, tko_url) = row + print "<hr>" + print time_created + " "*3 + print user_comment + "<br>" + print '<a href="%s">%s</a>' % (tko_url, tko_url) + + +def main(): + print "Content-type: text/html\n" + print + # create the actual page + print '<html><head><title>' + print 'History of TKO usage' + print '</title></head><body>' + body() + print '</body></html>' + + +main() + + diff --git a/tko/save_query.cgi b/tko/save_query.cgi new file mode 100644 index 00000000..7f091144 --- /dev/null +++ b/tko/save_query.cgi @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import os, cgi, cgitb, time +import db, unique_cookie + +## setting script globals +form = cgi.FieldStorage() +if 'label' in form.keys(): + comment = form['label'].value +else: + comment = '' +tm = time.asctime() +HTTP_REFERER = os.environ.get('HTTP_REFERER') + + +class QueryHistoryError(Exception): + pass + + +def log_query(): + uid = unique_cookie.unique_id('tko_history') + db_obj = db.db() + data_to_insert = {'uid':uid, 'time_created':tm, + 'user_comment':comment, 'url':HTTP_REFERER + } + try: + db_obj.insert('query_history', data_to_insert) + except: + raise QueryHistoryError("Could not save query") + + +def body(): + log_query() + print '<b>%s</b><br><br>' % "Your query has been saved" + print 'time: %s<br>' % tm + print 'comments: %s<br><br>' % comment + print '<table><tr align="center">' + print '<td align="center">' + print '<a href="query_history.cgi">View saved queries</a> ' + print '</td>' + print '<td align="center">' + print '<a href="%s">Back to Autotest</a><br>' % HTTP_REFERER + print '</td>' + + +def main(): + print "Content-type: text/html\n" + print '<html><head><title>' + print '</title></head>' + print '<body>' + body() + print '</body>' + print '</html>' + + +main() + + + diff --git a/tko/unique_cookie.py b/tko/unique_cookie.py new file mode 100644 index 00000000..64b3aa76 --- /dev/null +++ b/tko/unique_cookie.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import os, random + +def unique_id(cookie_key): + """ + Find out if remote caller has cookie set on the key. + If not, set cookie on client side: evaluate this key by a random string. + ( unique user identifier ) + In both scenarios return value of the cookie, be it old or newly set one + """ + uid = '' + ## try to retrieve uid from Cookie + if 'HTTP_COOKIE' in os.environ: + ## parse os.environ['HTTP_COOKIE'] + cookies = os.environ['HTTP_COOKIE'].split(';') + key = '%s=' % cookie_key + uid_cookies = [c for c in cookies if c.strip().startswith(key)] + + if uid_cookies: + assert(len(uid_cookies) == 1) + uid_cookie = uid_cookies[0] + uid = uid_cookie.replace(key, '') + + if not uid: + uid = str(random.random())[2:16] # random string of 14 digits + set_cookie_statement = 'Set-Cookie:tko_history_id=%s;' % uid + set_cookie_statement += 'expires=Thu, 26-Dec-2013 22:03:25 GMT;' + print set_cookie_statement + + return uid + + + |