mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame] | 3 | import os, cgi, cgitb, time, urllib |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 4 | import db, unique_cookie |
| 5 | |
| 6 | ## setting script globals |
| 7 | form = cgi.FieldStorage() |
| 8 | if 'label' in form.keys(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 9 | comment = form['label'].value |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 10 | else: |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 11 | comment = '' |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame] | 12 | dict_url = {} |
| 13 | for key in form.keys(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 14 | dict_url[key] = form[key].value |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame] | 15 | |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 16 | tm = time.asctime() |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame] | 17 | uid = unique_cookie.unique_id('tko_history') |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 18 | HTTP_REFERER = os.environ.get('HTTP_REFERER') |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 19 | if HTTP_REFERER is None: |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 20 | ## fall back strategy for proxy connection |
| 21 | ## substitute relative url |
| 22 | HTTP_REFERER = 'compose_query.cgi?' + urllib.urlencode(dict_url) |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class QueryHistoryError(Exception): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 26 | pass |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def log_query(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 30 | db_obj = db.db() |
| 31 | data_to_insert = {'uid':uid, 'time_created':tm, |
| 32 | 'user_comment':comment, 'url':HTTP_REFERER } |
| 33 | try: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 34 | db_obj.insert('tko_query_history', data_to_insert) |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 35 | except: |
| 36 | raise QueryHistoryError("Could not save query") |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 37 | |
| 38 | |
mbligh | 099c142 | 2008-06-06 17:42:09 +0000 | [diff] [blame] | 39 | def delete_query(time_stamp): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 40 | ## query is marked for delete by time stamp |
| 41 | db_obj = db.db() |
| 42 | data_to_delete = {'time_created':time_stamp} |
| 43 | try: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 44 | db_obj.delete('tko_query_history', data_to_delete) |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 45 | except Exception: |
| 46 | raise QueryHistoryError("Could not delete query") |
| 47 | |
mbligh | 099c142 | 2008-06-06 17:42:09 +0000 | [diff] [blame] | 48 | |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 49 | def body(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 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" |
mbligh | 099c142 | 2008-06-06 17:42:09 +0000 | [diff] [blame] | 61 | |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 62 | print '<a href="query_history.cgi">View saved queries</a> ' |
| 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>' |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def main(): |
mbligh | 15110b7 | 2009-01-13 23:33:33 +0000 | [diff] [blame] | 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>' |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | main() |