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(): |
| 9 | comment = form['label'].value |
| 10 | else: |
| 11 | comment = '' |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame^] | 12 | dict_url = {} |
| 13 | for key in form.keys(): |
| 14 | dict_url[key] = form[key].value |
| 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 | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame^] | 19 | if HTTP_REFERER == None: |
| 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): |
| 26 | pass |
| 27 | |
| 28 | |
| 29 | def log_query(): |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 30 | db_obj = db.db() |
| 31 | data_to_insert = {'uid':uid, 'time_created':tm, |
mbligh | d34763f | 2008-06-06 14:28:10 +0000 | [diff] [blame^] | 32 | 'user_comment':comment, 'url':HTTP_REFERER } |
mbligh | 94f07a2 | 2008-05-21 17:10:57 +0000 | [diff] [blame] | 33 | try: |
| 34 | db_obj.insert('query_history', data_to_insert) |
| 35 | except: |
| 36 | raise QueryHistoryError("Could not save query") |
| 37 | |
| 38 | |
| 39 | def body(): |
| 40 | log_query() |
| 41 | print '<b>%s</b><br><br>' % "Your query has been saved" |
| 42 | print 'time: %s<br>' % tm |
| 43 | print 'comments: %s<br><br>' % comment |
| 44 | print '<table><tr align="center">' |
| 45 | print '<td align="center">' |
| 46 | print '<a href="query_history.cgi">View saved queries</a> ' |
| 47 | print '</td>' |
| 48 | print '<td align="center">' |
| 49 | print '<a href="%s">Back to Autotest</a><br>' % HTTP_REFERER |
| 50 | print '</td>' |
| 51 | |
| 52 | |
| 53 | def main(): |
| 54 | print "Content-type: text/html\n" |
| 55 | print '<html><head><title>' |
| 56 | print '</title></head>' |
| 57 | print '<body>' |
| 58 | body() |
| 59 | print '</body>' |
| 60 | print '</html>' |
| 61 | |
| 62 | |
| 63 | main() |
| 64 | |
| 65 | |
| 66 | |