summaryrefslogtreecommitdiff
path: root/tko
diff options
context:
space:
mode:
authormbligh <mbligh@592f7852-d20e-0410-864c-8624ca9c26a4>2008-05-21 17:10:57 +0000
committermbligh <mbligh@592f7852-d20e-0410-864c-8624ca9c26a4>2008-05-21 17:10:57 +0000
commit6d9aac8bb08f8e1ad4eba0cc4e83840127075d88 (patch)
treee3b5e858e2d791b06a9adb1899c509aa238ded2c /tko
parentd0acd356678df7f901118f32a3ed6124786dbae5 (diff)
Overview:
Implement user's request: save query on demand a) Add to TKO greed view a form containing button [SaveQuery] and edit control for optional comments b) On saveQuery request TKO make sure that user's cookies contain a unique user's id c) ... and then write into table query_history in TKO: time stamp, comment, uid, url of the query d) Make sure user can read saved queries back ( can read only his/her own queries ) compose_query.cgi - added a form that supports user's request to save cu rrent query save_query.cgi - saves the query query_history.cgi - shows saved queries by this user unique_cookie.py - common cookie related function: set/get user's unique id 006_add_table_query_history.py - database migration. New table introduce d for query history. From: vsamarsk@google.com git-svn-id: svn://test.kernel.org/autotest/trunk@1533 592f7852-d20e-0410-864c-8624ca9c26a4
Diffstat (limited to 'tko')
-rw-r--r--tko/migrations/006_add_table_query_history.py18
-rw-r--r--tko/query_history.cgi40
-rw-r--r--tko/save_query.cgi59
-rw-r--r--tko/unique_cookie.py34
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 + "&nbsp;"*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>&nbsp;&nbsp;'
+ 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
+
+
+